package MlFront_Logs

  1. Overview
  2. Docs

Source file Optslog.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
(* Similar to https://github.com/dbuenzli/logs/blob/master/src/logs_cli.ml
   but with a (IMHO: saner) default of INFO and conventionally named
   "--log-level" options. *)

open Cmdliner

let strf = Format.asprintf

(** [level_arg level] is the command line argument [ARG] of [--log-level ARG]
    that matches the log level [level]. *)
let level_arg = function
  | Logs.App | Error -> "ERROR"
  | Warning -> "WARNING"
  | Info -> "INFO"
  | Debug -> "DEBUG"

let rec last_of_list hd tl =
  match tl with [] -> hd | hd' :: tl' -> last_of_list hd' tl'

(** [level ?env ?docs ?debug_opt ?short_opts ()] is the cmdliner {!Cmdliner.Term.t}
    for the logging options.

    By default the ["--log-level"] and ["--quiet"] options are added.

    Provide the [env] argument if there should be an environment variable
    for the ["--log-level"] option.
    
    Use the flag [~debug_opt:()] to add a ["--debug"] option that will
    enable debug logs.

    Use the flag [~short_opts:()] to add ["-l"] and ["-q"] options that
    are aliases of the ["--log-level"] and ["--quiet"] options, respectively.
    *)
let level ?env ?docs ?debug_opt ?short_opts () =
  let debug_t =
    match debug_opt with
    | None -> Term.const false
    | Some () ->
        let doc =
          Printf.sprintf "Enable debug logs. Takes precedence over %s."
            (match short_opts with
            | None -> "the $(b,--log-level) option"
            | Some () -> "$(b,-l) and $(b,--log-level) options")
        in
        Arg.(value & flag & info [ "debug" ] ~doc ?docs)
  in
  let log_level_t =
    let enum =
      [
        (* The first entry is the default. Aka it is a hack for the option's absent rendering *)
        ("INFO", None);
        (* ("QUIET", Some None); *)
        ("ERROR", Some (Some Logs.Error));
        ("WARNING", Some (Some Logs.Warning));
        ("INFO", Some (Some Logs.Info));
        ("DEBUG", Some (Some Logs.Debug));
      ]
    in
    let log_level = Arg.enum enum in
    let enum_alts = Arg.doc_alts_enum List.(tl enum) in
    let doc =
      strf
        "Set the verbosity of log messages. If the option is repeated the last \
         $(docv) is used. $(docv) must be %s."
        enum_alts
    in
    let names =
      match short_opts with
      | None -> [ "log-level" ]
      | Some () -> [ "l"; "log-level" ]
    in
    let t =
      Arg.(
        value & opt_all log_level [] & info ?env ~docv:"LEVEL" ~doc ?docs names)
    in
    Term.(
      const (fun (lst : Logs.level option option list) ->
          match lst with
          | [] -> None
          | hd :: tl ->
          match last_of_list hd tl with
          | (loo : Logs.level option option) -> Option.join loo)
      $ t)
  in
  let quiet_t =
    let doc =
      Printf.sprintf "Be quiet. Takes precedence over %s."
        (match (debug_opt, short_opts) with
        | None, None -> "the $(b,--log-level) option"
        | None, Some () -> "$(b,-l) and $(b,--log-level) options"
        | Some (), None -> "$(b,--debug) and $(b,--log-level) options"
        | Some (), Some () ->
            "$(b,--debug), $(b,-l) and $(b,--log-level) options")
    in
    let names =
      match short_opts with None -> [ "quiet" ] | Some () -> [ "q"; "quiet" ]
    in
    Arg.(value & flag & info ~doc ?docs names)
  in
  let choose quiet log_level debug =
    if quiet then None
    else if debug then Some Logs.Debug
    else
      match log_level with
      | Some log_level -> Some log_level
      | None -> Some Logs.Info
  in
  Term.(const choose $ quiet_t $ log_level_t $ debug_t)