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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//! Minimal-resource logger for Rust applications.
//!
//! `minimal_logger` provides a low-allocation, thread-buffered logger
//! with optional file output, periodic flushing, platform-native log rotation, and
//! change-aware runtime reconfiguration via a builder API.
//!
//! # Quick start
//!
//! ```rust
//! use log::{error, info, warn};
//!
//! fn main() {
//! minimal_logger::init(minimal_logger::MinimalLoggerConfig::new())
//! .expect("failed to initialise logger");
//!
//! info!("application started");
//! warn!("disk usage above 80%");
//! error!("connection refused");
//!
//! minimal_logger::shutdown();
//! }
//! ```
//!
//! # Configuration
//!
//! All settings are passed through a [`MinimalLoggerConfig`] builder. Unset fields use
//! compile-time defaults on [`init()`] and keep their current value on [`reinit()`].
//!
//! | Builder method | Default at `init` | Description |
//! |--------------------|-------------------|-----------------------------------------------------|
//! | `.level(l)` | `Info` | Global log level |
//! | `.filter(t, l)` | *(none)* | Per-target level override; may be called many times |
//! | `.file(path)` | *(stderr)* | Append log records to a file (`O_APPEND`) |
//! | `.stderr()` | *(default)* | Explicitly route output back to stderr |
//! | `.buf_capacity(n)` | `4096` | Per-thread `BufWriter` capacity in bytes |
//! | `.flush_ms(ms)` | `1000` | Periodic flush interval; `0` flushes every record |
//! | `.format(tmpl)` | *see below* | Log-line template with `{field}` placeholders |
//!
//! New Unix log files are created with owner-only permissions, subject to the
//! process umask. Oversized environment/config values are bounded to avoid
//! accidental memory or latency spikes.
//!
//! ## Reading from environment variables
//!
//! [`config_from_env()`] reads the standard `RUST_LOG*` environment variables and
//! returns a pre-populated [`MinimalLoggerConfig`]. Builder methods can be chained
//! to override individual settings before passing to [`init()`] or [`reinit()`]:
//!
//! ```rust,no_run
//! // Pure environment-variable configuration.
//! minimal_logger::init(minimal_logger::config_from_env())
//! .expect("logger init failed");
//!
//! // Env vars as a baseline with a programmatic override.
//! minimal_logger::init(
//! minimal_logger::config_from_env().level(log::LevelFilter::Debug)
//! ).expect("logger init failed");
//! ```
//!
//! ## Level and filter syntax
//!
//! Set a global default level and optional per-target overrides:
//!
//! ```rust,no_run
//! minimal_logger::init(
//! minimal_logger::MinimalLoggerConfig::new()
//! .level(log::LevelFilter::Debug) // all targets at DEBUG
//! ).expect("logger init failed");
//!
//! minimal_logger::init(
//! minimal_logger::MinimalLoggerConfig::new()
//! .level(log::LevelFilter::Warn) // global WARN
//! .filter("myapp", log::LevelFilter::Debug) // myapp and submodules at DEBUG
//! ).expect("logger init failed");
//! ```
//!
//! Recognised levels: `Off`, `Error`, `Warn`, `Info`, `Debug`, `Trace`.
//! When multiple filters match a target the most specific (longest prefix) wins.
//!
//! ## Format fields
//!
//! | Placeholder | Example output |
//! |-----------------|-------------------------------|
//! | `{timestamp}` | `2026-04-18T12:34:56.789012Z` |
//! | `{level}` | `INFO` |
//! | `{thread_name}` | `main` |
//! | `{target}` | `myapp::server` |
//! | `{module_path}` | `myapp::server` |
//! | `{file}` | `src/server.rs` |
//! | `{line}` | `42` |
//! | `{args}` | `listening on :8080` |
//!
//! Width and alignment: `{level:<5}` left-aligns in a field of width 5;
//! `{line:>4}` right-aligns. Use `{{` and `}}` for literal braces.
//!
//! Default format string:
//! ```text
//! {timestamp} [{level:<5}] T[{thread_name}] [{target}] {args}
//! ```
//!
//! # Lifecycle
//!
//! 1. [`init()`] registers the global logger and starts the flush worker thread
//! when the configured flush interval is non-zero.
//! 2. Log macros (`info!`, `debug!`, …) render one log line and write it into
//! a shared buffered file writer (or stderr when file output is disabled).
//! 3. [`reinit()`] applies a new [`MinimalLoggerConfig`] and updates only the subsystems
//! whose effective configuration changed.
//! 4. [`shutdown()`] flushes active buffered output before exit.
//!
//! # Runtime reconfiguration
//!
//! Build a [`MinimalLoggerConfig`] with only the fields you want to change and call
//! [`reinit()`]. Unset fields keep their current values — no need to repeat the
//! full configuration:
//!
//! ```rust,no_run
//! // Switch to debug level without touching file, format, or flush settings.
//! minimal_logger::reinit(
//! minimal_logger::MinimalLoggerConfig::new().level(log::LevelFilter::Debug)
//! );
//! ```
//!
//! Only the components whose resolved value actually changed are updated:
//!
//! - **Filters / max level** — recomputed and applied via `log::set_max_level`.
//! - **Output file** — destination swapped atomically; the previous writer is
//! flushed/synced before close.
//! - **Flush interval** — old worker thread stopped and joined; new one spawned.
//! - **Format template** — new parsed template swapped in atomically.
//! - **Buffer size** — applied to newly opened log files.
//!
//! # Flush model
//!
//! | Trigger | Mechanism |
//! |--------------|-----------------------------------------------------------------|
//! | Buffer full | `BufWriter` flushes automatically on the next `write_all` call |
//! | Periodic | Background worker flushes the active shared file buffer |
//! | Explicit | [`shutdown()`] or `Log::flush()` flushes the active buffer |
//!
//! # Log rotation
//!
//! | Platform | Signal / mechanism |
//! |----------------|-------------------------------------------|
//! | Linux / macOS | `SIGHUP` |
//! | Other Unix | `SIGHUP` |
//! | Windows | `Local\RustLogger_LogRotate` named event |
//!
//! When rotation fires, the logger opens a new file and atomically swaps the
//! active writer. The old writer is flushed and synced before close.
use ;
use ;
pub use crateMinimalLoggerConfig;
pub use crateconfig_from_env;
use crate::;
// ─────────────────────────────────────────────────────────────────────────────
// Atomic flags
//
// REOPEN_FLAG : set by platform signal/event → triggers LogFile swap in log().
//
// REOPEN_FLAG is checked in Log::log() and handled by reopening/switching the
// shared active file writer.
// ─────────────────────────────────────────────────────────────────────────────
/// Set by the platform rotation handler (`SIGHUP` / named event) to request
/// a log file reopen on the next `Log::log` call.
static REOPEN_FLAG: AtomicBool = new;
/// Ensures repeated I/O failures do not recursively flood stderr.
static IO_ERROR_REPORTED: AtomicBool = new;
pub
// ═════════════════════════════════════════════════════════════════════════════
// COMMON: Public API
// ═════════════════════════════════════════════════════════════════════════════
/// Initialise the logger and start the periodic flush worker.
///
/// Call this once at program startup, before any log macros are used.
/// Returns [`Err(SetLoggerError)`](log::SetLoggerError) if a global logger has
/// already been registered (either by this crate on a previous call, or by
/// another logging crate).
///
/// If called more than once the `config` argument is ignored — the singleton
/// is only constructed on the first successful call. Use [`reinit()`] to
/// update a running logger.
///
/// Pass [`MinimalLoggerConfig::new()`] for fully programmatic configuration, or
/// [`config_from_env()`] to read from the `RUST_LOG*` environment variables.
///
/// The first successful call also installs:
/// - A platform rotation handler (`SIGHUP` on Unix; a named event on Windows).
/// - A panic hook that flushes active buffered output before the process unwinds.
/// Apply an updated configuration to the running logger.
///
/// Only subsystems whose effective value changed are updated — the rest are
/// left untouched. If the resolved configuration is identical to the active
/// one, this function returns immediately without modifying any runtime state.
///
/// Unset fields in `config` **keep their current value** and do not reset to
/// defaults. See [`MinimalLoggerConfig`] for the full list of configurable fields.
///
/// | Changed setting | Effect |
/// |--------------------------|---------------------------------------------------------|
/// | `.level()` / `.filter()` | Log filters and max level updated atomically |
/// | `.file()` / `.stderr()` | Destination swapped; old writer flushed/synced on switch |
/// | `.flush_ms()` | Old flush worker stopped and joined; new worker spawned |
/// | `.format()` | Format template swapped atomically |
/// | `.buf_capacity()` | Applied when a new file writer is opened |
///
/// If the logger has not yet been initialised this function behaves like
/// [`init()`] with the supplied `config`, discarding any initialisation error.
/// Flush active buffered output and stop the background flush worker.
///
/// Call this near process exit to ensure all buffered log records are written
/// and the flush worker thread has exited cleanly.
///
/// This function does not close the log file descriptor; the OS releases it
/// when the process exits.