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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
use crate::;
use ;
use HCLog;
/// Initialize the logging system.
///
/// This is the main initialization function used in the [`Scope`] implementation.
/// It initializes the internal logging system with a given name `S`, a [`Level`],
/// a [`FacadeVariant`]. If the [`LOGCOMPAT`] option is set in the [`Options`], the
/// compatibility layer to the `log` crate will be initialized as well.
///
/// # Errors
///
/// Initialize the compatibility layer to the [`crate-log`] crate
///
/// Some other crates might use the `log` crate for logging. The intention of this compatibility
/// layer is to provide a way to capture the output generated via log macros (like `error!()`,
/// `warn!()`, `info!()`, `debug!()`, `trace!()`) and redirect them to the internal logging system.
/// If the layer is already initialized this is a noop and won't change the current state.
///
/// To alter the settings of the compatibility layer at runtime, you can use the [`set_level()`]
/// or [`set_logdest()`] functions with the
/// [`InteralLogKeys::LogCompat`](enum@InternalLogKeys#variant@LogCompat)
/// module key.
///
/// ## Mapping from `log` to internal log levels
///
/// The `boxed_logger` is always initialized with the highest LevelFilter `Trace`. The filtering
/// of the messages is handled by the internal logging system. Whereas the mapping of the log levels
/// is:
///
/// | Level from crate log | Level in internal logging system |
/// |------------------------------|----------------------------------|
/// | [`Error`](log::Level::Error) | [`Error`](Level::Error) |
/// | [`Warn`](log::Level::Warn) | [`Warn`](Level::Warn) |
/// | [`Info`](log::Level::Info) | [`Info`](Level::Info) |
/// | [`Debug`](log::Level::Debug) | [`Debug1`](Level::Debug1) |
/// | [`Trace`](log::Level::Trace) | [`Debug10`](Level::Debug10) |
///
/// There is no distinction of other Debug levels in the compatibility layer. If you need more
/// fine grained control over the log levels, you should use the internal logging system directly.
///
/// # Examples
///
/// ```
/// use hclog::{Level, FacadeVariant};
/// use log::{debug, error, info, trace, warn};
///
/// fn main() {
/// hclog::init_log_compat("log", Level::Debug5, FacadeVariant::StdOut, None).unwrap();
/// // try all log:: macros once
/// error!("error!() print with log::log");
/// warn!("warn!() print with log::log");
/// info!("info!() print with log::log");
/// debug!("debug!() print with log::log");
/// trace!("trace!() print with log::log - not printed because filtered");
/// }
/// ```
///
/// # Errors
///
/// # Panics
/// Dump the internal state of the logging system to the supplied writer
///
/// Dump the current internal context (state) to a supplied [`Write`]r `W` instance. Usually this
/// will be any type that implements the [`std::io::Write`] trait, like a [`std::fs::File`] or
/// [`std::io::Stdout`].
///
/// The dump will only be performed if the environment variable `HCLOG_DUMP_MODULES` is set to `1`.
/// This is a safety measure to prevent accidental dumps in production code.
///
/// # Examples
///
/// ```rust
/// hclog::dump(&mut std::io::stdout()).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if:
/// * [`ContextLock`]: the internal context can't be accessed
/// * [`IoError`]: any underlying I/O error while writing to the supplied writer
///
/// # Panics
///
/// This function might panic for any reason the used writer might panic. Please refer
/// to the according documentation of the writer in use.
/// Print a list of all available modules to the supplied writer `w`
///
/// Print a comma separated list of all available [`LogKey`]s in all [`Scope`]s to the
/// supplied [`Write`]r `w` instance.
///
/// # Examples
///
/// ```rust
/// hclog::list_modules(&mut std::io::stdout()).unwrap();
/// ```
///
/// # Errors
///
/// Returns an error if:
/// * [`ContextLock`]: the internal context can't be accessed
/// * [`IoError`]: any underlying I/O error while writing to the supplied writer
///
/* scope access (async stuff) */
/* submod mgmt functions */
/// Initialize a [`Scope`] with a list of provided [`LogKey`]s.
///
/// The passed [`Level`], [`FacadeVariant`] and [`Options`] will be used for the [`Scope`]
/// and passed down to the each LogKey unless they provide custom values via the [`LogKey`]
/// trait implementation.
///
/// The given modname `S` is the name of the scope used for logging like the binary name in
/// syslog. The modules `I` input is everything which can be converted to a iterator.
///
/// This is a shortcut for calling [`init()`] and [`add_submodules()`] in sequence.
///
/// # Examples
///
/// ```rust
/// use hclog::{Scope, Level, FacadeVariant, options::Options, Result};
/// # use hclog::{LogKey, ContextKey};
///
/// #[derive(Copy, Clone)]
/// enum LogKeys { IA, IB, IC }
///
/// # impl std::fmt::Display for LogKeys {
/// # fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// # match *self {
/// # Self::IA => fmt.write_str("IA"),
/// # Self::IB => fmt.write_str("IB"),
/// # Self::IC => fmt.write_str("IC"),
/// # }
/// # }
/// # }
///
/// impl Scope for LogKeys {
/// fn init<S: std::fmt::Display>(
/// name: S, level: Level, facade: FacadeVariant, options: Options
/// ) -> Result<()> {
/// // instead of calling:
/// // hclog::init<Self, S>(name, level, facade, options)?;
/// // hclog::add_submodules(&[Self::IA, Self::IB, Self::IC])
/// // you could call:
/// hclog::init_modules(name, &[Self::IA, Self::IB, Self::IC], level, facade, options)
/// }
/// }
///
/// # impl LogKey for LogKeys {
/// # fn log_key(&self) -> ContextKey { *self as ContextKey }
/// # }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// * [ContextLock]: the internal context can't be accessed
/// * [LogCompatInitialized]: the compatibility layer is already initialized
///
/// Add a list of [`LogKey`]s to the a [`Scope`]
///
/// Add a set of [`LogKey`] implementors to their reserved [`Scope`]. The [`Scope`] must be
/// initialized before calling this function. The passed Iterator `I` must yield a reference
/// to a [`LogKey`] implementor.
///
/// This function is usually called inside the [`init()`] function of a [`Scope`] implementation
/// but is not limited to. It can be called at any time to add more modules to a scope.
///
/// The `LogKey`s receive the [`Level`], [`FacadeVariant`] and [`Options`] from the [`Scope`]
/// or can provide custom values via the [`LogKey`] trait implementation.
///
/// # Examples
///
/// ```rust
/// use hclog::{Scope, Level, FacadeVariant, options::Options, Result};
/// # use hclog::{LogKey, ContextKey};
///
/// #[derive(Copy, Clone)]
/// enum LogKeys { AA, AB, AC }
///
/// # impl std::fmt::Display for LogKeys {
/// # fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// # match *self {
/// # Self::AA => fmt.write_str("AA"),
/// # Self::AB => fmt.write_str("AB"),
/// # Self::AC => fmt.write_str("AC"),
/// # }
/// # }
/// # }
///
/// # impl LogKey for LogKeys {
/// # fn log_key(&self) -> ContextKey { *self as ContextKey }
/// # }
///
/// impl Scope for LogKeys {
/// fn init<S: std::fmt::Display>(
/// name: S, level: Level, facade: FacadeVariant, options: Options
/// ) -> Result<()> {
/// hclog::init::<Self, S>(name, level, facade, options)?;
/// hclog::add_submodules(&[Self::AA, Self::AB, Self::AC])
/// }
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// * [ContextLock]: the internal context can't be accessed
/// * [ScopeNotInitialized]: the scope is not initialized
///
/// Set the log level for a list of modules.
///
/// The input `I` must be an iterator of string slices which are formatted as
/// `key:level,key:level,...`. The `key` is the name of the module and the `level`
/// is a valid [`Level`] as string.
///
/// The `key` can be the name of a known [`LogKey`] or `_all` to set the log level
/// for all available `LogKey`s in the current `Scope`. The `LogKey` and `Level` names
/// are case insensitive.
///
/// This function is primarily used for setting the log level at runtime via the commandline
/// or environment variables.
///
/// # Examples
///
/// ```rust
/// # use hclog_macros::HCLog;
/// // assume a call something like this:
/// // $ myapp _all:warn,ma:info,mb:debug10
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum LogKeys { MA, MB }
///
/// fn main() {
/// // library initialization here - skipped for brevity
/// let args = std::env::args().skip(1).collect::<Vec<_>>();
/// match hclog::set_mod_level(&args) {
/// Ok(_) => (),
/// Err(e) => panic!("Error: {}", e),
/// }
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// * [ContextLock]: the internal context can't be accessed
/// * [ParseArg]: parsing the input fails
/// * [KeyNotInitialized]: the module is not initialized
/// * [UnknownLogLevel]: the log level is unknown
///
/// Check if a module is initialized in a given [`Scope`]
///
/// # Examples
///
/// ```rust
/// # use hclog_macros::HCLog;
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum LogKeys {
/// A,
/// B,
/// # #[hclog(ignore)]
/// C,
/// }
///
/// # LogKeys::init_with_defaults("test").unwrap();
/// assert_eq!(hclog::has_module(LogKeys::A).unwrap(), true);
/// assert_eq!(hclog::has_module(LogKeys::B).unwrap(), true);
/// // assume C doesn't get initialized
/// assert_eq!(hclog::has_module(LogKeys::C).unwrap(), false);
/// ```
///
/// # Errors
///
/// Returns an Error if:
/// * [ContextLock]: the internal context can't be accessed
/// * [ScopeNotInitialized]: the scope is not initialized
///
/// Set the log destination `FacadeVariant` for a given LogKey `K`
///
/// Alter the currently set [`FacadeVariant`] for a given LogKey at runtime. The LogKey `K` is an
/// initialized logkey as passed to the `init_modules` or `add_submodules` function.
/// The `FacadeVariant` can be any valid [`FacadeVariant`].
///
/// # Examples
///
/// ```rust
/// use hclog::FacadeVariant;
/// # use hclog_macros::HCLog;
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum SomeKey { SL }
///
/// # SomeKey::init_with_defaults("test").unwrap();
/// hclog::set_logdest(SomeKey::SL, FacadeVariant::StdErr).unwrap();
/// ```
///
/// # Errors
///
/// Returns an Error if:
/// * the module is not initialized ([`ScopeNotInitialized`])
/// * the submodule is not initialized ([`KeyNotInitialized`])
/// * the context can't be accessed ([`ContextLock`])
/// Set a `Level` for a single LogKey `K`
///
/// Alters the currently set [`Level`] for a given LogKey at runtime. The LogKey `K` is an
/// initialized logkey as passed to the `init_modules` or `add_submodules` function. The `Level`
/// can be any valid [`Level`].
///
/// # Examples
///
/// ```rust
/// use hclog::Level;
/// # use hclog_macros::HCLog;
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum SomeKey { IM }
///
/// # SomeKey::init_with_defaults("test").unwrap();
/// hclog::set_level(SomeKey::IM, Level::Debug9).unwrap();
/// ```
///
/// # Errors
///
/// Returns an Error if:
/// * the module is not initialized ([`ScopeNotInitialized`])
/// * the submodule is not initialized ([`KeyNotInitialized`])
/// * the context can't be accessed ([`ContextLock`])
///
/// Reset the options of a given LogKey `K`
///
/// This will reset the Options for a given `K` which implements the [`LogKey`] trait.
/// When reseting the options, the default options will be used and the environment variables
/// will be checked. This will not affect the log level or the log destination.
///
/// Resetting the options might be usefull when the options where changed at runtime for some
/// reason and you want restore the original state. Note that this will not restore the state
/// on initialization of the module. Instead the default is used and the environment variables
/// are respected.
///
/// # Examples
///
/// ```rust
/// # use hclog_macros::HCLog;
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum Key { RM }
///
/// # Key::init_with_defaults("test").unwrap();
/// hclog::reset_module_options(Key::RM).unwrap();
/// ```
///
/// # Errors
///
/// Returns an Error if:
/// * the module is not initialized ([`ScopeNotInitialized`])
/// * the submodule is not initialized ([`KeyNotInitialized`])
/// * parsing the environment variables fails ([`ParseArg`])
/// * the context can't be accessed ([`ContextLock`])
///
/// Unset one or multiple [`Options`] for a given LogKey
///
/// The first argument must be a valid `K` which implements the [`LogKey`] trait.
///
/// The second argument is a list of [`Options`] which will be unset for the given `K`.
/// The options can be arithmetically combined using the `+` operator or, if using the
/// default [`Options`], unset via the `-` operator. For a list of valid Options see the
/// [`Options`] documentation.
///
/// # Examples
///
/// ```rust
/// use hclog::{ErrorKind, options::{LOGCOMPAT, TIMESTAMP}};
/// # use hclog_macros::HCLog;
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum SomeKey { UM }
///
/// match hclog::unset_module_options(SomeKey::UM, LOGCOMPAT + TIMESTAMP) {
/// Ok(_) => (),
/// Err(e) if e == ErrorKind::ScopeNotInitialized => println!("Scope not initialized"),
/// Err(e) if e == ErrorKind::KeyNotInitialized => println!("Module not initialized"),
/// Err(_) => panic!("Unexpected error"),
/// }
/// ```
///
/// # Errors
///
/// Returns an Error if:
/// * the module is not initialized ([`ScopeNotInitialized`])
/// * the submodule is not initialized ([`KeyNotInitialized`])
/// * the context can't be accessed ([`ContextLock`])
///
/// Set one or multiple [`Options`] for a given LogKey
///
/// The first argument must be a valid `K` which implements the [`LogKey`] trait.
///
/// The second argument is a list of [`Options`] which will be set for the given `K`.
/// The options can be arithmetically combined using the `+` operator or, if using the
/// default [`Options`], unset via the `-` operator. For a list of valid Options see the
/// [`Options`] documentation.
///
/// # Examples
///
/// ```rust
/// use hclog::{ErrorKind, options::{LOGCOMPAT, TIMESTAMP}};
/// # use hclog_macros::HCLog;
///
/// # #[derive(Copy, Clone, HCLog)]
/// enum Key { SM }
///
/// # Key::init_with_defaults("test").unwrap();
/// match hclog::set_module_options(Key::SM, LOGCOMPAT + TIMESTAMP) {
/// Ok(_) => (),
/// Err(e) if e == ErrorKind::ScopeNotInitialized => println!("Scope not initialized"),
/// Err(e) if e == ErrorKind::KeyNotInitialized => println!("Module not initialized"),
/// Err(_) => panic!("Unexpected error"),
/// }
/// ```
///
/// # Errors
///
/// Returns an Error if:
/// * the module is not initialized ([`ScopeNotInitialized`])
/// * the submodule is not initialized ([`KeyNotInitialized`])
/// * the context can't be accessed ([`ContextLock`])
/*
* Don't document this function. It's only used for internal by the macros
*/
pub