Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove getter/setter functions from BatLog, more docs, code cleanup

  • Loading branch information...
commit 2727343e1d898c62b4bc4757f9de1337cc789ec9 1 parent c3383e1
@thelema thelema authored
Showing with 49 additions and 57 deletions.
  1. +17 −29 src/batLog.ml
  2. +32 −28 src/batLog.mli
View
46 src/batLog.ml
@@ -32,16 +32,6 @@ let output = ref stderr
let prefix = ref ""
let flags = ref [`Date; `Time]
-(* TODO: make threadsafe? *)
-let get_output () = !output
-let set_output oc = output := oc
-
-let get_prefix () = !prefix
-let set_prefix p = prefix := p
-
-let get_flags () = !flags
-let set_flags fs = flags := fs
-
let print_flag ?fp t oc = function
| `Date ->
let {Unix.tm_year=y; tm_mon=m; tm_mday=d} = Lazy.force t in
@@ -63,29 +53,31 @@ let write_flags ?fp oc fs =
(* BatPrintf.fprintf !output "%a%s%s\n" (write_flags ?fp) !flags !prefix s *)
let log ?fp s =
- write_flags ?fp !output !flags;
- nwrite !output !prefix;
- nwrite !output s;
- write !output '\n'
+ let oc = !output in (* makes sure all output goes to a single channel when multi-threaded *)
+ write_flags ?fp oc !flags;
+ nwrite oc !prefix;
+ nwrite oc s;
+ write oc '\n'
(* BatPrintf.fprintf !output ("%a%s" ^^ fmt ^^"\n") (write_flags ?fp) !flags !prefix *)
let logf ?fp fmt =
- write_flags ?fp !output !flags;
- nwrite !output !prefix;
- BatPrintf.fprintf !output fmt
+ let oc = !output in
+ write_flags ?fp oc !flags;
+ nwrite oc !prefix;
+ BatPrintf.fprintf oc fmt
(* BatPrintf.kfprintf (fun _ -> exit 1) !output "%a%s%s\n" (write_flags ?fp) !flags !prefix s *)
let fatal ?fp s =
- write_flags ?fp !output !flags;
- nwrite !output !prefix;
- nwrite !output s;
- write !output '\n';
+ let oc = !output in
+ write_flags ?fp oc !flags;
+ nwrite oc !prefix;
+ nwrite oc s;
+ write oc '\n';
exit 1
let fatalf ?fp fmt =
BatPrintf.kfprintf (fun _ -> exit 1) !output ("%a%s" ^^ fmt ^^ "%!")
- (write_flags ?fp) !flags
- !prefix
+ (write_flags ?fp) !flags !prefix
module type Config = sig
type t
@@ -93,8 +85,8 @@ module type Config = sig
val prefix: string
val flags: flag list
end
-module Make (S:Config) = struct
+module Make (S:Config) = struct
let log ?fp s =
write_flags ?fp S.out S.flags;
nwrite S.out S.prefix;
@@ -116,7 +108,6 @@ module Make (S:Config) = struct
let fatalf ?fp fmt =
BatPrintf.kfprintf (fun _ -> exit 1) S.out ("%a%s" ^^ fmt ^^ "\n%!")
(write_flags ?fp) S.flags S.prefix
-
end
let make_logger out prefix flags =
@@ -157,13 +148,10 @@ module type Level_sig = sig
end
module Make_lev(L : Level_sig)(S: Config) = struct
- (* This is threadsafe to get/set, so no setter/getter needed;
+ (* These are threadsafe to get/set, so no setter/getter needed;
publicly accessible *)
let level = ref L.default_level
-
let output = ref S.out
- let get_output () = !output
- let set_output o = output := o
(** Main logging function *)
let log ?fp l m =
View
60 src/batLog.mli
@@ -23,30 +23,36 @@
*)
open BatIO
-(** Get and set the default output channel for simple logging.
- Defaults to stderr *)
-val get_output : unit -> unit output
-val set_output : unit output -> unit
+(** This ref holds the output channel for simple logging. Defaults to
+ [stderr]
-(** Get and set the text printed before each log message. Defaults to
- the empty string. *)
-val get_prefix : unit -> string
-val set_prefix : string -> unit
+ @since 2.0; had getter and setter in 1.x
+*)
+val output : unit output ref
-(** Get and set the output flags. These flags control how the log
- messages are output. The default is [`Date; `Time] and log
- messages are printed as:
+(** This ref holds the text printed before each log message. Defaults to
+ the empty string.
+
+ @since 2.0; had getter and setter in 1.x
+*)
+val prefix : string ref
- 2011/0628 01:23:45: prefixmessage
- *)
type flag = [
-| `Date (** Print the current date as 2011/0628 *)
+| `Date (** Print the current date as 2011-06-28 *)
| `Time (** Print the current time as 01:23:45 *)
-| `Filepos (** Print the file and position of this log command (UNIMPLEMENTED) *)
+| `Filepos (** Print the file and linenum of this log command (UNIMPLEMENTED - needs syntax extension) *)
| `Custom of unit -> string (** Print the results of running the given closure *)
]
-val get_flags : unit -> flag list
-val set_flags : flag list -> unit
+
+(** This ref holds the output flags. These flags control how the log
+ messages are output. The default is [`Date; `Time] and log
+ messages are printed as:
+
+ 2011/0628 01:23:45: prefixmessage
+
+ @since 2.0; had getter and setter in 1.x
+*)
+val flags : flag list ref
(** [log s] logs the message s, returning unit.
@@ -109,14 +115,14 @@ end
*)
val make_logger :
- 'a BatInnerIO.output ->
+ 'a output ->
string ->
[< `Custom of unit -> string | `Date | `Filepos | `Time ] list ->
< fatal : ?fp:string -> string -> 'b;
fatalf : ?fp:string ->
- ('c, 'a BatInnerIO.output, unit, unit, unit, 'd) format6 -> 'c;
+ ('c, 'a output, unit, unit, unit, 'd) format6 -> 'c;
log : ?fp:string -> string -> unit;
- logf : ?fp:string -> ('e, 'a BatInnerIO.output, unit) BatPrintf.t -> 'e >
+ logf : ?fp:string -> ('e, 'a output, unit) format -> 'e >
(** The different verbosity levels supported in the [Easy] logger *)
type easy_lev = [ `trace | `debug | `info | `warn | `error | `fatal | `always ]
@@ -134,10 +140,9 @@ module Easy : sig
enable logging for [`info], [`warn], [`error], [`fatal] and
[`always] levels. *)
val level : easy_lev ref
-
- (** get and set the output channel. *)
- val get_output : unit -> unit BatInnerIO.output
- val set_output : unit BatInnerIO.output -> unit
+ (** Set this ref to the output you want logging messages to go
+ to. Defaults to [stderr]. *)
+ val output : unit output ref
(** [log lev msg] logs the message [msg] if the current logging
level is [lev] or lower. *)
@@ -145,7 +150,7 @@ module Easy : sig
(** As [log], but instead of a string message, a printf format is
allowed with whatever arguments are appropriate. *)
- val logf : ?fp:string -> easy_lev -> ('a, unit output, unit) Pervasives.format -> 'a
+ val logf : ?fp:string -> easy_lev -> ('a, unit output, unit) format -> 'a
end
@@ -166,8 +171,7 @@ end
(** Make your own level-based logger, like [Easy] *)
module Make_lev(L:Level_sig)(S:Config) : sig
val level : L.t ref
- val get_output : unit -> S.t BatInnerIO.output
- val set_output : S.t BatInnerIO.output -> unit
+ val output : S.t output ref
val log : ?fp:string -> L.t -> string -> unit
- val logf : ?fp:string -> L.t -> ('a, S.t output, unit) Pervasives.format -> 'a
+ val logf : ?fp:string -> L.t -> ('a, S.t output, unit) format -> 'a
end
Please sign in to comment.
Something went wrong with that request. Please try again.