[logging] use dummy logger when log_events flag is off

Reviewed By: mbouaziz

Differential Revision: D6980622

fbshipit-source-id: 6748554
master
Sam Blackshear 7 years ago committed by Facebook Github Bot
parent f191819642
commit 72a49962f0

@ -30,11 +30,10 @@ module IO = struct
let prepare () = let prepare () =
if Config.log_events then ( close () ;
close () ; let fname = events_dir ^/ (Unix.getpid () |> Pid.to_string) ^ log_file_extension in
let fname = events_dir ^/ (Unix.getpid () |> Pid.to_string) ^ log_file_extension in let oc = Pervasives.open_out_gen [Open_append; Open_creat] 0o666 fname in
let oc = Pervasives.open_out_gen [Open_append; Open_creat] 0o666 fname in out_chan := Some oc
out_chan := Some oc )
let write fmt = let write fmt =
@ -72,8 +71,6 @@ end = struct
new_id new_id
end end
let get_log_identifier () = Random_id.get ()
let bind_default opt map_func prev = match opt with Some x -> map_func x prev | None -> prev let bind_default opt map_func prev = match opt with Some x -> map_func x prev | None -> prev
type frontend_exception = type frontend_exception =
@ -188,36 +185,64 @@ let sysname =
with _ -> "Unknown" with _ -> "Unknown"
let create_row event = module type S = sig
incr sequence_ctr ; val get_log_identifier : unit -> string
let open JsonBuilder in
let base = val prepare : unit -> unit
empty |> add_string ~key:"command" ~data:(InferCommand.to_string Config.command)
|> add_string ~key:"event_tag" ~data:(string_of_event event) val log : event -> unit
|> add_string ~key:"hostname" ~data:(Unix.gethostname ())
|> add_string ~key:"infer_commit" ~data:Version.commit val dump : unit -> unit
|> add_int ~key:"is_originator" ~data:(if CLOpt.is_originator then 1 else 0) end
|> add_int ~key:"pid" ~data:(pid ())
|> add_string ~key:"run_identifier" ~data:(get_log_identifier ()) module LoggerImpl : S = struct
|> add_int ~key:"sequence" ~data:(!sequence_ctr - 1) |> add_string ~key:"sysname" ~data:sysname let get_log_identifier () = Random_id.get ()
|> add_int ~key:"time" ~data:(int_of_float (Unix.time ()))
in let create_row event =
( match event with incr sequence_ctr ;
| UncaughtException (exn, exitcode) -> let open JsonBuilder in
base |> add_string ~key:"exception" ~data:(Caml.Printexc.exn_slot_name exn) let base =
|> add_string ~key:"exception_info" ~data:(Exn.to_string exn) empty |> add_string ~key:"command" ~data:(InferCommand.to_string Config.command)
|> add_int ~key:"exitcode" ~data:exitcode |> add_string ~key:"event_tag" ~data:(string_of_event event)
| FrontendException record -> |> add_string ~key:"hostname" ~data:(Unix.gethostname ())
create_frontend_exception_row base record |> add_string ~key:"infer_commit" ~data:Version.commit
| ProceduresTranslatedSummary record -> |> add_int ~key:"is_originator" ~data:(if CLOpt.is_originator then 1 else 0)
create_procedures_translated_row base record |> add_int ~key:"pid" ~data:(pid ())
| AnalysisStats record -> |> add_string ~key:"run_identifier" ~data:(get_log_identifier ())
create_analysis_stats_row base record ) |> add_int ~key:"sequence" ~data:(!sequence_ctr - 1)
|> JsonBuilder.to_json |> add_string ~key:"sysname" ~data:sysname
|> add_int ~key:"time" ~data:(int_of_float (Unix.time ()))
in
let prepare = IO.prepare ( match event with
| UncaughtException (exn, exitcode) ->
let log event = IO.write "%s\n" (create_row event) base |> add_string ~key:"exception" ~data:(Caml.Printexc.exn_slot_name exn)
|> add_string ~key:"exception_info" ~data:(Exn.to_string exn)
let dump = IO.dump |> add_int ~key:"exitcode" ~data:exitcode
| FrontendException record ->
create_frontend_exception_row base record
| ProceduresTranslatedSummary record ->
create_procedures_translated_row base record
| AnalysisStats record ->
create_analysis_stats_row base record )
|> JsonBuilder.to_json
let prepare = IO.prepare
let log event = IO.write "%s\n" (create_row event)
let dump = IO.dump
end
module DummyLogger : S = struct
let get_log_identifier () = ""
let prepare () = ()
let log _ = ()
let dump _ = ()
end
(* use real logger if logging is enabled, dummy logger otherwise *)
include ( val if Config.log_events then (module LoggerImpl : S) else (module DummyLogger : S) )

Loading…
Cancel
Save