Skip to content

Commit

Permalink
feat log: add Log_ctx.set_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Dec 4, 2024
1 parent bf918e7 commit 7668eb3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/log/log_ctx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ let get_tags_from_ctx () : Logs.Tag.set =
(fun _ (Logs.Tag.V (tag, v)) set -> Logs.Tag.add tag v set)
map Logs.Tag.empty

let set_tag (tag : _ tag) v : unit =
match
LS.get_in_local_hmap_opt ctx_k |> Option.value ~default:Str_map.empty
with
| exception _ -> ()
| old_map ->
let new_map =
Str_map.add (Logs.Tag.name tag) (Logs.Tag.V (tag, v)) old_map
in
LS.set_in_local_hmap ctx_k new_map

let with_tag (tag : _ tag) v (f : unit -> 'b) : 'b =
match
LS.get_in_local_hmap_opt ctx_k |> Option.value ~default:Str_map.empty
Expand Down
3 changes: 3 additions & 0 deletions src/log/log_ctx.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ type 'a tag = 'a Logs.Tag.def

val create_tag : ?doc:string -> string -> 'a Fmt.printer -> 'a tag

val set_tag : 'a tag -> 'a -> unit
(** Set tag for the rest of the current task, does nothing outside of a task *)

val with_tag : 'a tag -> 'a -> (unit -> 'b) -> 'b
(** Set the tag to this value locally in the ambient context *)

Expand Down

0 comments on commit 7668eb3

Please sign in to comment.