]> matita.cs.unibo.it Git - helm.git/blob - helm/uwobo/src/ocaml/uwobo_logger.ml
Initial revision
[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 (self)
55     inherit Uwobo_common.threadSafe
56     initializer
57       print_endline (sprintf "Logger started with level %s" (string_of_priority level))
58     val level_no = int_of_priority level
59     val mutable enabled = false
60     method enable = self#doCritical (lazy (enabled <- true))
61     method disable = self#doCritical (lazy (enabled <- false))
62     method log (prio: priority) msg =
63       self#doCritical (lazy (
64         if enabled && (int_of_priority prio <= level_no) then
65           prerr_endline (sprintf ("%s: %s") (string_of_priority prio) msg)
66       ))
67   end
68
69   (** non thread safe, a processingLogger is usually instantied locally for each
70   thread *)
71 class processingLogger () =
72   object
73     val mutable log_lines: string list = []
74     method log msg = log_lines <- msg :: log_lines
75     method asText = String.concat "\n" (List.rev log_lines)
76     method asHtml =
77       sprintf
78         "<html><body>\n%s\n</body></html>"
79         (String.concat
80           "<br />\n"
81           (List.map (Netencoding.Url.encode ~plus:false) (List.rev log_lines)))
82   end
83