linol/logs/Logs_cli/index.html
2025-04-03 13:23:17 +00:00

35 lines
4.6 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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.0.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> &#x00BB; <a href="../index.html">logs</a> &#x00BB; 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">&#45;&gt;</span></span>
<span><span class="optlabel">?docs</span>:string <span class="arrow">&#45;&gt;</span></span>
<span>unit <span class="arrow">&#45;&gt;</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 -&gt; m &quot;%s&quot; msg);
Logs.info (fun m -&gt; m &quot;End-user information.&quot;);
Logs.debug (fun m -&gt; m &quot;Developer information.&quot;);
Logs.err (fun m -&gt; m &quot;Something bad happened.&quot;);
Logs.warn (fun m -&gt; m &quot;Something bad may happen in the future.&quot;);
if Logs.err_count () &gt; 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
let setup_log =
let env = Cmd.Env.info &quot;TOOL_VERBOSITY&quot; in
Term.(const setup_log $ Fmt_cli.style_renderer () $ Logs_cli.level ~env ())
let msg =
let doc = &quot;The message to output.&quot; in
Arg.(value &amp; pos 0 string &quot;Hello horrible world!&quot; &amp; info [] ~doc)
let main () =
let cmd = Cmd.make (Cmd.info &quot;tool&quot;) Term.(const hello $ setup_log $ msg) in
Cmd.eval' cmd
let () = if !Sys.interactive then () else exit (main ())</code></pre></div></body></html>