Skip to content
This repository has been archived by the owner on May 22, 2018. It is now read-only.

Commit

Permalink
Debug: allow disabling specific log levels
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Hoes <rob.hoes@citrix.com>
  • Loading branch information
robhoes committed May 15, 2013
1 parent 8eab002 commit 09d822c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
38 changes: 27 additions & 11 deletions log/debug.ml
Expand Up @@ -91,17 +91,33 @@ let facility_m = Mutex.create ()
let set_facility f = Mutex.execute facility_m (fun () -> facility := f)
let get_facility () = Mutex.execute facility_m (fun () -> !facility)

let logging_disabled_for = ref []
let all_levels = [Syslog.Debug; Syslog.Info; Syslog.Warning; Syslog.Err]
let logging_disabled_for : (string * Syslog.level) list ref = ref []
let logging_disabled_for_m = Mutex.create ()
let disable brand =
Mutex.execute logging_disabled_for_m
(fun () -> logging_disabled_for := brand :: !logging_disabled_for)
let enable brand =
Mutex.execute logging_disabled_for_m
(fun () -> logging_disabled_for := List.filter (fun x -> x <> brand) !logging_disabled_for)
let is_disabled brand =
Mutex.execute logging_disabled_for_m
(fun () -> List.mem brand !logging_disabled_for)

let disable ?level brand =
let levels = match level with
| None -> all_levels
| Some l -> [l]
in
Mutex.execute logging_disabled_for_m (fun () ->
let disable' brand level = logging_disabled_for := (brand, level) :: !logging_disabled_for in
List.iter (disable' brand) levels
)

let enable ?level brand =
let levels = match level with
| None -> all_levels
| Some l -> [l]
in
Mutex.execute logging_disabled_for_m (fun () ->
logging_disabled_for := List.filter (fun (x, y) -> not (x = brand && List.mem y levels)) !logging_disabled_for
)

let is_disabled brand level =
Mutex.execute logging_disabled_for_m (fun () ->
List.mem (brand, level) !logging_disabled_for
)

let gettimestring () =
let time = Unix.gettimeofday () in
Expand Down Expand Up @@ -146,7 +162,7 @@ module Debugger = functor(Brand: BRAND) -> struct
let output level priority (fmt: ('a, unit, string, 'b) format4) =
Printf.kprintf
(fun s ->
if not(is_disabled Brand.name) then begin
if not(is_disabled Brand.name level) then begin
let msg = make_log_message false Brand.name priority s in

if !print_debug
Expand Down
14 changes: 9 additions & 5 deletions log/debug.mli
Expand Up @@ -44,11 +44,15 @@ val gettimestring : unit -> string
val set_facility : Syslog.facility -> unit
(** Set the syslog facility that will be used by this program. *)

val disable : string -> unit
(** [disable brand] Suppress all log output from the given [brand]. This function is idempotent. *)

val enable : string -> unit
(** [enable brand] Enable all log output from the given [brand]. This function is idempotent. *)
val disable : ?level:Syslog.level -> string -> unit
(** [disable brand] Suppress all log output from the given [brand]. Specifying a [level] disables
* only this log level, otherwise all levels for the given [brand] are disabled.
* This function is idempotent. *)

val enable : ?level:Syslog.level -> string -> unit
(** [enable brand] Enable all log output from the given [brand]. Specifying a [level] enables
* only this log level, otherwise all levels for the given [brand] are enabled.
* This function is idempotent. *)

val log_to_stdout : unit -> unit
(** [log_to_stdout ()] will echo all log output to stdout (not the default) *)
Expand Down

0 comments on commit 09d822c

Please sign in to comment.