mirror of
https://github.com/c-cube/linol.git
synced 2025-12-08 20:25:35 -05:00
43 lines
12 KiB
HTML
43 lines
12 KiB
HTML
<!DOCTYPE html>
|
||
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Logs_lwt (logs.Logs_lwt)</title><meta charset="utf-8"/><link rel="stylesheet" href="../../_odoc-theme/odoc.css"/><meta name="generator" content="odoc 2.4.2"/><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">logs</a> » Logs_lwt</nav><header class="odoc-preamble"><h1>Module <code><span>Logs_lwt</span></code></h1><p><a href="../../lwt/Lwt/index.html"><code>Lwt</code></a> logging.</p><p>The log functions of this module return <code>Lwt</code> threads that proceed only when the log operation is over, as defined by the current <a href="../Logs/index.html#type-reporter"><code>Logs.reporter</code></a>.</p><p>See a <a href="#report_ex" title="report_ex">cooperative reporter example</a>.</p><p><em>v0.7.0 - <a href="https://erratique.ch/software/logs">homepage</a></em></p></header><nav class="odoc-toc"><ul><li><a href="#log-functions">Log functions</a><ul><li><a href="#logging--value-errors">Logging <code>result</code> value <code>Error</code>s</a></li></ul></li><li><a href="#source-specific-log-functions">Source specific log functions</a></li><li><a href="#report_ex">Cooperative reporter example</a></li></ul></nav><div class="odoc-content"><h2 id="log-functions"><a href="#log-functions" class="anchor"></a>Log functions</h2><div class="odoc-spec"><div class="spec type anchored" id="type-log"><a href="#type-log" class="anchor"></a><code><span><span class="keyword">type</span> <span>'a log</span></span><span> = <span><span><span>(<span class="type-var">'a</span>, <span>unit <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span>)</span> <a href="../Logs/index.html#type-msgf">Logs.msgf</a></span> <span class="arrow">-></span></span> <span>unit <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p>The type for Lwt log functions. The returned thread only proceeds once the log operation is over. See <a href="../Logs/index.html#type-log"><code>Logs.log</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-msg"><a href="#val-msg" class="anchor"></a><code><span><span class="keyword">val</span> msg : <span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span><a href="../Logs/index.html#type-level">Logs.level</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-log">log</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-msg"><code>Logs.msg</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-app"><a href="#val-app" class="anchor"></a><code><span><span class="keyword">val</span> app : <span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-log">log</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-app"><code>Logs.app</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-err"><a href="#val-err" class="anchor"></a><code><span><span class="keyword">val</span> err : <span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-log">log</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-err"><code>Logs.err</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-warn"><a href="#val-warn" class="anchor"></a><code><span><span class="keyword">val</span> warn : <span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-log">log</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-warn"><code>Logs.warn</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-info"><a href="#val-info" class="anchor"></a><code><span><span class="keyword">val</span> info : <span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-log">log</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-info"><code>Logs.info</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-debug"><a href="#val-debug" class="anchor"></a><code><span><span class="keyword">val</span> debug : <span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="#type-log">log</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-debug"><code>Logs.debug</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-kmsg"><a href="#val-kmsg" class="anchor"></a><code><span><span class="keyword">val</span> kmsg :
|
||
<span><span>(<span>unit <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span>
|
||
<span><a href="../Logs/index.html#type-level">Logs.level</a> <span class="arrow">-></span></span>
|
||
<span><span><span>(<span class="type-var">'a</span>, <span><span class="type-var">'b</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span>)</span> <a href="../Logs/index.html#type-msgf">Logs.msgf</a></span> <span class="arrow">-></span></span>
|
||
<span><span class="type-var">'b</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-kmsg"><code>Logs.kmsg</code></a>.</p></div></div><h3 id="logging--value-errors"><a href="#logging--value-errors" class="anchor"></a>Logging <code>result</code> value <code>Error</code>s</h3><div class="odoc-spec"><div class="spec value anchored" id="val-on_error"><a href="#val-on_error" class="anchor"></a><code><span><span class="keyword">val</span> on_error :
|
||
<span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?level</span>:<a href="../Logs/index.html#type-level">Logs.level</a> <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?header</span>:string <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?tags</span>:<a href="../Logs/Tag/index.html#type-set">Logs.Tag.set</a> <span class="arrow">-></span></span>
|
||
<span><span class="label">pp</span>:<span>(<span><a href="../../ocaml/Stdlib/Format/index.html#type-formatter">Stdlib.Format.formatter</a> <span class="arrow">-></span></span> <span><span class="type-var">'b</span> <span class="arrow">-></span></span> unit)</span> <span class="arrow">-></span></span>
|
||
<span><span class="label">use</span>:<span>(<span><span class="type-var">'b</span> <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">-></span></span>
|
||
<span><span><span><span>(<span class="type-var">'a</span>, <span class="type-var">'b</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-result">result</a></span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span> <span class="arrow">-></span></span>
|
||
<span><span class="type-var">'a</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-on_error"><code>Logs.on_error</code></a>.</p></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-on_error_msg"><a href="#val-on_error_msg" class="anchor"></a><code><span><span class="keyword">val</span> on_error_msg :
|
||
<span><span class="optlabel">?src</span>:<a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?level</span>:<a href="../Logs/index.html#type-level">Logs.level</a> <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?header</span>:string <span class="arrow">-></span></span>
|
||
<span><span class="optlabel">?tags</span>:<a href="../Logs/Tag/index.html#type-set">Logs.Tag.set</a> <span class="arrow">-></span></span>
|
||
<span><span class="label">use</span>:<span>(<span>unit <span class="arrow">-></span></span> <span><span class="type-var">'a</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span>)</span> <span class="arrow">-></span></span>
|
||
<span><span><span><span>(<span class="type-var">'a</span>, <span>[ <span>`Msg of string</span> ]</span>)</span> <a href="../../ocaml/Stdlib/index.html#type-result">result</a></span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span> <span class="arrow">-></span></span>
|
||
<span><span class="type-var">'a</span> <a href="../../lwt/Lwt/index.html#type-t">Lwt.t</a></span></span></code></div><div class="spec-doc"><p>See <a href="../Logs/index.html#val-on_error_msg"><code>Logs.on_error_msg</code></a>.</p></div></div><h2 id="source-specific-log-functions"><a href="#source-specific-log-functions" class="anchor"></a>Source specific log functions</h2><div class="odoc-spec"><div class="spec module-type anchored" id="module-type-LOG"><a href="#module-type-LOG" class="anchor"></a><code><span><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-LOG/index.html">LOG</a></span><span> = <span class="keyword">sig</span> ... <span class="keyword">end</span></span></code></div></div><div class="odoc-spec"><div class="spec value anchored" id="val-src_log"><a href="#val-src_log" class="anchor"></a><code><span><span class="keyword">val</span> src_log : <span><a href="../Logs/index.html#type-src">Logs.src</a> <span class="arrow">-></span></span> <span>(<span class="keyword">module</span> <a href="module-type-LOG/index.html">LOG</a>)</span></span></code></div><div class="spec-doc"><p><code>src_log src</code> is a <a href="module-type-LOG/index.html" title="LOG">set of logging functions</a> for <code>src</code>.</p></div></div><h2 id="report_ex"><a href="#report_ex" class="anchor"></a>Cooperative reporter example</h2><p>The following reporter will play nice with <code>Lwt</code>'s runtime, it will behave synchronously for the log functions of this module and asynchronously for those of the <a href="../Logs/index.html"><code>Logs</code></a> module (see <a href="../Logs/index.html#sync"><code>sync</code></a>).</p><p>It reuses <a href="../Logs_fmt/index.html#reporter"><code>reporter</code></a> and will produce colorful output if the standard formatters are setup to do so. For example it can be used instead of <a href="../Logs_fmt/index.html#reporter"><code>reporter</code></a> in the <a href="../Logs_cli/index.html#ex" title="ex">full setup example</a>.</p><pre class="language-ocaml"><code>let lwt_reporter () =
|
||
let buf_fmt ~like =
|
||
let b = Buffer.create 512 in
|
||
Fmt.with_buffer ~like b,
|
||
fun () -> let m = Buffer.contents b in Buffer.reset b; m
|
||
in
|
||
let app, app_flush = buf_fmt ~like:Fmt.stdout in
|
||
let dst, dst_flush = buf_fmt ~like:Fmt.stderr in
|
||
let reporter = Logs_fmt.reporter ~app ~dst () in
|
||
let report src level ~over k msgf =
|
||
let k () =
|
||
let write () = match level with
|
||
| Logs.App -> Lwt_io.write Lwt_io.stdout (app_flush ())
|
||
| _ -> Lwt_io.write Lwt_io.stderr (dst_flush ())
|
||
in
|
||
let unblock () = over (); Lwt.return_unit in
|
||
Lwt.finalize write unblock |> Lwt.ignore_result;
|
||
k ()
|
||
in
|
||
reporter.Logs.report src level ~over:(fun () -> ()) k msgf;
|
||
in
|
||
{ Logs.report = report }</code></pre></div></body></html>
|