mirror of
https://github.com/c-cube/linol.git
synced 2025-12-09 12:45:53 -05:00
38 lines
4.6 KiB
HTML
38 lines
4.6 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Logs_cli (logs.Logs_cli)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 3.1.0"/><meta name="viewport" content="width=device-width,initial-scale=1.0"/><script src="../../highlight.pack.js"></script><script>hljs.initHighlightingOnLoad();</script></head><body class="odoc"><nav class="odoc-nav"><a href="../index.html">Up</a> – <a href="../../index.html">Index</a> » <a href="../index.html">logs</a> » Logs_cli</nav><header class="odoc-preamble"><h1>Module <code><span>Logs_cli</span></code></h1><p><a href="../../cmdliner/Cmdliner/index.html"><code>Cmdliner</code></a> support for <a href="../Logs/index.html"><code>Logs</code></a>.</p><p>See a full <a href="#ex" title="ex">example</a>.</p></header><div class="odoc-tocs"><nav class="odoc-toc odoc-local-toc"><ul><li><a href="#options-for-setting-the-report-level">Options for setting the report level</a></li><li><a href="#ex">Example</a></li></ul></nav></div><div class="odoc-content"><h2 id="options-for-setting-the-report-level"><a href="#options-for-setting-the-report-level" class="anchor"></a>Options for setting the report level</h2><div class="odoc-spec"><div class="spec value anchored" id="val-level"><a href="#val-level" class="anchor"></a><code><span><span class="keyword">val</span> level :
|
||
<span><span class="optlabel">?env</span>:<a href="../../cmdliner/Cmdliner/Cmd/Env/index.html#type-info">Cmdliner.Cmd.Env.info</a> <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?docs</span>:string <span class="arrow">-></span></span>
|
||
<span>unit <span class="arrow">-></span></span>
|
||
<span><span><a href="../Logs/index.html#type-level">Logs.level</a> option</span> <a href="../../cmdliner/Cmdliner/Term/index.html#type-t">Cmdliner.Term.t</a></span></span></code></div><div class="spec-doc"><p><code>level ?env ?docs ()</code> is a term for three <a href="../../cmdliner/Cmdliner/index.html"><code>Cmdliner</code></a> options that can be used with <a href="../Logs/index.html#val-set_level"><code>Logs.set_level</code></a>. The options are documented under <code>docs</code> (defaults to the default of <a href="../../cmdliner/Cmdliner/Arg/index.html#type-info"><code>Cmdliner.Arg.info</code></a>).</p><p>The options work as follows:</p><ul><li><code>-v</code> or <code>--verbose</code>, if it appears once, the value of the term is <code>Some Logs.Info</code> and more than once <code>Some Logs.Debug</code>.</li><li><code>--verbosity=LEVEL</code>, the value of the term is <code>l</code> where <code>l</code> depends on on <code>LEVEL</code>. Takes over the option <code>-v</code>.</li><li><code>-q</code> or <code>--quiet</code>, the value of the term is <code>None</code>. Takes over the <code>-v</code> and <code>--verbosity</code> options.</li><li>If both options are absent the default value is <code>Some Logs.warning</code></li></ul><p>If <code>env</code> is provided, the default value in case all options are absent can be overridden by the corresponding environment variable.</p></div></div><h2 id="ex"><a href="#ex" class="anchor"></a>Example</h2><p>The following example shows how to setup <a href="../Logs/index.html"><code>Logs</code></a> and <a href="../../fmt/Fmt/index.html"><code>Fmt</code></a> so that logging is performed on standard outputs with ANSI coloring if these are <code>tty</code>s. The command line interface provides options to control the use of colors and the log reporting level.</p><pre class="language-ocaml"><code>let hello _ msg =
|
||
Logs.app (fun m -> m "%s" msg);
|
||
Logs.info (fun m -> m "End-user information.");
|
||
Logs.debug (fun m -> m "Developer information.");
|
||
Logs.err (fun m -> m "Something bad happened.");
|
||
Logs.warn (fun m -> m "Something bad may happen in the future.");
|
||
if Logs.err_count () > 0 then 1 else 0
|
||
|
||
let setup_log ~style_renderer ~level =
|
||
Fmt_tty.setup_std_outputs ?style_renderer ();
|
||
Logs.set_level level;
|
||
Logs.set_reporter (Logs_fmt.reporter ())
|
||
|
||
(* Command line interface *)
|
||
|
||
open Cmdliner
|
||
open Cmdliner.Term.Syntax
|
||
|
||
let cmd =
|
||
Cmd.make (Cmd.info "tool") @@
|
||
let env = Cmd.Env.info "TOOL_VERBOSITY" in
|
||
let+ style_renderer = Fmt_cli.style_renderer ()
|
||
and+ level = Logs_cli.level ~env ()
|
||
and+ msg =
|
||
let doc = "The message to output." in
|
||
Arg.(value & pos 0 string "Hello horrible world!" & info [] ~doc)
|
||
in
|
||
setup_log ~style_renderer ~level;
|
||
hello msg
|
||
|
||
|
||
let main () = Cmd.eval' cmd
|
||
let () = if !Sys.interactive then () else exit (main ())</code></pre></div></body></html>
|