From 47630ca9a5693e7eec324112faeb5b1c74b2abd3 Mon Sep 17 00:00:00 2001 From: favonia Date: Sun, 12 Nov 2023 08:57:26 -0600 Subject: [PATCH] feat(Logger): debugging interface --- src/Logger.ml | 22 ++++++++++++++++++++++ src/Logger.mli | 3 +++ src/LoggerSigs.ml | 7 +++++++ 3 files changed, 32 insertions(+) create mode 100644 src/Logger.ml create mode 100644 src/Logger.mli create mode 100644 src/LoggerSigs.ml diff --git a/src/Logger.ml b/src/Logger.ml new file mode 100644 index 0000000..19c93f2 --- /dev/null +++ b/src/Logger.ml @@ -0,0 +1,22 @@ +include LoggerSigs + +type 'a Effect.t += + | Debug : Diagnostic.loctext -> unit Effect.t + | CallBegin : Diagnostic.loctext -> unit Effect.t + | CallEnd : Diagnostic.loctext -> unit Effect.t + +let debug_loctext t = Effect.perform @@ Debug t +let debug ?loc s = debug_loctext @@ Diagnostic.loctext ?loc s +let debugf ?loc = Diagnostic.kloctextf ?loc debug_loctext + +let stalk_open_loctext t = Effect.perform @@ CallBegin t +let stalk_close_loctext t = Effect.perform @@ CallEnd t +let stalk ?loc s f = + stalk_open_loctext (Diagnostic.loctext ?loc s); + Fun.protect f + ~finally:(fun () -> stalk_close_loctext (Diagnostic.loctext ?loc s)) +let stalkf ?loc = + Diagnostic.ktextf @@ fun t f -> + stalk_open_loctext {Range.value = t; loc}; + Fun.protect f + ~finally:(fun () -> stalk_close_loctext {Range.value = t; loc}) diff --git a/src/Logger.mli b/src/Logger.mli new file mode 100644 index 0000000..202099b --- /dev/null +++ b/src/Logger.mli @@ -0,0 +1,3 @@ +include module type of LoggerSigs + +module Make () : S diff --git a/src/LoggerSigs.ml b/src/LoggerSigs.ml new file mode 100644 index 0000000..37ced53 --- /dev/null +++ b/src/LoggerSigs.ml @@ -0,0 +1,7 @@ +module type S = +sig + val debug : ?loc:Range.t -> string -> unit + val debugf : ?loc:Range.t -> ('a, Format.formatter, unit, unit) format4 -> 'a + val stalk : ?loc:Range.t -> string -> (unit -> 'a) -> 'a + val stalkf : ?loc:Range.t -> ('b, Format.formatter, unit, (unit -> 'a) -> 'a) format4 -> 'b +end