From ba6e70565a55babb4c01b58a08291916b05d90f0 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 | 25 +++++++++++++++++++++++++ src/Logger.mli | 3 +++ src/LoggerSigs.ml | 7 +++++++ 3 files changed, 35 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..fd1ddb5 --- /dev/null +++ b/src/Logger.ml @@ -0,0 +1,25 @@ +include LoggerSigs + +module Make () = struct + type 'a Effect.t += + | Debug : Diagnostic.loctext -> unit Effect.t + | CallBegin : Diagnostic.loctext -> unit Effect.t + | CallEnd : Diagnostic.loctext -> unit Effect.t + + let emit_loctext t = Effect.perform @@ Debug t + let emit ?loc s = emit_loctext @@ Diagnostic.loctext ?loc s + let emitf ?loc = Diagnostic.kloctextf ?loc emit_loctext + + let trace_open_loctext t = Effect.perform @@ CallBegin t + let trace_close_loctext t = Effect.perform @@ CallEnd t + + let trace ?loc s f = + trace_open_loctext (Diagnostic.loctext ?loc s); + Fun.protect f + ~finally:(fun () -> trace_close_loctext (Diagnostic.loctext ?loc s)) + let tracef ?loc = + Diagnostic.ktextf @@ fun t f -> + trace_open_loctext {Range.value = t; loc}; + Fun.protect f + ~finally:(fun () -> trace_close_loctext {Range.value = t; loc}) +end 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..f0d97fa --- /dev/null +++ b/src/LoggerSigs.ml @@ -0,0 +1,7 @@ +module type S = +sig + val emit : ?loc:Range.t -> string -> unit + val emitf : ?loc:Range.t -> ('a, Format.formatter, unit, unit) format4 -> 'a + val trace : ?loc:Range.t -> string -> (unit -> 'a) -> 'a + val tracef : ?loc:Range.t -> ('b, Format.formatter, unit, (unit -> 'a) -> 'a) format4 -> 'b +end