package logs
Logging infrastructure for OCaml
Install
dune-project
Dependency
Authors
Maintainers
Sources
logs-0.9.0.tbz
sha512=b75fb28e83f33461b06b5c9b60972c4a9a9a1599d637b4a0c7b1e86a87f34fe5361e817cb31f42ad7e7cbb822473b28fab9f58a02870eb189ebe88dae8e045ff
doc/src/logs/logs.ml.html
Source file logs.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
(*--------------------------------------------------------------------------- Copyright (c) 2015 The logs programmers. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) let rec atomic_list_cons v atomic = let l = Atomic.get atomic in if Atomic.compare_and_set atomic l (v :: l) then () else atomic_list_cons v atomic (* Reporting levels *) type level = App | Error | Warning | Info | Debug let level' = Atomic.make (Some Warning) let level () = Atomic.get level' let pp_level ppf = function | App -> () | Error -> Format.pp_print_string ppf "ERROR" | Warning -> Format.pp_print_string ppf "WARNING" | Info -> Format.pp_print_string ppf "INFO" | Debug -> Format.pp_print_string ppf "DEBUG" let level_to_string = function | None -> "quiet" | Some App -> "app" | Some Error -> "error" | Some Warning -> "warning" | Some Info -> "info" | Some Debug -> "debug" let level_of_string = function | "quiet" -> Ok None | "app" -> Ok (Some App) | "error" -> Ok (Some Error) | "warning" -> Ok (Some Warning) | "info" -> Ok (Some Info) | "debug" -> Ok (Some Debug) | l -> Error (`Msg (Printf.sprintf "%S: unknown log level" l)) (* Sources *) module Src = struct type t = { uid : int; name : string; doc : string; level : level option Atomic.t } let uid = let id = Atomic.make 0 in fun () -> Atomic.fetch_and_add id 1 let list = Atomic.make [] let create ?(doc = "undocumented") name = let level = Atomic.make (Atomic.get level') in let src = { uid = uid (); name; doc; level } in atomic_list_cons src list; src let name s = s.name let doc s = s.doc let level s = Atomic.get (s.level) let set_level s l = Atomic.set s.level l let equal src0 src1 = src0.uid = src1.uid let compare src0 src1 = (compare : int -> int -> int) src0.uid src1.uid let pp ppf src = Format.fprintf ppf "@[<1>(src@ @[<1>(name %S)@]@ @[<1>(uid %d)@] @[<1>(doc %S)@])@]" src.name src.uid src.doc let list () = Atomic.get list end type src = Src.t let default = Src.create "application" ~doc:"The application log" let set_level ?(all = true) l = Atomic.set level' l; if all then List.iter (fun s -> Src.set_level s l) (Src.list ()) (* Message tags *) module Tag = struct (* Universal type, see http://mlton.org/UniversalType. Note: we can get rid of that once we have OCaml >= 5.1 *) type univ = exn let univ (type s) () = let module M = struct exception E of s option end in (fun x -> M.E (Some x)), (function M.E x -> x | _ -> None) (* Tag definitions *) type 'a def = { uid : int; to_univ : 'a -> univ; of_univ : univ -> 'a option; name : string; doc : string; pp : Format.formatter -> 'a -> unit; } type def_e = Def : 'a def -> def_e let list = Atomic.make ([] : def_e list) let uid = let id = Atomic.make 0 in fun () -> Atomic.fetch_and_add id 1 let def ?(doc = "undocumented") name pp = let to_univ, of_univ = univ () in let tag = { uid = uid (); to_univ; of_univ; name; doc; pp } in atomic_list_cons (Def tag) list; tag let name d = d.name let doc d = d.doc let printer d = d.pp let pp_def ppf d = Format.fprintf ppf "tag:%s" d.name let list () = Atomic.get list (* Tag values *) type t = V : 'a def * 'a -> t let pp ppf (V (d, v)) = Format.fprintf ppf "@[<1>(%a@ @[%a@])@]" pp_def d d.pp v (* Tag sets *) module Key = struct type t = V : 'a def -> t let compare (V k0) (V k1) = (compare : int -> int -> int) k0.uid k1.uid end module M = Map.Make (Key) type set = t M.t let empty = M.empty let is_empty = M.is_empty let mem k s = M.mem (Key.V k) s let add k v s = M.add (Key.V k) (V (k, v)) s let rem k s = M.remove (Key.V k) s let find : type a. a def -> set -> a option = fun k s -> try match M.find (Key.V k) s with | V (k', v) -> k.of_univ (k'.to_univ v) with Not_found -> None let get k s = match find k s with | None -> invalid_arg (Printf.sprintf "tag named %s not found in set" k.name) | Some v -> v let fold f s acc = M.fold (fun _ t acc -> f t acc) s acc let pp_set ppf s = let pp_tag tag is_first = if is_first then () else Format.fprintf ppf "@,"; Format.fprintf ppf "%a" pp tag; false in Format.fprintf ppf "@[<1>{"; ignore (fold pp_tag s true); Format.fprintf ppf "}@]"; () end (* Reporters *) type ('a, 'b) msgf = (?header:string -> ?tags:Tag.set -> ('a, Format.formatter, unit, 'b) format4 -> 'a) -> 'b type reporter_mutex = { lock : unit -> unit; unlock : unit -> unit } let reporter_mutex' = Atomic.make { lock = (fun () -> ()); unlock = (fun () -> ()) } let set_reporter_mutex ~lock ~unlock = Atomic.set reporter_mutex' { lock; unlock } type reporter = { report : 'a 'b. src -> level -> over:(unit -> unit) -> (unit -> 'b) -> ('a, 'b) msgf -> 'b } let nop_reporter = { report = fun _ _ ~over k _ -> over (); k () } let reporter' = Atomic.make nop_reporter let set_reporter r = Atomic.set reporter' r let reporter () = Atomic.get reporter' let report src level ~over k msgf = let mutex = Atomic.get reporter_mutex' in let over () = over (); mutex.unlock () in mutex.lock (); (Atomic.get reporter').report src level ~over k msgf let pp_brackets pp_v ppf v = Format.pp_print_char ppf '['; pp_v ppf v; Format.pp_print_char ppf ']' let pp_header ppf (l, h) = match h with | None -> if l = App then () else pp_brackets pp_level ppf l | Some h -> pp_brackets Format.pp_print_string ppf h let pp_exec_header = let exec = match Array.length Sys.argv with | 0 -> Filename.basename Sys.executable_name | n -> Filename.basename Sys.argv.(0) in fun ppf (l, h) -> if l = App then match h with | None -> () | Some h -> pp_brackets Format.pp_print_string ppf h; Format.pp_print_char ppf ' ' else match h with | None -> Format.pp_print_string ppf exec; Format.pp_print_string ppf ": "; pp_brackets pp_level ppf l; Format.pp_print_char ppf ' ' | Some h -> Format.pp_print_string ppf exec; Format.pp_print_string ppf ": "; pp_brackets Format.pp_print_string ppf h; Format.pp_print_char ppf ' ' let format_reporter ?(pp_header = pp_exec_header) ?(app = Format.std_formatter) ?(dst = Format.err_formatter) () = let report src level ~over k msgf = let k ppf = Format.pp_close_box ppf (); Format.pp_print_newline ppf (); over (); k () in msgf @@ fun ?header ? fmt -> let ppf = if level = App then app else dst in pp_header ppf (level, header); Format.pp_open_box ppf 0; Format.kfprintf k ppf fmt in { report } (* Log functions *) let err_count' = Atomic.make 0 let err_count () = Atomic.get err_count' let incr_err_count () = Atomic.incr err_count' let warn_count' = Atomic.make 0 let warn_count () = Atomic.get warn_count' let incr_warn_count () = Atomic.incr warn_count' type 'a log = ('a, unit) msgf -> unit let over () = () let kmsg k ?(src = default) level msgf = begin match level with | Error -> Atomic.incr err_count' | Warning -> Atomic.incr warn_count' | _ -> () end; match Src.level src with | None -> k () | Some current_level when level > current_level -> k () | Some _ -> report src level ~over k msgf let kunit _ = () let msg ?src level msgf = kmsg kunit ?src level msgf let app ?src msgf = kmsg kunit ?src App msgf let err ?src msgf = kmsg kunit ?src Error msgf let warn ?src msgf = kmsg kunit ?src Warning msgf let info ?src msgf = kmsg kunit ?src Info msgf let debug ?src msgf = kmsg kunit ?src Debug msgf (* Log result errors *) let on_error ?src ?(level = Error) ?header ? ~pp ~use = function | Ok v -> v | Error e -> kmsg (fun () -> use e) ?src level @@ fun m -> m ?header ?tags "@[%a@]" pp e let on_error_msg ?src ?(level = Error) ?header ? ~use = function | Ok v -> v | Error (`Msg msg) -> kmsg use ?src level @@ fun m -> m ?header ?tags "@[%a@]" Format.pp_print_text msg (* Source specific logging functions *) module type LOG = sig val msg : level -> 'a log val app : 'a log val err : 'a log val warn : 'a log val info : 'a log val debug : 'a log val kmsg : (unit -> 'b) -> level -> ('a, 'b) msgf -> 'b val on_error : ?level:level -> ?header:string -> ?tags:Tag.set -> pp:(Format.formatter -> 'b -> unit) -> use:('b -> 'a) -> ('a, 'b) result -> 'a val on_error_msg : ?level:level -> ?header:string -> ?tags:Tag.set -> use:(unit -> 'a) -> ('a, [`Msg of string]) result -> 'a end let src_log src = let module Log = struct let msg level msgf = msg ~src level msgf let kmsg k level msgf = kmsg k ~src level msgf let app msgf = msg App msgf let err msgf = msg Error msgf let warn msgf = msg Warning msgf let info msgf = msg Info msgf let debug msgf = msg Debug msgf let on_error ?level ?header ? ~pp ~use = on_error ~src ?level ?header ?tags ~pp ~use let on_error_msg ?level ?header ? ~use = on_error_msg ~src ?level ?header ?tags ~use end in (module Log : LOG)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>