From 45e9fac30aa37fb70d7f17e79f11f2aa8e0401f6 Mon Sep 17 00:00:00 2001 From: tali Date: Wed, 31 Jan 2024 16:29:24 -0500 Subject: [PATCH] use ptime library to get rfc3339 timestamps w/ timezone --- lib/logging/dune | 2 +- lib/logging/logging.ml | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/logging/dune b/lib/logging/dune index aa02075..4692419 100644 --- a/lib/logging/dune +++ b/lib/logging/dune @@ -1,4 +1,4 @@ (library (package talircd) (name logging) - (libraries unix)) + (libraries ptime ptime.clock.os)) diff --git a/lib/logging/logging.ml b/lib/logging/logging.ml index 587f464..33e8294 100644 --- a/lib/logging/logging.ml +++ b/lib/logging/logging.ml @@ -7,8 +7,18 @@ type level = 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 = - ts:float -> + ts:Time.t -> ns:string -> lvl:level -> string -> @@ -53,7 +63,7 @@ type 'a log_function = ((('a, Format.formatter, unit) format -> 'a) -> unit) -> unit let write logger lvl msg = - let ts = Unix.gettimeofday () in + let ts = Time.stamp () in let ns = logger.namespace in Option.iter (fun w -> w ~ts ~ns ~lvl msg) logger.writers @@ -147,11 +157,7 @@ module Pretty = struct let ansi_off = "\x1b[0m" let pr_timestamp bp ts = - let ts_ms = int_of_float (ts *. 1000.0) mod 1000 in - 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 + Buffer.add_string bp (Time.to_string ts) let pr_spaces bp n = for _ = 1 to n do