1. Vincent Fiack
  2. ocaml-toys

Commits

Vincent Fiack  committed d659213

object instead of record.

  • Participants
  • Parent commits baf8522
  • Branches default

Comments (0)

Files changed (2)

File src/log.ml

View file
 type loglevel = DEBUG | INFO | WARN | ERROR | DISABLED
 
-type logger = {
+type logger = <
 	debug: string -> unit;
 	info: string -> unit;
 	warn: string -> unit;
 	error: string -> unit;
-	close: unit -> unit;
-}
+	close: unit;
+>
 
 let string_of_level = function
 	| DEBUG -> "DEBUG"
 		let now = string_of_time (Unix.gettimeofday()) in
 		Printf.fprintf channel "%s [%-5s] %s\n%!" now (string_of_level level) msg
 
-let create_logger channel level = {
-	debug = write channel level DEBUG;
-	info = write channel level INFO;
-	warn = write channel level WARN;
-	error = write channel level ERROR;
-	close = fun () -> close_out channel;
-}
+let create_logger channel level = object (self)
+	method debug = write channel level DEBUG;
+	method info = write channel level INFO;
+	method warn = write channel level WARN;
+	method error = write channel level ERROR;
+	method close = close_out channel;
+end
 
 let create_file_logger path level =
 	let channel = open_out_gen [Open_wronly; Open_append; Open_creat; Open_text] 0666 path
 
 let set_default_logger logger = default_logger := logger
 
-let debug msg = !default_logger.debug msg
-and warn msg = !default_logger.warn msg
-and info msg = !default_logger.info msg
-and error msg = !default_logger.error msg
+let debug msg = !default_logger#debug msg
+and warn msg = !default_logger#warn msg
+and info msg = !default_logger#info msg
+and error msg = !default_logger#error msg

File src/log.mli

View file
 type loglevel = DEBUG | INFO | WARN | ERROR | DISABLED
 
-type logger = {
-	debug : string -> unit;
+type logger = <
+	debug: string -> unit;
 	info: string -> unit;
 	warn: string -> unit;
 	error: string -> unit;
-	close: unit -> unit;
-}
+	close: unit;
+>
 
 val create_logger: out_channel -> loglevel -> logger
 val create_file_logger: string -> loglevel -> logger