use ptime library to get rfc3339 timestamps w/ timezone

This commit is contained in:
tali 2024-01-31 16:29:24 -05:00
parent 484941c3ce
commit 45e9fac30a
2 changed files with 14 additions and 8 deletions

View File

@ -1,4 +1,4 @@
(library (library
(package talircd) (package talircd)
(name logging) (name logging)
(libraries unix)) (libraries ptime ptime.clock.os))

View File

@ -7,8 +7,18 @@ type level =
external int_of_level : level -> int = "%identity" external int_of_level : level -> int = "%identity"
module Time = struct
(* TODO: abstract this? *)
type t = Ptime.t
let stamp = Ptime_clock.now
let pp = Ptime.pp_rfc3339 ()
?tz_offset_s:(Ptime_clock.current_tz_offset_s ())
let to_string x =
Format.asprintf "%a" pp x
end
type writer = type writer =
ts:float -> ts:Time.t ->
ns:string -> ns:string ->
lvl:level -> lvl:level ->
string -> string ->
@ -53,7 +63,7 @@ type 'a log_function =
((('a, Format.formatter, unit) format -> 'a) -> unit) -> unit ((('a, Format.formatter, unit) format -> 'a) -> unit) -> unit
let write logger lvl msg = let write logger lvl msg =
let ts = Unix.gettimeofday () in let ts = Time.stamp () in
let ns = logger.namespace in let ns = logger.namespace in
Option.iter (fun w -> w ~ts ~ns ~lvl msg) logger.writers Option.iter (fun w -> w ~ts ~ns ~lvl msg) logger.writers
@ -147,11 +157,7 @@ module Pretty = struct
let ansi_off = "\x1b[0m" let ansi_off = "\x1b[0m"
let pr_timestamp bp ts = let pr_timestamp bp ts =
let ts_ms = int_of_float (ts *. 1000.0) mod 1000 in Buffer.add_string bp (Time.to_string ts)
let tm = Unix.localtime ts in
Printf.bprintf bp "%04d-%02d-%02d %02d:%02d:%02d.%03d"
(tm.tm_year + 1900) (tm.tm_mon + 1) tm.tm_mday
tm.tm_hour tm.tm_min tm.tm_sec ts_ms
let pr_spaces bp n = let pr_spaces bp n =
for _ = 1 to n do for _ = 1 to n do