@@ -38,32 +38,32 @@ module Cache = struct
3838 let invalidate t =
3939 Mutex. execute t.m
4040 (fun () ->
41- t.item < - None ;
41+ t.item < - None ;
4242 )
4343
4444 let get t =
4545 Mutex. execute t.m
4646 (fun () ->
47- match t.item with
48- | Some x -> x
49- | None ->
50- let x = t.fn () in
51- t.item < - Some x;
52- x
47+ match t.item with
48+ | Some x -> x
49+ | None ->
50+ let x = t.fn () in
51+ t.item < - Some x;
52+ x
5353 )
5454end
5555
5656let hostname = Cache. make
57- (fun () ->
58- let h = Unix. gethostname () in
59- Backtrace. set_my_name (Filename. basename(Sys. argv.(0 )) ^ " @ " ^ h);
60- h
61- )
57+ (fun () ->
58+ let h = Unix. gethostname () in
59+ Backtrace. set_my_name (Filename. basename(Sys. argv.(0 )) ^ " @ " ^ h);
60+ h
61+ )
6262
6363let invalidate_hostname_cache () = Cache. invalidate hostname
6464
6565let get_thread_id () =
66- try Thread. id (Thread. self () ) with _ -> - 1
66+ try Thread. id (Thread. self () ) with _ -> - 1
6767
6868module ThreadLocalTable = struct
6969 type 'a t = {
@@ -83,14 +83,15 @@ module ThreadLocalTable = struct
8383 let remove t =
8484 let id = get_thread_id () in
8585 Mutex. execute t.m (fun () -> Hashtbl. remove t.tbl id)
86-
86+
8787 let find t =
8888 let id = get_thread_id () in
8989 Mutex. execute t.m (fun () ->
90- if Hashtbl. mem t.tbl id
91- then Some (Hashtbl. find t.tbl id)
92- else None
93- )
90+ try
91+ Some (Hashtbl. find t.tbl id)
92+ with
93+ | _ -> None
94+ )
9495end
9596
9697let names = ThreadLocalTable. make ()
@@ -127,21 +128,21 @@ let loglevel = ref default_loglevel
127128
128129let disabled_modules () =
129130 Mutex. execute loglevel_m (fun () ->
130- Hashtbl. fold (fun key _ acc -> key :: acc) logging_disabled_for []
131- )
131+ Hashtbl. fold (fun key _ acc -> key :: acc) logging_disabled_for []
132+ )
132133
133134let is_disabled brand level =
134135 Mutex. execute loglevel_m (fun () ->
135- Syslog. is_masked ~threshold: ! loglevel level ||
136+ Syslog. is_masked ~threshold: ! loglevel level ||
136137 Hashtbl. mem logging_disabled_for (brand, level)
137- )
138+ )
138139
139140let reset_levels () =
140141 Mutex. execute loglevel_m (fun () ->
141- loglevel := default_loglevel;
142- Hashtbl. clear logging_disabled_for
143- )
144-
142+ loglevel := default_loglevel;
143+ Hashtbl. clear logging_disabled_for
144+ )
145+
145146
146147let facility = ref Syslog. Daemon
147148let facility_m = Mutex. create ()
@@ -196,9 +197,9 @@ let with_thread_named name f x =
196197 raise e
197198
198199module StringSet = Set. Make (struct type t= string let compare= Pervasives. compare end )
199- let debug_keys = ref StringSet. empty
200+ let debug_keys = ref StringSet. empty
200201let get_all_debug_keys () =
201- StringSet. fold (fun key keys -> key::keys) ! debug_keys []
202+ StringSet. fold (fun key keys -> key::keys) ! debug_keys []
202203
203204let dkmutex = Mutex. create ()
204205
@@ -209,78 +210,78 @@ end
209210let all_levels = [Syslog. Debug ; Syslog. Info ; Syslog. Warning ; Syslog. Err ]
210211
211212let add_to_stoplist brand level =
212- Hashtbl. replace logging_disabled_for (brand, level) ()
213+ Hashtbl. replace logging_disabled_for (brand, level) ()
213214
214215let remove_from_stoplist brand level =
215- Hashtbl. remove logging_disabled_for (brand, level)
216+ Hashtbl. remove logging_disabled_for (brand, level)
216217
217218let disable ?level brand =
218- let levels = match level with
219- | None -> all_levels
220- | Some l -> [l]
221- in
222- Mutex. execute loglevel_m (fun () ->
223- List. iter (add_to_stoplist brand) levels
224- )
219+ let levels = match level with
220+ | None -> all_levels
221+ | Some l -> [l]
222+ in
223+ Mutex. execute loglevel_m (fun () ->
224+ List. iter (add_to_stoplist brand) levels
225+ )
225226
226227let enable ?level brand =
227- let levels = match level with
228- | None -> all_levels
229- | Some l -> [l]
230- in
231- Mutex. execute loglevel_m (fun () ->
232- List. iter (remove_from_stoplist brand) levels
233- )
228+ let levels = match level with
229+ | None -> all_levels
230+ | Some l -> [l]
231+ in
232+ Mutex. execute loglevel_m (fun () ->
233+ List. iter (remove_from_stoplist brand) levels
234+ )
234235
235236let set_level level =
236- Mutex. execute loglevel_m (fun () ->
237- loglevel := level
238- )
237+ Mutex. execute loglevel_m (fun () ->
238+ loglevel := level
239+ )
239240
240241module type DEBUG = sig
241- val debug : ('a , unit , string , unit ) format4 -> 'a
242+ val debug : ('a , unit , string , unit ) format4 -> 'a
242243
243- val warn : ('a , unit , string , unit ) format4 -> 'a
244+ val warn : ('a , unit , string , unit ) format4 -> 'a
244245
245- val info : ('a , unit , string , unit ) format4 -> 'a
246+ val info : ('a , unit , string , unit ) format4 -> 'a
246247
247- val error : ('a , unit , string , unit ) format4 -> 'a
248+ val error : ('a , unit , string , unit ) format4 -> 'a
248249
249- val audit : ?raw : bool -> ('a , unit , string , string ) format4 -> 'a
250+ val audit : ?raw : bool -> ('a , unit , string , string ) format4 -> 'a
250251
251- val log_backtrace : unit -> unit
252+ val log_backtrace : unit -> unit
252253
253- val log_and_ignore_exn : (unit -> unit ) -> unit
254+ val log_and_ignore_exn : (unit -> unit ) -> unit
254255end
255256
256257module Make = functor (Brand : BRAND ) -> struct
257258 let _ =
258- Mutex. execute dkmutex (fun () ->
259- debug_keys := StringSet. add Brand. name ! debug_keys)
260-
261- let output level priority (fmt : ('a, unit, string, 'b) format4 ) =
262- Printf. kprintf
263- (fun s ->
264- if not (is_disabled Brand. name level)
265- then output_log Brand. name level priority s
266- ) fmt
267-
268- let debug fmt = output Syslog. Debug " debug" fmt
269- let warn fmt = output Syslog. Warning " warn" fmt
270- let info fmt = output Syslog. Info " info" fmt
271- let error fmt = output Syslog. Err " error" fmt
272- let audit ?(raw =false ) (fmt : ('a, unit, string, 'b) format4 ) =
273- Printf. kprintf
274- (fun s ->
275- let msg = if raw then s else format true Brand. name " audit" s in
276- Syslog. log Syslog. Local6 Syslog. Info msg;
277- msg
278- ) fmt
279-
280- let log_backtrace () =
281- let backtrace = Printexc. get_backtrace () in
282- debug " %s" (String. escaped backtrace)
283-
284- let log_and_ignore_exn f =
285- try f () with _ -> log_backtrace ()
259+ Mutex. execute dkmutex (fun () ->
260+ debug_keys := StringSet. add Brand. name ! debug_keys)
261+
262+ let output level priority (fmt : ('a, unit, string, 'b) format4 ) =
263+ Printf. kprintf
264+ (fun s ->
265+ if not (is_disabled Brand. name level)
266+ then output_log Brand. name level priority s
267+ ) fmt
268+
269+ let debug fmt = output Syslog. Debug " debug" fmt
270+ let warn fmt = output Syslog. Warning " warn" fmt
271+ let info fmt = output Syslog. Info " info" fmt
272+ let error fmt = output Syslog. Err " error" fmt
273+ let audit ?(raw =false ) (fmt : ('a, unit, string, 'b) format4 ) =
274+ Printf. kprintf
275+ (fun s ->
276+ let msg = if raw then s else format true Brand. name " audit" s in
277+ Syslog. log Syslog. Local6 Syslog. Info msg;
278+ msg
279+ ) fmt
280+
281+ let log_backtrace () =
282+ let backtrace = Printexc. get_backtrace () in
283+ debug " %s" (String. escaped backtrace)
284+
285+ let log_and_ignore_exn f =
286+ try f () with _ -> log_backtrace ()
286287end
0 commit comments