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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Thread-local context management for hierarchical, task-based logging.
//!
//! This module provides the core context system that enables logwise to track tasks,
//! manage hierarchical logging contexts, and collect performance statistics. Contexts
//! are thread-local and form a parent-child hierarchy that allows for structured logging
//! with automatic indentation and task tracking.
//!
//! # Overview
//!
//! The context system consists of three main components:
//!
//! - [`Context`]: A hierarchical logging context that can represent either a new task or
//! inherit from a parent context
//! - [`Task`]: Represents a logical unit of work with performance tracking capabilities
//! - [`ApplyContext`]: A [`Future`] wrapper that preserves context across executor boundaries
//!
//! # Thread-Local Context Management
//!
//! Each thread maintains its own current context via thread-local storage. The context
//! forms a linked list from child to parent, enabling hierarchical task tracking:
//!
//! ```rust
//! use logwise::context::Context;
//!
//! // Set a new root context for this thread
//! Context::reset("root_task".to_string());
//!
//! // Get the current context
//! let current = Context::current();
//!
//! // Create a child context that inherits from the current one
//! let child = Context::from_parent(current.clone());
//! child.set_current();
//! ```
//!
//! # Task-Based Logging
//!
//! Tasks represent logical units of work and automatically log their lifecycle:
//!
//! ```rust
//! use logwise::context::Context;
//!
//! // Create a new task context
//! let task_ctx = Context::new_task(
//! Some(Context::current()),
//! "data_processing".to_string(),
//! logwise::Level::Info,
//! true,
//! );
//! task_ctx.clone().set_current();
//!
//! // The task automatically logs when it's created and dropped
//! // Any performance statistics collected during the task are logged on drop
//! ```
//!
//! # Performance Tracking
//!
//! The context system integrates with logwise's performance tracking to collect
//! statistics about task execution:
//!
//! ```rust
//! logwise::declare_logging_domain!();
//! # fn main() {
//! # use logwise::context::Context;
//! # Context::reset("test".to_string());
//! // Performance intervals are automatically added to the current task
//! // When using the perfwarn! macro:
//! logwise::perfwarn!("expensive_operation", {
//! // This duration is automatically tracked in the current context's task
//! std::thread::sleep(std::time::Duration::from_millis(100));
//! });
//! // Statistics are logged when the task is dropped
//! # }
//! ```
//!
//! # Async Context Preservation
//!
//! The [`ApplyContext`] wrapper ensures contexts are preserved across async boundaries,
//! particularly useful with executors that don't preserve thread-local state:
//!
//! ```rust
//! use logwise::context::{Context, ApplyContext};
//! # async fn async_operation() {}
//!
//! # async fn example() {
//! let ctx = Context::new_task(None, "async_task".to_string(), logwise::Level::Info, true);
//!
//! // Wrap the future to preserve context during polling
//! let future = ApplyContext::new(ctx, async_operation());
//! future.await;
//! # }
//! ```
//!
//! # Tracing Support
//!
//! Contexts support selective tracing for detailed debugging:
//!
//! ```rust
//! use logwise::context::Context;
//!
//! // Enable tracing for the current context and its children
//! Context::begin_trace();
//!
//! // Check if currently tracing
//! if Context::currently_tracing() {
//! // Trace-level logs will be enabled
//! }
//! ```
// Re-export public types
pub use ApplyContext;
pub use ;
pub use ;