log-lazy 0.2.0

A lazy logging library with bitwise level control
Documentation
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
//! Lazy logging with bitwise level control.
//!
//! Expensive log messages are supplied as closures and are only evaluated when
//! the requested level is enabled.
//!
//! ```
//! use log_lazy::{levels, LogLazy};
//!
//! let log = LogLazy::with_level(levels::ERROR);
//! let mut evaluated = false;
//!
//! log.debug(|| {
//!     evaluated = true;
//!     "debug details"
//! });
//!
//! assert!(!evaluated);
//! ```

use chrono::{Local, SecondsFormat, Utc};
use std::collections::BTreeMap;
use std::fmt::{self, Debug, Display};
use std::sync::Arc;

/// Bitmask used to represent enabled log levels.
pub type LevelMask = u16;

/// Standard bitwise log level constants.
pub mod levels {
    use super::LevelMask;

    pub const NONE: LevelMask = 0;
    pub const FATAL: LevelMask = 1;
    pub const ERROR: LevelMask = 2;
    pub const WARN: LevelMask = 4;
    pub const INFO: LevelMask = 8;
    pub const DEBUG: LevelMask = 16;
    pub const VERBOSE: LevelMask = 32;
    pub const TRACE: LevelMask = 64;
    pub const SILLY: LevelMask = 128;
    pub const ALL: LevelMask = 255;
    pub const PRODUCTION: LevelMask = FATAL | ERROR | WARN;
    pub const DEVELOPMENT: LevelMask = FATAL | ERROR | WARN | INFO | DEBUG;
}

/// One standard output log level.
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
pub struct Level {
    mask: LevelMask,
    name: &'static str,
}

impl Level {
    pub const FATAL: Self = Self::new(levels::FATAL, "fatal");
    pub const ERROR: Self = Self::new(levels::ERROR, "error");
    pub const WARN: Self = Self::new(levels::WARN, "warn");
    pub const INFO: Self = Self::new(levels::INFO, "info");
    pub const DEBUG: Self = Self::new(levels::DEBUG, "debug");
    pub const VERBOSE: Self = Self::new(levels::VERBOSE, "verbose");
    pub const TRACE: Self = Self::new(levels::TRACE, "trace");
    pub const SILLY: Self = Self::new(levels::SILLY, "silly");

    pub const fn new(mask: LevelMask, name: &'static str) -> Self {
        Self { mask, name }
    }

    pub const fn mask(self) -> LevelMask {
        self.mask
    }

    pub const fn name(self) -> &'static str {
        self.name
    }

    pub const fn from_mask(mask: LevelMask) -> Option<Self> {
        match mask {
            levels::FATAL => Some(Self::FATAL),
            levels::ERROR => Some(Self::ERROR),
            levels::WARN => Some(Self::WARN),
            levels::INFO => Some(Self::INFO),
            levels::DEBUG => Some(Self::DEBUG),
            levels::VERBOSE => Some(Self::VERBOSE),
            levels::TRACE => Some(Self::TRACE),
            levels::SILLY => Some(Self::SILLY),
            _ => None,
        }
    }
}

impl Debug for Level {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Level")
            .field("name", &self.name)
            .field("mask", &self.mask)
            .finish()
    }
}

/// Standard levels in the order returned by [`LogLazy::get_enabled_levels`].
pub const STANDARD_LEVELS: [Level; 8] = [
    Level::FATAL,
    Level::ERROR,
    Level::WARN,
    Level::INFO,
    Level::DEBUG,
    Level::VERBOSE,
    Level::TRACE,
    Level::SILLY,
];

/// Level input accepted by the logger.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum LevelSpec<'a> {
    Mask(LevelMask),
    Name(&'a str),
}

impl<'a> From<Level> for LevelSpec<'a> {
    fn from(level: Level) -> Self {
        Self::Mask(level.mask())
    }
}

impl<'a> From<LevelMask> for LevelSpec<'a> {
    fn from(mask: LevelMask) -> Self {
        Self::Mask(mask)
    }
}

impl<'a> From<u8> for LevelSpec<'a> {
    fn from(mask: u8) -> Self {
        Self::Mask(LevelMask::from(mask))
    }
}

impl<'a> From<u32> for LevelSpec<'a> {
    fn from(mask: u32) -> Self {
        Self::Mask(LevelMask::try_from(mask).unwrap_or(levels::NONE))
    }
}

impl<'a> From<usize> for LevelSpec<'a> {
    fn from(mask: usize) -> Self {
        Self::Mask(LevelMask::try_from(mask).unwrap_or(levels::NONE))
    }
}

impl<'a> From<i32> for LevelSpec<'a> {
    fn from(mask: i32) -> Self {
        Self::Mask(LevelMask::try_from(mask).unwrap_or(levels::NONE))
    }
}

impl<'a> From<&'a str> for LevelSpec<'a> {
    fn from(name: &'a str) -> Self {
        Self::Name(name)
    }
}

impl<'a> From<&'a String> for LevelSpec<'a> {
    fn from(name: &'a String) -> Self {
        Self::Name(name.as_str())
    }
}

/// Owned level input accepted by [`LogLazyOptions`].
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum OwnedLevelSpec {
    Mask(LevelMask),
    Name(String),
}

impl From<Level> for OwnedLevelSpec {
    fn from(level: Level) -> Self {
        Self::Mask(level.mask())
    }
}

impl From<LevelMask> for OwnedLevelSpec {
    fn from(mask: LevelMask) -> Self {
        Self::Mask(mask)
    }
}

impl From<u8> for OwnedLevelSpec {
    fn from(mask: u8) -> Self {
        Self::Mask(LevelMask::from(mask))
    }
}

impl From<u32> for OwnedLevelSpec {
    fn from(mask: u32) -> Self {
        Self::Mask(LevelMask::try_from(mask).unwrap_or(levels::NONE))
    }
}

impl From<usize> for OwnedLevelSpec {
    fn from(mask: usize) -> Self {
        Self::Mask(LevelMask::try_from(mask).unwrap_or(levels::NONE))
    }
}

impl From<i32> for OwnedLevelSpec {
    fn from(mask: i32) -> Self {
        Self::Mask(LevelMask::try_from(mask).unwrap_or(levels::NONE))
    }
}

impl From<&str> for OwnedLevelSpec {
    fn from(name: &str) -> Self {
        Self::Name(name.to_string())
    }
}

impl From<String> for OwnedLevelSpec {
    fn from(name: String) -> Self {
        Self::Name(name)
    }
}

/// Function used to emit an already evaluated log message.
pub type LogSink = Arc<dyn Fn(Level, String) + Send + Sync + 'static>;

/// One log argument that can be evaluated lazily.
pub struct LogArg {
    value: LogArgValue,
}

enum LogArgValue {
    Text(String),
    Lazy(Box<dyn FnOnce() -> String + Send + 'static>),
}

impl LogArg {
    /// Creates an already evaluated argument.
    pub fn text(value: impl Into<String>) -> Self {
        Self {
            value: LogArgValue::Text(value.into()),
        }
    }

    /// Creates an argument that is evaluated only after the level is enabled
    /// and preprocessors have run.
    pub fn lazy<F, M>(value: F) -> Self
    where
        F: FnOnce() -> M + Send + 'static,
        M: Display,
    {
        Self {
            value: LogArgValue::Lazy(Box::new(move || value().to_string())),
        }
    }

    /// Returns the text for eager arguments.
    pub fn as_text(&self) -> Option<&str> {
        match &self.value {
            LogArgValue::Text(value) => Some(value.as_str()),
            LogArgValue::Lazy(_) => None,
        }
    }

    /// Returns true when this argument still holds a lazy closure.
    pub fn is_lazy(&self) -> bool {
        matches!(self.value, LogArgValue::Lazy(_))
    }

    fn evaluate(self) -> String {
        match self.value {
            LogArgValue::Text(value) => value,
            LogArgValue::Lazy(value) => value(),
        }
    }
}

impl Debug for LogArg {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.value {
            LogArgValue::Text(value) => formatter.debug_tuple("LogArg::Text").field(value).finish(),
            LogArgValue::Lazy(_) => formatter.write_str("LogArg::Lazy(..)"),
        }
    }
}

impl From<&str> for LogArg {
    fn from(value: &str) -> Self {
        Self::text(value)
    }
}

impl From<String> for LogArg {
    fn from(value: String) -> Self {
        Self::text(value)
    }
}

/// Options passed to a preprocessor.
pub struct PreprocessorOptions {
    pub args: Vec<LogArg>,
    pub level: Level,
}

/// Options passed to a postprocessor.
pub struct PostprocessorOptions {
    pub message: String,
    pub level: Level,
}

/// Function used to transform log arguments before message compilation.
pub type Preprocessor = Arc<dyn Fn(PreprocessorOptions) -> Vec<LogArg> + Send + Sync + 'static>;

/// Function used to transform a compiled message before output.
pub type Postprocessor = Arc<dyn Fn(PostprocessorOptions) -> String + Send + Sync + 'static>;

/// Options for constructing a logger with the extensible API.
#[derive(Clone)]
pub struct LogLazyOptions {
    level: OwnedLevelSpec,
    presets: BTreeMap<String, LevelMask>,
    sink: Option<LogSink>,
    preprocessors: Vec<Preprocessor>,
    postprocessors: Vec<Postprocessor>,
}

impl Debug for LogLazyOptions {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("LogLazyOptions")
            .field("level", &self.level)
            .field("presets", &self.presets)
            .field("has_sink", &self.sink.is_some())
            .field("preprocessors", &self.preprocessors.len())
            .field("postprocessors", &self.postprocessors.len())
            .finish()
    }
}

impl Default for LogLazyOptions {
    fn default() -> Self {
        Self {
            level: OwnedLevelSpec::Mask(levels::INFO),
            presets: BTreeMap::new(),
            sink: None,
            preprocessors: Vec::new(),
            postprocessors: Vec::new(),
        }
    }
}

impl LogLazyOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn level<L>(mut self, level: L) -> Self
    where
        L: Into<OwnedLevelSpec>,
    {
        self.level = level.into();
        self
    }

    pub fn preset(mut self, name: impl Into<String>, mask: LevelMask) -> Self {
        self.presets.insert(name.into(), mask);
        self
    }

    pub fn sink<F>(mut self, sink: F) -> Self
    where
        F: Fn(Level, String) + Send + Sync + 'static,
    {
        self.sink = Some(Arc::new(sink));
        self
    }

    pub fn preprocessor(mut self, preprocessor: Preprocessor) -> Self {
        self.preprocessors.push(preprocessor);
        self
    }

    pub fn preprocessor_fn<F>(mut self, preprocessor: F) -> Self
    where
        F: Fn(PreprocessorOptions) -> Vec<LogArg> + Send + Sync + 'static,
    {
        self.preprocessors.push(Arc::new(preprocessor));
        self
    }

    pub fn postprocessor(mut self, postprocessor: Postprocessor) -> Self {
        self.postprocessors.push(postprocessor);
        self
    }

    pub fn postprocessor_fn<F>(mut self, postprocessor: F) -> Self
    where
        F: Fn(PostprocessorOptions) -> String + Send + Sync + 'static,
    {
        self.postprocessors.push(Arc::new(postprocessor));
        self
    }
}

/// Built-in preprocessor helpers.
pub mod preprocessors {
    use super::{LogArg, Preprocessor, PreprocessorOptions};
    use std::sync::Arc;

    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub enum ContextPosition {
        Start,
        End,
    }

    #[derive(Debug, Clone, Eq, PartialEq)]
    pub struct AddContextOptions {
        pub context: String,
        pub position: ContextPosition,
    }

    impl AddContextOptions {
        pub fn new(context: impl Into<String>) -> Self {
            Self {
                context: context.into(),
                position: ContextPosition::End,
            }
        }

        pub fn position(mut self, position: ContextPosition) -> Self {
            self.position = position;
            self
        }
    }

    pub fn add_context(options: AddContextOptions) -> Preprocessor {
        Arc::new(move |mut preprocessor_options: PreprocessorOptions| {
            let context = LogArg::text(options.context.clone());
            match options.position {
                ContextPosition::Start => {
                    preprocessor_options.args.insert(0, context);
                    preprocessor_options.args
                }
                ContextPosition::End => {
                    preprocessor_options.args.push(context);
                    preprocessor_options.args
                }
            }
        })
    }

    pub struct FilterPredicateOptions<'a> {
        pub arg: &'a LogArg,
        pub index: usize,
        pub level: super::Level,
    }

    pub struct FilterOptions<F> {
        pub predicate: F,
    }

    pub fn filter<F>(options: FilterOptions<F>) -> Preprocessor
    where
        F: for<'a> Fn(FilterPredicateOptions<'a>) -> bool + Send + Sync + 'static,
    {
        let predicate = options.predicate;
        Arc::new(move |preprocessor_options: PreprocessorOptions| {
            let level = preprocessor_options.level;
            preprocessor_options
                .args
                .into_iter()
                .enumerate()
                .filter_map(|(index, arg)| {
                    if predicate(FilterPredicateOptions {
                        arg: &arg,
                        index,
                        level,
                    }) {
                        Some(arg)
                    } else {
                        None
                    }
                })
                .collect()
        })
    }

    pub struct MapTransformOptions {
        pub arg: LogArg,
        pub index: usize,
        pub level: super::Level,
    }

    pub struct MapOptions<F> {
        pub transform: F,
    }

    pub fn map<F>(options: MapOptions<F>) -> Preprocessor
    where
        F: Fn(MapTransformOptions) -> LogArg + Send + Sync + 'static,
    {
        let transform = options.transform;
        Arc::new(move |preprocessor_options: PreprocessorOptions| {
            let level = preprocessor_options.level;
            preprocessor_options
                .args
                .into_iter()
                .enumerate()
                .map(|(index, arg)| transform(MapTransformOptions { arg, index, level }))
                .collect()
        })
    }
}

/// Built-in postprocessor helpers.
pub mod postprocessors {
    use super::{format_timestamp, Postprocessor, PostprocessorOptions};
    use std::sync::Arc;

    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub enum TimestampFormat {
        Iso,
        Locale,
        Time,
        Millis,
    }

    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub struct TimestampOptions {
        pub format: TimestampFormat,
    }

    impl Default for TimestampOptions {
        fn default() -> Self {
            Self {
                format: TimestampFormat::Iso,
            }
        }
    }

    impl TimestampOptions {
        pub fn new() -> Self {
            Self::default()
        }

        pub fn format(mut self, format: TimestampFormat) -> Self {
            self.format = format;
            self
        }
    }

    pub fn timestamp(options: TimestampOptions) -> Postprocessor {
        Arc::new(move |postprocessor_options: PostprocessorOptions| {
            format!(
                "[{}] {}",
                format_timestamp(options.format),
                postprocessor_options.message
            )
        })
    }

    #[derive(Debug, Clone, Copy, Eq, PartialEq)]
    pub struct LevelOptions {
        pub uppercase: bool,
    }

    impl Default for LevelOptions {
        fn default() -> Self {
            Self { uppercase: true }
        }
    }

    impl LevelOptions {
        pub fn new() -> Self {
            Self::default()
        }

        pub fn uppercase(mut self, uppercase: bool) -> Self {
            self.uppercase = uppercase;
            self
        }
    }

    pub fn level(options: LevelOptions) -> Postprocessor {
        Arc::new(move |postprocessor_options: PostprocessorOptions| {
            let level_name = if options.uppercase {
                postprocessor_options.level.name().to_uppercase()
            } else {
                postprocessor_options.level.name().to_string()
            };
            format!("[{}] {}", level_name, postprocessor_options.message)
        })
    }

    #[derive(Debug, Clone, Eq, PartialEq)]
    pub struct PidOptions {
        pub label: String,
    }

    impl Default for PidOptions {
        fn default() -> Self {
            Self {
                label: "PID".to_string(),
            }
        }
    }

    impl PidOptions {
        pub fn new() -> Self {
            Self::default()
        }

        pub fn label(mut self, label: impl Into<String>) -> Self {
            self.label = label.into();
            self
        }
    }

    pub fn pid(options: PidOptions) -> Postprocessor {
        Arc::new(move |postprocessor_options: PostprocessorOptions| {
            format!(
                "[{}:{}] {}",
                options.label,
                std::process::id(),
                postprocessor_options.message
            )
        })
    }

    #[derive(Debug, Clone, Eq, PartialEq)]
    pub struct TextOptions {
        pub text: String,
    }

    impl TextOptions {
        pub fn new(text: impl Into<String>) -> Self {
            Self { text: text.into() }
        }
    }

    pub fn prefix(options: TextOptions) -> Postprocessor {
        Arc::new(move |postprocessor_options: PostprocessorOptions| {
            format!("{} {}", options.text, postprocessor_options.message)
        })
    }

    pub fn suffix(options: TextOptions) -> Postprocessor {
        Arc::new(move |postprocessor_options: PostprocessorOptions| {
            format!("{} {}", postprocessor_options.message, options.text)
        })
    }
}

/// Logger instance with a mutable bitmask and lazy message evaluation.
#[derive(Clone)]
pub struct LogLazy {
    current_level: LevelMask,
    presets: BTreeMap<String, LevelMask>,
    sink: LogSink,
    preprocessors: Vec<Preprocessor>,
    postprocessors: Vec<Postprocessor>,
}

impl Debug for LogLazy {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("LogLazy")
            .field("current_level", &self.current_level)
            .field("presets", &self.presets)
            .field("preprocessors", &self.preprocessors.len())
            .field("postprocessors", &self.postprocessors.len())
            .finish_non_exhaustive()
    }
}

impl Default for LogLazy {
    fn default() -> Self {
        Self {
            current_level: levels::INFO,
            presets: default_presets(),
            sink: Arc::new(default_sink),
            preprocessors: Vec::new(),
            postprocessors: Vec::new(),
        }
    }
}

impl LogLazy {
    /// Creates a logger with the default `info` level.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a logger from the extensible options API.
    pub fn with_options(options: LogLazyOptions) -> Self {
        let mut presets = default_presets();
        for (name, mask) in options.presets {
            presets.insert(name, mask);
        }

        let current_level = level_from_owned_spec(options.level, &presets, levels::INFO);

        Self {
            current_level,
            presets,
            sink: options.sink.unwrap_or_else(|| Arc::new(default_sink)),
            preprocessors: options.preprocessors,
            postprocessors: options.postprocessors,
        }
    }

    /// Creates a logger with a custom level.
    pub fn with_level<'a, L>(level: L) -> Self
    where
        L: Into<LevelSpec<'a>>,
    {
        let mut logger = Self::default();
        logger.set_level(level);
        logger
    }

    /// Creates a logger with a custom level and custom presets.
    pub fn with_level_and_presets<'a, L, I, S>(level: L, presets: I) -> Self
    where
        L: Into<LevelSpec<'a>>,
        I: IntoIterator<Item = (S, LevelMask)>,
        S: Into<String>,
    {
        let mut logger = Self::default();
        for (name, mask) in presets {
            logger.add_preset(name, mask);
        }
        logger.set_level(level);
        logger
    }

    /// Creates a logger with a custom sink.
    pub fn with_sink<'a, L, F>(level: L, sink: F) -> Self
    where
        L: Into<LevelSpec<'a>>,
        F: Fn(Level, String) + Send + Sync + 'static,
    {
        let mut logger = Self::with_level(level);
        logger.sink = Arc::new(sink);
        logger
    }

    /// Adds or replaces a named preset.
    pub fn add_preset(&mut self, name: impl Into<String>, mask: LevelMask) {
        self.presets.insert(name.into(), mask);
    }

    /// Returns the active level bitmask.
    pub const fn level(&self) -> LevelMask {
        self.current_level
    }

    /// Sets the active level bitmask.
    pub fn set_level<'a, L>(&mut self, level: L)
    where
        L: Into<LevelSpec<'a>>,
    {
        self.current_level = self.level_or_default(level, levels::INFO);
    }

    /// Resolves a string or numeric level to a mask, falling back to `default`.
    pub fn level_or_default<'a, L>(&self, level: L, default: LevelMask) -> LevelMask
    where
        L: Into<LevelSpec<'a>>,
    {
        match level.into() {
            LevelSpec::Mask(mask) => mask,
            LevelSpec::Name(name) => self
                .presets
                .get(name)
                .copied()
                .or_else(|| builtin_level_mask(name))
                .or_else(|| name.parse::<LevelMask>().ok())
                .unwrap_or(default),
        }
    }

    /// Checks whether a level is currently enabled.
    pub fn should_log<'a, L>(&self, level: L) -> bool
    where
        L: Into<LevelSpec<'a>>,
    {
        if self.current_level == levels::NONE {
            return false;
        }

        let level_mask = self.level_or_default(level, levels::NONE);
        level_mask != levels::NONE && (self.current_level & level_mask) != 0
    }

    /// Enables one level bit or mask.
    pub fn enable_level<'a, L>(&mut self, level: L)
    where
        L: Into<LevelSpec<'a>>,
    {
        let level_mask = self.level_or_default(level, levels::NONE);
        self.current_level |= level_mask;
    }

    /// Disables one level bit or mask.
    pub fn disable_level<'a, L>(&mut self, level: L)
    where
        L: Into<LevelSpec<'a>>,
    {
        let level_mask = self.level_or_default(level, levels::NONE);
        self.current_level &= !level_mask;
    }

    /// Returns enabled standard level names in stable order.
    pub fn get_enabled_levels(&self) -> Vec<&'static str> {
        STANDARD_LEVELS
            .iter()
            .filter(|level| self.should_log(level.mask()))
            .map(|level| level.name())
            .collect()
    }

    /// Logs at the default `info` level.
    pub fn log<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::INFO, message);
    }

    /// Logs at an explicit level.
    pub fn emit<'a, L, F, M>(&self, level: L, message: F)
    where
        L: Into<LevelSpec<'a>>,
        F: FnOnce() -> M,
        M: Display,
    {
        let level_mask = self.level_or_default(level, levels::NONE);
        if !self.should_log(level_mask) {
            return;
        }

        if let Some(level) = Level::from_mask(level_mask) {
            if self.preprocessors.is_empty() && self.postprocessors.is_empty() {
                (self.sink)(level, message().to_string());
            } else {
                self.emit_prepared_args(level, vec![LogArg::text(message().to_string())]);
            }
        }
    }

    /// Logs explicit lazy arguments at an explicit level.
    pub fn emit_args<'a, L, I>(&self, level: L, args: I)
    where
        L: Into<LevelSpec<'a>>,
        I: IntoIterator<Item = LogArg>,
    {
        let level_mask = self.level_or_default(level, levels::NONE);
        if !self.should_log(level_mask) {
            return;
        }

        if let Some(level) = Level::from_mask(level_mask) {
            self.emit_prepared_args(level, args.into_iter().collect());
        }
    }

    fn emit_prepared_args(&self, level: Level, args: Vec<LogArg>) {
        let mut processable_args = args;
        for preprocessor in &self.preprocessors {
            processable_args = preprocessor(PreprocessorOptions {
                args: processable_args,
                level,
            });
        }

        let mut message = processable_args
            .into_iter()
            .map(LogArg::evaluate)
            .collect::<Vec<_>>()
            .join(" ");

        for postprocessor in &self.postprocessors {
            message = postprocessor(PostprocessorOptions { message, level });
        }

        (self.sink)(level, message);
    }

    pub fn fatal<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::FATAL, message);
    }

    pub fn error<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::ERROR, message);
    }

    pub fn warn<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::WARN, message);
    }

    pub fn info<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::INFO, message);
    }

    pub fn debug<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::DEBUG, message);
    }

    pub fn verbose<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::VERBOSE, message);
    }

    pub fn trace<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::TRACE, message);
    }

    pub fn silly<F, M>(&self, message: F)
    where
        F: FnOnce() -> M,
        M: Display,
    {
        self.emit(Level::SILLY, message);
    }
}

/// Lazily formats and emits a message at an explicit level.
#[macro_export]
macro_rules! log_lazy {
    ($logger:expr, $level:expr, $($arg:tt)+) => {{
        $logger.emit($level, || format!($($arg)+));
    }};
}

/// Lazily formats and emits an `info` message.
#[macro_export]
macro_rules! info_lazy {
    ($logger:expr, $($arg:tt)+) => {{
        $logger.info(|| format!($($arg)+));
    }};
}

/// Lazily formats and emits a `debug` message.
#[macro_export]
macro_rules! debug_lazy {
    ($logger:expr, $($arg:tt)+) => {{
        $logger.debug(|| format!($($arg)+));
    }};
}

/// Lazily formats and emits an `error` message.
#[macro_export]
macro_rules! error_lazy {
    ($logger:expr, $($arg:tt)+) => {{
        $logger.error(|| format!($($arg)+));
    }};
}

fn default_presets() -> BTreeMap<String, LevelMask> {
    BTreeMap::from([
        ("production".to_string(), levels::PRODUCTION),
        ("development".to_string(), levels::DEVELOPMENT),
    ])
}

fn level_from_owned_spec(
    level: OwnedLevelSpec,
    presets: &BTreeMap<String, LevelMask>,
    default: LevelMask,
) -> LevelMask {
    match level {
        OwnedLevelSpec::Mask(mask) => mask,
        OwnedLevelSpec::Name(name) => presets
            .get(name.as_str())
            .copied()
            .or_else(|| builtin_level_mask(name.as_str()))
            .or_else(|| name.parse::<LevelMask>().ok())
            .unwrap_or(default),
    }
}

fn format_timestamp(format: postprocessors::TimestampFormat) -> String {
    match format {
        postprocessors::TimestampFormat::Iso => {
            Utc::now().to_rfc3339_opts(SecondsFormat::Millis, true)
        }
        postprocessors::TimestampFormat::Locale => Local::now().format("%c").to_string(),
        postprocessors::TimestampFormat::Time => Local::now().format("%T").to_string(),
        postprocessors::TimestampFormat::Millis => Utc::now().timestamp_millis().to_string(),
    }
}

fn builtin_level_mask(name: &str) -> Option<LevelMask> {
    match name {
        "none" => Some(levels::NONE),
        "fatal" => Some(levels::FATAL),
        "error" => Some(levels::ERROR),
        "warn" => Some(levels::WARN),
        "info" => Some(levels::INFO),
        "debug" => Some(levels::DEBUG),
        "verbose" => Some(levels::VERBOSE),
        "trace" => Some(levels::TRACE),
        "silly" => Some(levels::SILLY),
        "all" => Some(levels::ALL),
        "production" => Some(levels::PRODUCTION),
        "development" => Some(levels::DEVELOPMENT),
        _ => None,
    }
}

fn default_sink(level: Level, message: String) {
    match level {
        Level::FATAL | Level::ERROR | Level::WARN => eprintln!("{message}"),
        _ => println!("{message}"),
    }
}