Skip to content

Instantly share code, notes, and snippets.

@jtpaasch
Created May 4, 2018 02:12
Show Gist options
  • Select an option

  • Save jtpaasch/248215b73fd73003b9334aff096ce558 to your computer and use it in GitHub Desktop.

Select an option

Save jtpaasch/248215b73fd73003b9334aff096ce558 to your computer and use it in GitHub Desktop.
A simple logging module (OCaml)
module Logs = struct
let hash = Hashtbl.create 256
let log name msg =
let ch = Hashtbl.find hash name in
Printf.fprintf ch "%s\n%!" msg
let close name =
let ch = Hashtbl.find hash name in
close_out ch
let channel target =
match target with
| "stdout" -> stdout
| "stderr" -> stderr
| filename -> open_out_gen [Open_creat; Open_text; Open_append] 0o640 filename
let create name target =
let ch = channel target in
Hashtbl.add hash name ch;
at_exit (fun _ -> close name)
end
let main () =
Logs.create "verbose_log" "verbose.log";
Logs.log "verbose_log" "Logging to verbose log...";
Logs.create "other_verbose_log" "verbose.log";
Logs.log "other_verbose_log" "Logging to other verbose log...";
Logs.create "main_log" "stdout";
Logs.log "main_log" "Logging to stdout...";
Printf.printf "Done\n%!"
let () = Unix.handle_unix_error main ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment