Skip to content

Commit

Permalink
test: register a printer for traces
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Sep 24, 2023
1 parent cd906ff commit c63c618
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Logger.ml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ struct
~fatal:(fun d -> fatal_diagnostic (m d))

let register_printer f =
Traces.register_printer (fun `Read -> f `Trace);
Printexc.register_printer @@ function
| Effect.Unhandled (Emit diag) -> f (`Emit diag)
| Fatal diag -> f (`Fatal diag)
Expand Down
3 changes: 2 additions & 1 deletion src/LoggerSigs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,11 @@ sig
@param fatal The interceptor of fatal diagnostics. The default value is {!val:fatal_diagnostic}. *)
val try_with : ?emit:(Code.t Diagnostic.t -> unit) -> ?fatal:(Code.t Diagnostic.t -> 'a) -> (unit -> 'a) -> 'a

val register_printer : ([ `Emit of Code.t Diagnostic.t | `Fatal of Code.t Diagnostic.t ] -> string option) -> unit
val register_printer : ([ `Trace | `Emit of Code.t Diagnostic.t | `Fatal of Code.t Diagnostic.t ] -> string option) -> unit
(** [register_printer p] registers a printer [p] via {!val:Printexc.register_printer} to convert unhandled internal effects and exceptions into strings for the OCaml runtime system to display. Ideally, all internal effects and exceptions should have been handled by {!val:run} and there is no need to use this function, but when it is not the case, this function can be helpful for debugging. The functor {!module:Logger.Make} always registers a simple printer to suggest using {!val:run}, but you can register new ones to override it. The return type of the printer [p] should return [Some s] where [s] is the resulting string, or [None] if it chooses not to convert a particular effect or exception. The registered printers are tried in reverse order until one of them returns [Some s] for some [s]; that is, the last registered printer is tried first. Note that this function is a wrapper of {!val:Printexc.register_printer} and all the registered printers (via this function or {!val:Printexc.register_printer}) are put into the same list.

The input type of the printer [p] is a variant representation of all internal effects and exceptions used in this module:
- [`Trace] corresponds to the effect triggered by {!val:trace}; and
- [`Emit diag] corresponds to the effect triggered by {!val:emit}; and
- [`Fatal diag] corresponds to the exception triggered by {!val:fatal}.

Expand Down

0 comments on commit c63c618

Please sign in to comment.