mirror of
https://github.com/edg-l/edlang.git
synced 2024-11-10 01:58:23 +00:00
151 lines
16 KiB
HTML
151 lines
16 KiB
HTML
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Utilities for enriching error handling with `tracing` diagnostic information."><title>tracing_error - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceSerif4-Regular-46f98efaafac5295.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Regular-018c141bf0843ffd.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/FiraSans-Medium-8f9a781e4970d388.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Regular-562dcc5011b6de7d.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../static.files/SourceCodePro-Semibold-d899c5a5c4aeb14a.ttf.woff2"><link rel="stylesheet" href="../static.files/normalize-76eba96aa4d2e634.css"><link rel="stylesheet" href="../static.files/rustdoc-ac92e1bbe349e143.css"><meta name="rustdoc-vars" data-root-path="../" data-static-root-path="../static.files/" data-current-crate="tracing_error" data-themes="" data-resource-suffix="" data-rustdoc-version="1.76.0 (07dca489a 2024-02-04)" data-channel="1.76.0" data-search-js="search-2b6ce74ff89ae146.js" data-settings-js="settings-4313503d2e1961c2.js" ><script src="../static.files/storage-f2adc0d6ca4d09fb.js"></script><script defer src="../crates.js"></script><script defer src="../static.files/main-305769736d49e732.js"></script><noscript><link rel="stylesheet" href="../static.files/noscript-feafe1bb7466e4bd.css"></noscript><link rel="alternate icon" type="image/png" href="../static.files/favicon-16x16-8b506e7a72182f1c.png"><link rel="alternate icon" type="image/png" href="../static.files/favicon-32x32-422f7d1d52889060.png"><link rel="icon" type="image/svg+xml" href="../static.files/favicon-2c020d218678b618.svg"></head><body class="rustdoc mod crate"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="logo-container" href="../tracing_error/index.html"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt=""></a></nav><nav class="sidebar"><div class="sidebar-crate"><a class="logo-container" href="../tracing_error/index.html"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt="logo"></a><h2><a href="../tracing_error/index.html">tracing_error</a><span class="version">0.2.0</span></h2></div><div class="sidebar-elems"><ul class="block">
|
||
<li><a id="all-types" href="all.html">All Items</a></li></ul><section><ul class="block"><li><a href="#modules">Modules</a></li><li><a href="#structs">Structs</a></li><li><a href="#traits">Traits</a></li></ul></section></div></nav><div class="sidebar-resizer"></div>
|
||
<main><div class="width-limiter"><nav class="sub"><form class="search-form"><span></span><div id="sidebar-button" tabindex="-1"><a href="../tracing_error/all.html" title="show sidebar"></a></div><input class="search-input" name="search" aria-label="Run search in the documentation" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" tabindex="-1"><a href="../help.html" title="help">?</a></div><div id="settings-menu" tabindex="-1"><a href="../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../static.files/wheel-7b819b6101059cd0.svg"></a></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1>Crate <a class="mod" href="#">tracing_error</a><button id="copy-path" title="Copy item path to clipboard"><img src="../static.files/clipboard-7571035ce49a181d.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="src" href="../src/tracing_error/lib.rs.html#1-236">source</a> · <button id="toggle-all-docs" title="collapse all docs">[<span>−</span>]</button></span></div><details class="toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Utilities for enriching error handling with <a href="https://docs.rs/tracing"><code>tracing</code></a> diagnostic
|
||
information.</p>
|
||
<h2 id="overview"><a href="#overview">Overview</a></h2>
|
||
<p><a href="https://docs.rs/tracing"><code>tracing</code></a> is a framework for instrumenting Rust programs to collect
|
||
scoped, structured, and async-aware diagnostics. This crate provides
|
||
integrations between <a href="https://docs.rs/tracing"><code>tracing</code></a> instrumentation and Rust error handling. It
|
||
enables enriching error types with diagnostic information from <code>tracing</code>
|
||
<a href="https://docs.rs/tracing/latest/tracing/span/index.html">span</a> contexts, formatting those contexts when errors are displayed, and
|
||
automatically generate <code>tracing</code> <a href="https://docs.rs/tracing/latest/tracing/struct.Event.html">events</a> when errors occur.</p>
|
||
<p>The crate provides the following:</p>
|
||
<ul>
|
||
<li>
|
||
<p><a href="struct.SpanTrace.html"><code>SpanTrace</code></a>, a captured trace of the current <code>tracing</code> <a href="https://docs.rs/tracing/latest/tracing/span/index.html">span</a> context</p>
|
||
</li>
|
||
<li>
|
||
<p><a href="struct.ErrorLayer.html"><code>ErrorLayer</code></a>, a <a href="https://docs.rs/tracing-subscriber/latest/tracing_subscriber/layer/trait.Layer.html">subscriber layer</a> which enables capturing <code>SpanTrace</code>s</p>
|
||
</li>
|
||
</ul>
|
||
<p><strong>Note</strong>: This crate is currently experimental.</p>
|
||
<p><em>Compiler support: <a href="#supported-rust-versions">requires <code>rustc</code> 1.42+</a></em></p>
|
||
<h3 id="feature-flags"><a href="#feature-flags">Feature Flags</a></h3>
|
||
<ul>
|
||
<li><code>traced-error</code> - Enables the <a href="struct.TracedError.html"><code>TracedError</code></a> type and related Traits
|
||
<ul>
|
||
<li><a href="trait.InstrumentResult.html"><code>InstrumentResult</code></a> and <a href="trait.InstrumentError.html"><code>InstrumentError</code></a> extension traits, which
|
||
provide an <a href="trait.InstrumentResult.html#tymethod.in_current_span"><code>in_current_span()</code></a> method for bundling errors with a
|
||
<a href="struct.SpanTrace.html"><code>SpanTrace</code></a>.</li>
|
||
<li><a href="trait.ExtractSpanTrace.html"><code>ExtractSpanTrace</code></a> extension trait, for extracting <code>SpanTrace</code>s from
|
||
behind <code>dyn Error</code> trait objects.</li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
<h3 id="usage"><a href="#usage">Usage</a></h3>
|
||
<p><code>tracing-error</code> provides the <a href="struct.SpanTrace.html"><code>SpanTrace</code></a> type, which captures the current
|
||
<code>tracing</code> span context when it is constructed and allows it to be displayed
|
||
at a later time.</p>
|
||
<p>For example:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::{fmt, error::Error};
|
||
<span class="kw">use </span>tracing_error::SpanTrace;
|
||
|
||
<span class="attr">#[derive(Debug)]
|
||
</span><span class="kw">pub struct </span>MyError {
|
||
context: SpanTrace,
|
||
<span class="comment">// ...
|
||
</span>}
|
||
|
||
<span class="kw">impl </span>fmt::Display <span class="kw">for </span>MyError {
|
||
<span class="kw">fn </span>fmt(<span class="kw-2">&</span><span class="self">self</span>, f: <span class="kw-2">&mut </span>fmt::Formatter<<span class="lifetime">'_</span>>) -> fmt::Result {
|
||
<span class="comment">// ... format other parts of the error ...
|
||
|
||
</span><span class="self">self</span>.context.fmt(f)<span class="question-mark">?</span>;
|
||
|
||
<span class="comment">// ... format other error context information, cause chain, etc ...
|
||
</span>}
|
||
}
|
||
|
||
<span class="kw">impl </span>Error <span class="kw">for </span>MyError {}
|
||
|
||
<span class="kw">impl </span>MyError {
|
||
<span class="kw">pub fn </span>new() -> <span class="self">Self </span>{
|
||
<span class="self">Self </span>{
|
||
context: SpanTrace::capture(),
|
||
<span class="comment">// ... other error information ...
|
||
</span>}
|
||
}
|
||
}</code></pre></div>
|
||
<p>This crate also provides <a href="struct.TracedError.html"><code>TracedError</code></a>, for attaching a <a href="struct.SpanTrace.html"><code>SpanTrace</code></a> to
|
||
an existing error. The easiest way to wrap errors in <code>TracedError</code> is to
|
||
either use the <a href="trait.InstrumentResult.html"><code>InstrumentResult</code></a> and <a href="trait.InstrumentError.html"><code>InstrumentError</code></a> traits or the
|
||
<code>From</code>/<code>Into</code> traits.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tracing_error::prelude::<span class="kw-2">*</span>;
|
||
|
||
std::fs::read_to_string(<span class="string">"myfile.txt"</span>).in_current_span()<span class="question-mark">?</span>;</code></pre></div>
|
||
<p>Once an error has been wrapped with with a <a href="struct.TracedError.html"><code>TracedError</code></a> the <a href="struct.SpanTrace.html"><code>SpanTrace</code></a>
|
||
can be extracted one of 3 ways: either via <a href="struct.TracedError.html"><code>TracedError</code></a>’s
|
||
<code>Display</code>/<code>Debug</code> implementations, or via the <a href="trait.ExtractSpanTrace.html"><code>ExtractSpanTrace</code></a> trait.</p>
|
||
<p>For example, here is how one might print the errors but specialize the
|
||
printing when the error is a placeholder for a wrapping <a href="struct.SpanTrace.html"><code>SpanTrace</code></a>:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::error::Error;
|
||
<span class="kw">use </span>tracing_error::ExtractSpanTrace <span class="kw">as _</span>;
|
||
|
||
<span class="kw">fn </span>print_extracted_spantraces(error: <span class="kw-2">&</span>(<span class="kw">dyn </span>Error + <span class="lifetime">'static</span>)) {
|
||
<span class="kw">let </span><span class="kw-2">mut </span>error = <span class="prelude-val">Some</span>(error);
|
||
<span class="kw">let </span><span class="kw-2">mut </span>ind = <span class="number">0</span>;
|
||
|
||
<span class="macro">eprintln!</span>(<span class="string">"Error:"</span>);
|
||
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(err) = error {
|
||
<span class="kw">if let </span><span class="prelude-val">Some</span>(spantrace) = err.span_trace() {
|
||
<span class="macro">eprintln!</span>(<span class="string">"found a spantrace:\n{}"</span>, spantrace);
|
||
} <span class="kw">else </span>{
|
||
<span class="macro">eprintln!</span>(<span class="string">"{:>4}: {}"</span>, ind, err);
|
||
}
|
||
|
||
error = err.source();
|
||
ind += <span class="number">1</span>;
|
||
}
|
||
}
|
||
</code></pre></div>
|
||
<p>Whereas here, we can still display the content of the <code>SpanTraces</code> without
|
||
any special casing by simply printing all errors in our error chain.</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>std::error::Error;
|
||
|
||
<span class="kw">fn </span>print_naive_spantraces(error: <span class="kw-2">&</span>(<span class="kw">dyn </span>Error + <span class="lifetime">'static</span>)) {
|
||
<span class="kw">let </span><span class="kw-2">mut </span>error = <span class="prelude-val">Some</span>(error);
|
||
<span class="kw">let </span><span class="kw-2">mut </span>ind = <span class="number">0</span>;
|
||
|
||
<span class="macro">eprintln!</span>(<span class="string">"Error:"</span>);
|
||
|
||
<span class="kw">while let </span><span class="prelude-val">Some</span>(err) = error {
|
||
<span class="macro">eprintln!</span>(<span class="string">"{:>4}: {}"</span>, ind, err);
|
||
error = err.source();
|
||
ind += <span class="number">1</span>;
|
||
}
|
||
}</code></pre></div>
|
||
<p>Applications that wish to use <code>tracing-error</code>-enabled errors should
|
||
construct an <a href="struct.ErrorLayer.html"><code>ErrorLayer</code></a> and add it to their <a href="https://docs.rs/tracing/latest/tracing/trait.Subscriber.html"><code>Subscriber</code></a> in order to
|
||
enable capturing <a href="struct.SpanTrace.html"><code>SpanTrace</code></a>s. For example:</p>
|
||
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tracing_error::ErrorLayer;
|
||
<span class="kw">use </span>tracing_subscriber::prelude::<span class="kw-2">*</span>;
|
||
|
||
<span class="kw">fn </span>main() {
|
||
<span class="kw">let </span>subscriber = tracing_subscriber::Registry::default()
|
||
<span class="comment">// any number of other subscriber layers may be added before or
|
||
// after the `ErrorLayer`...
|
||
</span>.with(ErrorLayer::default());
|
||
|
||
<span class="comment">// set the subscriber as the default for the application
|
||
</span>tracing::subscriber::set_global_default(subscriber);
|
||
}</code></pre></div>
|
||
<h3 id="supported-rust-versions"><a href="#supported-rust-versions">Supported Rust Versions</a></h3>
|
||
<p>Tracing is built against the latest stable release. The minimum supported
|
||
version is 1.42. The current Tracing version is not guaranteed to build on
|
||
Rust versions earlier than the minimum supported version.</p>
|
||
<p>Tracing follows the same compiler support policies as the rest of the Tokio
|
||
project. The current stable Rust compiler and the three most recent minor
|
||
versions before it will always be supported. For example, if the current
|
||
stable compiler version is 1.45, the minimum supported version will not be
|
||
increased past 1.42, three minor versions prior. Increasing the minimum
|
||
supported compiler version is not considered a semver breaking change as
|
||
long as doing so complies with this policy.</p>
|
||
</div></details><h2 id="modules" class="section-header"><a href="#modules">Modules</a></h2><ul class="item-table"><li><div class="item-name"><a class="mod" href="prelude/index.html" title="mod tracing_error::prelude">prelude</a></div><div class="desc docblock-short">The <code>tracing-error</code> prelude.</div></li></ul><h2 id="structs" class="section-header"><a href="#structs">Structs</a></h2><ul class="item-table"><li><div class="item-name"><a class="struct" href="struct.ErrorLayer.html" title="struct tracing_error::ErrorLayer">ErrorLayer</a></div><div class="desc docblock-short">A subscriber <a href="https://docs.rs/tracing-subscriber/0.3/tracing_subscriber/layer/trait.Layer.html"><code>Layer</code></a> that enables capturing <a href="../struct.SpanTrace.html"><code>SpanTrace</code></a>s.</div></li><li><div class="item-name"><a class="struct" href="struct.SpanTrace.html" title="struct tracing_error::SpanTrace">SpanTrace</a></div><div class="desc docblock-short">A captured trace of <a href="https://docs.rs/tracing"><code>tracing</code></a> spans.</div></li><li><div class="item-name"><a class="struct" href="struct.SpanTraceStatus.html" title="struct tracing_error::SpanTraceStatus">SpanTraceStatus</a></div><div class="desc docblock-short">The current status of a SpanTrace, indicating whether it was captured or
|
||
whether it is empty for some other reason.</div></li><li><div class="item-name"><a class="struct" href="struct.TracedError.html" title="struct tracing_error::TracedError">TracedError</a></div><div class="desc docblock-short">A wrapper type for <code>Error</code>s that bundles a <code>SpanTrace</code> with an inner <code>Error</code>
|
||
type.</div></li></ul><h2 id="traits" class="section-header"><a href="#traits">Traits</a></h2><ul class="item-table"><li><div class="item-name"><a class="trait" href="trait.ExtractSpanTrace.html" title="trait tracing_error::ExtractSpanTrace">ExtractSpanTrace</a></div><div class="desc docblock-short">A trait for extracting SpanTraces created by <code>in_current_span()</code> from <code>dyn Error</code> trait objects</div></li><li><div class="item-name"><a class="trait" href="trait.InstrumentError.html" title="trait tracing_error::InstrumentError">InstrumentError</a></div><div class="desc docblock-short">Extension trait for instrumenting errors with <code>SpanTrace</code>s</div></li><li><div class="item-name"><a class="trait" href="trait.InstrumentResult.html" title="trait tracing_error::InstrumentResult">InstrumentResult</a></div><div class="desc docblock-short">Extension trait for instrumenting errors in <code>Result</code>s with <code>SpanTrace</code>s</div></li></ul></section></div></main></body></html> |