]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/ocaml/uwobo_logger.ml
snapshot Wed, 27 Nov 2002 02:45:45 +0100
[helm.git] / helm / uwobo / src / ocaml / uwobo_logger.ml
1
2 (* Copyright (C) 2002, HELM Team.
3  * 
4  * This file is part of HELM, an Hypertextual, Electronic
5  * Library of Mathematics, developed at the Computer Science
6  * Department, University of Bologna, Italy.
7  * 
8  * HELM is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * HELM is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with HELM; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21  * MA  02111-1307, USA.
22  * 
23  * For details, see the HELM World-Wide-Web page,
24  * http://cs.unibo.it/helm/.
25  *)
26
27 open Printf;;
28
29 type priority = [ 
30   `Emerg | `Alert | `Crit | `Err | `Warning | `Notice | `Info | `Debug
31 ]
32
33 let int_of_priority = function
34   | `Emerg    -> 0
35   | `Alert    -> 1
36   | `Crit     -> 2
37   | `Err      -> 3
38   | `Warning  -> 4
39   | `Notice   -> 5
40   | `Info     -> 6
41   | `Debug    -> 7
42
43 let string_of_priority = function
44   | `Emerg    -> "EMERGENCY"
45   | `Alert    -> "ALERT"
46   | `Crit     -> "CRITICAL"
47   | `Err      -> "ERROR"
48   | `Warning  -> "WARNING"
49   | `Notice   -> "NOTICE"
50   | `Info     -> "INFO"
51   | `Debug    -> "DEBUG"
52
53 class sysLogger ?(level: priority = `Notice) () =
54   object
55     initializer
56       print_endline (sprintf "Logger started with level %s" (string_of_priority level))
57     val level_no = int_of_priority level
58     val mutable enabled = false
59     method enable = enabled <- true
60     method disable = enabled <- false
61     method log (prio: priority) msg =
62       if enabled && (int_of_priority prio <= level_no) then
63         prerr_endline (sprintf ("%s: %s") (string_of_priority prio) msg)
64   end
65
66 class processingLogger =
67   let html_escape s = (* TODO too naive, use Nethtml.encode instead *)
68     Pcre.replace ~pat:"<" ~templ:"&lt;"
69       (Pcre.replace ~pat:">" ~templ:"&gt;"
70         (Pcre.replace ~pat:"&" ~templ:"&amp;" s))
71   in
72   fun () ->
73   object
74     val mutable log_lines: string list = []
75     method log msg = log_lines <- msg :: log_lines
76     method asText = String.concat "\n" (List.rev log_lines)
77     method asHtml =
78       sprintf
79         "<html><body>\n%s\n</body></html>"
80         (String.concat
81           "<br />\n"
82           (List.map html_escape (List.rev log_lines)))
83   end
84