-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow overriding the traceln function
- Loading branch information
Showing
11 changed files
with
155 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
type traceln = { | ||
traceln : 'a. ?__POS__:string * int * int * int -> ('a, Format.formatter, unit, unit) format4 -> 'a; | ||
} [@@unboxed] | ||
|
||
let traceln_key : traceln Fiber.key = Fiber.create_key () | ||
|
||
let traceln_mutex = Mutex.create () | ||
|
||
let default_traceln ?__POS__:pos fmt = | ||
let k go = | ||
let b = Buffer.create 512 in | ||
let f = Format.formatter_of_buffer b in | ||
go f; | ||
Option.iter (fun (file, lnum, _, _) -> Format.fprintf f " [%s:%d]" file lnum) pos; | ||
Format.pp_close_box f (); | ||
Format.pp_print_flush f (); | ||
let msg = Buffer.contents b in | ||
Ctf.label msg; | ||
let lines = String.split_on_char '\n' msg in | ||
Mutex.lock traceln_mutex; | ||
Fun.protect ~finally:(fun () -> Mutex.unlock traceln_mutex) @@ fun () -> | ||
List.iter (Printf.eprintf "+%s\n") lines; | ||
flush stderr | ||
in | ||
Format.kdprintf k ("@[" ^^ fmt) | ||
|
||
let traceln ?__POS__ fmt = | ||
let traceln = | ||
match Fiber.get traceln_key with | ||
| Some { traceln } -> traceln | ||
| None | ||
| exception Unhandled -> default_traceln | ||
in | ||
traceln ?__POS__ fmt | ||
|
||
type t = < | ||
traceln : traceln Fiber.key; | ||
> | ||
|
||
let v = object | ||
method traceln = traceln_key | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Setting up the environment | ||
|
||
```ocaml | ||
# #require "eio_main";; | ||
# open Eio.Std;; | ||
``` | ||
|
||
## Overriding tracing | ||
|
||
```ocaml | ||
# Eio_main.run @@ fun env -> | ||
let debug = Eio.Stdenv.debug env in | ||
let my_traceln = { | ||
Eio.Debug.traceln = fun ?__POS__:_ fmt -> Fmt.epr ("++" ^^ fmt ^^ "@.") | ||
} in | ||
Fiber.both | ||
(fun () -> | ||
Fiber.with_binding debug#traceln my_traceln @@ fun () -> | ||
Fiber.both | ||
(fun () -> traceln "a") | ||
(fun () -> Fiber.yield (); traceln "b") | ||
) | ||
(fun () -> traceln "c");; | ||
++a | ||
+c | ||
++b | ||
- : unit = () | ||
``` |