daemonbase 0.1.5

A library for providing the foundation for daemon processes.
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
//! Logging.

use crate::config::{ConfigFile, ConfigPath};
use crate::error::{ExitError, Failed};
use clap::ArgAction;
use log::error;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::{Mutex, MutexGuard, OnceLock};
use std::{fmt, fs, io};

// Export LevelFilter for clients that don't use log, e.g. if they use tracing
// instead.
pub use log::LevelFilter;

#[cfg(unix)]
pub use syslog::Facility;

//------------ Logger --------------------------------------------------------

/// The configuration for logging.
#[derive(Clone, Debug)]
pub struct Logger {
    /// The log levels to be logged.
    level: LevelFilter,

    /// The target to log to.
    target: Target,
}

impl Logger {
    pub fn new(level: LevelFilter, target: Target) -> Self {
        Self { level, target }
    }

    /// Initialize logging.
    ///
    /// Initializes the logging system so it can be used before having
    /// read the configuration. The function sets a maximum log level of
    /// `warn`, leading to only printing important information, and directs
    /// all log output to stderr.
    pub fn init_logging() -> Result<(), ExitError> {
        log::set_max_level(LevelFilter::Warn);
        if let Err(err) = log::set_logger(&GLOBAL_LOGGER) {
            eprintln!("Failed to initialize logger: {err}.\nAborting.");
            return Err(ExitError::default());
        }
        Ok(())
    }

    /// Creates the logger from a config struct.
    pub fn from_config(config: &Config) -> Result<Self, Failed> {
        Ok(Self {
            level: config.log_level.0,
            target: match config.log_target {
                TargetName::Default => Target::Default,
                #[cfg(unix)]
                TargetName::Syslog => {
                    Target::Syslog(config.syslog_facility.into())
                }
                TargetName::Stderr => Target::Stderr,
                TargetName::File => match config.log_file.as_ref() {
                    Some(LogPath::Stderr) => Target::Stderr,
                    Some(LogPath::Path(file)) => {
                        Target::File(file.clone().into())
                    }
                    None => {
                        error!("Missing 'log-file' option in config.");
                        return Err(Failed);
                    }
                },
            },
        })
    }

    /// Switches logging to the configured target.
    ///
    /// Once the configuration has been successfully loaded, logging should
    /// be switched to whatever the user asked for via this method.
    ///
    /// The `daemon` argument changes how the default log target is
    /// interpreted: If it is `true`, syslog is used on Unix systems if
    /// available via one of the standard Unix sockets. Otherwise, stderr is
    /// used.
    ///
    /// This method should only be called once. It returns an error if called
    /// again.
    pub fn switch_logging(&self, daemon: bool) -> Result<(), Failed> {
        let logger = Dispatch::new(self, daemon)?;
        GLOBAL_LOGGER.switch(logger);
        log::set_max_level(self.level);
        Ok(())
    }

    /// Rotates the log file if necessary.
    pub fn rotate_log(&self) -> Result<(), Failed> {
        GLOBAL_LOGGER.rotate()
    }
}

//------------ Config --------------------------------------------------------

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Config {
    #[serde(rename = "log-level", alias = "log_level", default)]
    log_level: LevelName,

    #[serde(rename = "log", alias = "log_target", default)]
    log_target: TargetName,

    #[cfg(unix)]
    #[serde(rename = "syslog-facility", alias = "log_facility", default)]
    syslog_facility: unix::FacilityArg,

    #[serde(rename = "log-file", alias = "log_file")]
    log_file: Option<LogPath>,
}

impl Config {
    /// Creates the logger from a config file.
    pub fn from_config_file(file: &mut ConfigFile) -> Result<Self, Failed> {
        Ok(Self {
            log_level: file
                .take_from_str::<LevelName>("log-level")?
                .unwrap_or_default(),
            log_target: file
                .take_from_str::<TargetName>("log")?
                .unwrap_or_default(),
            #[cfg(unix)]
            syslog_facility: file
                .take_from_str::<unix::FacilityArg>("syslog-facility")?
                .unwrap_or_default(),
            log_file: file.take_string("log-file")?.map(Into::into),
        })
    }

    pub fn from_args(args: &Args) -> Self {
        let mut res = Self::default();
        res.apply_args(args);
        res
    }

    /// Applies the arguments to the logger.
    pub fn apply_args(&mut self, args: &Args) {
        if let Some(level) = args.opt_level() {
            self.log_level = LevelName(level)
        }

        if args.stderr {
            self.log_target = TargetName::Stderr;
        } else if let Some(path) = args.logfile.as_ref() {
            self.log_target = TargetName::File;
            self.log_file = Some(path.clone());
        } else {
            #[cfg(unix)]
            if args.syslog {
                self.log_target = TargetName::Syslog;
            }
        }

        #[cfg(unix)]
        if let Some(facility) = args.syslog_facility {
            self.syslog_facility = facility;
        }
    }

    /// Adds the configuration a config file
    pub fn add_to_config_file(&self, config: &mut ConfigFile) {
        config.insert_string("log-level", self.log_level.as_str());
        config.insert_string("log", self.log_target.as_str());
        #[cfg(unix)]
        if !self.syslog_facility.is_default() {
            config.insert_string(
                "syslog-facility",
                self.syslog_facility.as_str(),
            );
        }
        if let Some(path) = self.log_file.as_ref() {
            config.insert_string("log-file", path);
        }
    }
}

//------------ TargetName ----------------------------------------------------

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize)]
#[serde(try_from = "String", into = "&'static str")]
enum TargetName {
    #[default]
    Default,

    #[cfg(unix)]
    Syslog,
    Stderr,
    File,
}

impl TargetName {
    fn as_str(self) -> &'static str {
        match self {
            TargetName::Default => "default",
            #[cfg(unix)]
            TargetName::Syslog => "syslog",
            TargetName::Stderr => "stderr",
            TargetName::File => "file",
        }
    }
}

impl From<TargetName> for &'static str {
    fn from(target: TargetName) -> Self {
        target.as_str()
    }
}

impl TryFrom<String> for TargetName {
    type Error = &'static str;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::from_str(&s)
    }
}

impl FromStr for TargetName {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "default" => Ok(TargetName::Default),
            #[cfg(unix)]
            "syslog" => Ok(TargetName::Syslog),
            "stderr" => Ok(TargetName::Stderr),
            "file" => Ok(TargetName::File),
            _ => Err("invalid log target"),
        }
    }
}

//------------ LevelName -----------------------------------------------------

#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
#[serde(try_from = "String", into = "&'static str")]
struct LevelName(LevelFilter);

impl Default for LevelName {
    fn default() -> Self {
        LevelName(LevelFilter::Warn)
    }
}

impl LevelName {
    fn as_str(self) -> &'static str {
        match self.0 {
            LevelFilter::Off => "off",
            LevelFilter::Error => "error",
            LevelFilter::Warn => "warn",
            LevelFilter::Info => "info",
            LevelFilter::Debug => "debug",
            LevelFilter::Trace => "trace",
        }
    }
}

impl From<LevelName> for &'static str {
    fn from(level: LevelName) -> Self {
        level.as_str()
    }
}

impl TryFrom<String> for LevelName {
    type Error = &'static str;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::from_str(&s)
    }
}

impl FromStr for LevelName {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        LevelFilter::from_str(s)
            .map(Self)
            .map_err(|_| "invalid log level")
    }
}

//------------ LogPath -------------------------------------------------------

/// A path that is either "-" for stderr or an actual path.
#[derive(Clone, Debug)]
pub enum LogPath {
    /// Standard error designated as a "-"
    Stderr,

    /// An actual path.
    Path(ConfigPath),
}

impl From<String> for LogPath {
    fn from(src: String) -> Self {
        if src == "-" {
            Self::Stderr
        } else {
            Self::Path(src.into())
        }
    }
}

impl<'de> Deserialize<'de> for LogPath {
    fn deserialize<D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Self, D::Error> {
        let path = String::deserialize(deserializer)?;
        if path == "-" {
            Ok(Self::Stderr)
        } else {
            Ok(Self::Path(path.into()))
        }
    }
}

impl Serialize for LogPath {
    fn serialize<S: Serializer>(
        &self,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        match self {
            Self::Stderr => "-".serialize(serializer),
            Self::Path(path) => path.serialize(serializer),
        }
    }
}

impl fmt::Display for LogPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Stderr => f.write_str("-"),
            Self::Path(path) => write!(f, "{}", path.display()),
        }
    }
}

//------------ Args ----------------------------------------------------------

#[derive(Clone, Debug, clap::Args)]
#[group(id = "log-args")]
pub struct Args {
    /// Log more information, twice for even more
    #[arg(short, long, action = ArgAction::Count)]
    verbose: u8,

    /// Log less information, twice for no information
    #[arg(short, long, action = ArgAction::Count, conflicts_with = "verbose")]
    quiet: u8,

    /// Log to syslog
    #[cfg(unix)]
    #[arg(long, conflicts_with_all = ["stderr", "logfile"])]
    syslog: bool,

    /// Log to stderr
    #[arg(long, conflicts_with = "logfile")]
    stderr: bool,

    /// Log to this file
    #[arg(long, value_name = "PATH", conflicts_with = "stderr")]
    logfile: Option<LogPath>,

    /// Facility to use for syslog logging
    #[cfg(unix)]
    #[arg(long, value_name = "FACILITY")]
    syslog_facility: Option<unix::FacilityArg>,
}

impl Args {
    #[cfg(unix)]
    pub fn is_syslog(&self) -> bool {
        self.syslog
    }

    pub fn is_stderr(&self) -> bool {
        self.stderr
    }

    pub fn log_file(&self) -> Option<&LogPath> {
        self.logfile.as_ref()
    }

    #[cfg(unix)]
    pub fn syslog_facility(&self) -> Option<&unix::FacilityArg> {
        self.syslog_facility.as_ref()
    }

    pub fn opt_level(&self) -> Option<LevelFilter> {
        if self.verbose > 1 {
            Some(LevelFilter::Debug)
        } else if self.verbose == 1 {
            Some(LevelFilter::Info)
        } else if self.quiet > 1 {
            Some(LevelFilter::Off)
        } else if self.quiet == 1 {
            Some(LevelFilter::Error)
        } else {
            None
        }
    }

    pub fn to_config(&self) -> Config {
        Config::from_args(self)
    }
}

//------------ Target --------------------------------------------------------

/// The target to log to.
#[derive(Clone, Debug, Default)]
pub enum Target {
    /// Default.
    ///
    /// Logs to `Syslog(Facility::LOG_DAEMON)` on Unix in daemon mode and
    /// `Stderr` otherwise.
    #[default]
    Default,

    /// Syslog.
    ///
    /// The argument is the syslog facility to use.
    #[cfg(unix)]
    Syslog(syslog::Facility),

    /// Stderr.
    Stderr,

    /// A file.
    ///
    /// The argument is the file name.
    File(PathBuf),
}

//--- PartialEq and Eq

impl PartialEq for Target {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Default, Self::Default) => true,
            #[cfg(unix)]
            (&Self::Syslog(s), &Self::Syslog(o)) => {
                (s as usize) == (o as usize)
            }
            (Self::Stderr, Self::Stderr) => true,
            (Self::File(s), Self::File(o)) => s == o,
            _ => false,
        }
    }
}

impl Eq for Target {}

//------------ Dispatch ------------------------------------------------------

/// Format and write log messages.
struct Dispatch {
    /// Where to write messages to.
    target: Mutex<LogBackend>,

    /// The maximum log level.
    level: LevelFilter,
}

/// The actual target for logging
enum LogBackend {
    #[cfg(unix)]
    Syslog(unix::SyslogLogger),
    File {
        file: fs::File,
        path: PathBuf,
    },
    Stderr {
        stderr: io::Stderr,
        timestamp: bool,
    },
}

impl Dispatch {
    /// Creates a new logger from config and additional information.
    fn new(config: &Logger, daemon: bool) -> Result<Self, Failed> {
        let target = match config.target {
            #[cfg(unix)]
            Target::Default => {
                if daemon {
                    Self::new_syslog_target(
                        syslog::Facility::LOG_DAEMON,
                        false,
                    )?
                } else {
                    Self::new_stderr_target(false)
                }
            }
            #[cfg(not(unix))]
            Target::Default => Self::new_stderr_target(false),
            #[cfg(unix)]
            Target::Syslog(facility) => {
                Self::new_syslog_target(facility, true)?
            }
            Target::File(ref path) => Self::new_file_target(path.clone())?,
            Target::Stderr => Self::new_stderr_target(daemon),
        };
        Ok(Self {
            target: Mutex::new(target),
            level: config.level,
        })
    }

    /// Creates a syslog target.
    ///
    /// If `use_inet` is `true`, also tries using the TCP and UDP options.
    #[cfg(unix)]
    fn new_syslog_target(
        facility: syslog::Facility,
        use_inet: bool,
    ) -> Result<LogBackend, Failed> {
        unix::SyslogLogger::new(facility, use_inet).map(LogBackend::Syslog)
    }

    fn new_file_target(path: PathBuf) -> Result<LogBackend, Failed> {
        Ok(LogBackend::File {
            file: match Self::open_log_file(&path) {
                Ok(file) => file,
                Err(err) => {
                    error!(
                        "Failed to open log file '{}': {}",
                        path.display(),
                        err
                    );
                    return Err(Failed);
                }
            },
            path,
        })
    }

    /// Opens a log file.
    fn open_log_file(path: &PathBuf) -> Result<fs::File, io::Error> {
        fs::OpenOptions::new().create(true).append(true).open(path)
    }

    /// Configures the stderr target.
    fn new_stderr_target(timestamp: bool) -> LogBackend {
        LogBackend::Stderr {
            stderr: io::stderr(),
            timestamp,
        }
    }

    /// Returns a mutex lock for the target
    fn target(&self) -> MutexGuard<'_, LogBackend> {
        self.target.lock().expect("poisoned mutex")
    }

    /// Logs a message.
    ///
    /// This method may exit the whole process if logging fails.
    fn log(&self, record: &log::Record) {
        if self.should_ignore(record) {
            return;
        }

        if let Err(err) = self.try_log(record) {
            self.log_failure(err);
        }
    }

    /// Tries logging a message and returns an error if there is one.
    fn try_log(&self, record: &log::Record) -> Result<(), io::Error> {
        match self.target().deref_mut() {
            #[cfg(unix)]
            LogBackend::Syslog(logger) => logger.log(record),
            LogBackend::File { file, .. } => {
                writeln!(
                    file,
                    "[{}] [{}] {}",
                    format_timestamp(),
                    record.level(),
                    record.args()
                )
            }
            LogBackend::Stderr { stderr, timestamp } => {
                // We never fail when writing to stderr.
                if *timestamp {
                    let _ = writeln!(
                        stderr,
                        "[{}] [{}] {}",
                        format_timestamp(),
                        record.level(),
                        record.args()
                    );
                } else {
                    let _ = writeln!(
                        stderr,
                        "[{}] {}",
                        record.level(),
                        record.args()
                    );
                }
                Ok(())
            }
        }
    }

    /// Handles an error that happened during logging.
    fn log_failure(&self, err: io::Error) -> ! {
        // We try to write a meaningful message to stderr and then abort.
        match self.target().deref() {
            #[cfg(unix)]
            LogBackend::Syslog(_) => {
                eprintln!("Logging to syslog failed: {err}. Exiting.");
            }
            LogBackend::File { path, .. } => {
                eprintln!(
                    "Logging to file {} failed: {}. Exiting.",
                    path.display(),
                    err
                );
            }
            LogBackend::Stderr { .. } => {
                // We never fail when writing to stderr.
            }
        }
        std::process::exit(1)
    }

    /// Flushes the logging backend.
    fn flush(&self) {
        match self.target().deref_mut() {
            #[cfg(unix)]
            LogBackend::Syslog(logger) => logger.flush(),
            LogBackend::File { file, .. } => {
                let _ = file.flush();
            }
            LogBackend::Stderr { stderr, .. } => {
                let _ = stderr.lock().flush();
            }
        }
    }

    /// Determines whether a log record should be ignored.
    ///
    /// This filters out messages by libraries that we don’t really want to
    /// see.
    fn should_ignore(&self, record: &log::Record) -> bool {
        let module = match record.module_path() {
            Some(module) => module,
            None => return false,
        };

        // log::Level sorts more important first.

        if record.level() > log::Level::Error {
            // From rustls, only log errors.
            if module.starts_with("rustls") {
                return true;
            }
        }
        if self.level >= log::LevelFilter::Debug {
            // Don’t filter anything else if we are in debug or trace.
            return false;
        }

        // Ignore these modules unless INFO or more important.
        record.level() > log::Level::Info
            && (module.starts_with("tokio_reactor")
                || module.starts_with("hyper")
                || module.starts_with("reqwest")
                || module.starts_with("h2"))
    }

    /// Rotates the log target if necessary.
    ///
    /// This method exits the whole process when rotating fails.
    fn rotate(&self) -> Result<(), Failed> {
        if let LogBackend::File { file, path } = self.target().deref_mut() {
            // This tries to open the file. If this fails, it writes a
            // message to both the old file and stderr and then exits.
            *file = match Self::open_log_file(path) {
                Ok(file) => file,
                Err(err) => {
                    let _ = writeln!(
                        file,
                        "Re-opening log file {} failed: {}. Exiting.",
                        path.display(),
                        err
                    );
                    eprintln!(
                        "Re-opening log file {} failed: {}. Exiting.",
                        path.display(),
                        err
                    );
                    return Err(Failed);
                }
            }
        }
        Ok(())
    }
}

//------------ SyslogLogger --------------------------------------------------

#[cfg(unix)]
mod unix {
    use super::*;
    use clap::builder::PossibleValue;

    /// A syslog logger.
    ///
    /// This is essentially [`syslog::BasicLogger`] but that one keeps the
    /// logger behind a mutex – which we already do – and doesn’t return
    /// error – which we do want to see.
    pub struct SyslogLogger(
        syslog::Logger<syslog::LoggerBackend, syslog::Formatter3164>,
    );

    impl SyslogLogger {
        /// Creates a new syslog logger.
        pub fn new(
            facility: syslog::Facility,
            use_inet: bool,
        ) -> Result<Self, Failed> {
            let process = std::env::current_exe()
                .ok()
                .and_then(|path| {
                    path.file_name()
                        .and_then(std::ffi::OsStr::to_str)
                        .map(ToString::to_string)
                })
                .unwrap_or_else(|| String::from("routinator"));
            let formatter = syslog::Formatter3164 {
                facility,
                hostname: None,
                process,
                pid: std::process::id(),
            };

            match syslog::unix(formatter.clone()) {
                Ok(logger) => return Ok(Self(logger)),
                Err(err) => {
                    if !use_inet {
                        error!("Cannot connect to syslog: {err}");
                        return Err(Failed);
                    }
                }
            }

            let logger = syslog::tcp(formatter.clone(), ("127.0.0.1", 601))
                .or_else(|_| {
                    syslog::udp(
                        formatter,
                        ("127.0.0.1", 0),
                        ("127.0.0.1", 514),
                    )
                });
            match logger {
                Ok(logger) => Ok(Self(logger)),
                Err(err) => {
                    error!("Cannot connect to syslog: {err}");
                    Err(Failed)
                }
            }
        }

        /// Tries logging.
        pub fn log(&mut self, record: &log::Record) -> Result<(), io::Error> {
            match record.level() {
                log::Level::Error => self.0.err(record.args()),
                log::Level::Warn => self.0.warning(record.args()),
                log::Level::Info => self.0.info(record.args()),
                log::Level::Debug => self.0.debug(record.args()),
                log::Level::Trace => {
                    // Syslog doesn’t have trace, use debug instead.
                    self.0.debug(record.args())
                }
            }
            .map_err(|err| match err {
                syslog::Error::Io(err) => err,
                err => io::Error::other(err),
            })
        }

        /// Flushes the logger.
        ///
        /// Ignores any errors.
        pub fn flush(&mut self) {
            let _ = self.0.backend.flush();
        }
    }

    /// Helper type to use the facility with a clap parser.
    #[derive(Clone, Copy, Debug, Deserialize, Serialize)]
    #[serde(try_from = "String", into = "&'static str")]
    pub struct FacilityArg(syslog::Facility);

    impl FacilityArg {
        pub fn is_default(self) -> bool {
            matches!(self.0, syslog::Facility::LOG_DAEMON)
        }

        pub fn as_str(self) -> &'static str {
            use syslog::Facility::*;

            match self.0 {
                LOG_KERN => "kern",
                LOG_USER => "user",
                LOG_MAIL => "mail",
                LOG_DAEMON => "daemon",
                LOG_ALERT => "alert",
                LOG_AUDIT => "audit",
                LOG_AUTH => "auth",
                LOG_SYSLOG => "syslog",
                LOG_LPR => "lpr",
                LOG_NEWS => "news",
                LOG_NTP => "ntp",
                LOG_UUCP => "uucp",
                LOG_CRON => "cron",
                LOG_AUTHPRIV => "authpriv",
                LOG_FTP => "ftp",
                LOG_CLOCK_DAEMON => "clock",
                LOG_LOCAL0 => "local0",
                LOG_LOCAL1 => "local1",
                LOG_LOCAL2 => "local2",
                LOG_LOCAL3 => "local3",
                LOG_LOCAL4 => "local4",
                LOG_LOCAL5 => "local5",
                LOG_LOCAL6 => "local6",
                LOG_LOCAL7 => "local7",
            }
        }
    }

    impl Default for FacilityArg {
        fn default() -> Self {
            Self(syslog::Facility::LOG_DAEMON)
        }
    }

    impl From<syslog::Facility> for FacilityArg {
        fn from(f: syslog::Facility) -> Self {
            Self(f)
        }
    }

    impl From<FacilityArg> for syslog::Facility {
        fn from(arg: FacilityArg) -> Self {
            arg.0
        }
    }

    impl From<FacilityArg> for &'static str {
        fn from(arg: FacilityArg) -> Self {
            arg.as_str()
        }
    }

    impl TryFrom<String> for FacilityArg {
        type Error = &'static str;

        fn try_from(s: String) -> Result<Self, Self::Error> {
            Self::from_str(&s)
        }
    }

    impl FromStr for FacilityArg {
        type Err = &'static str;

        fn from_str(s: &str) -> Result<Self, Self::Err> {
            syslog::Facility::from_str(s)
                .map(Self)
                .map_err(|_| "invalid syslog facility")
        }
    }

    impl clap::ValueEnum for FacilityArg {
        fn value_variants<'a>() -> &'a [Self] {
            &[
                Self(syslog::Facility::LOG_KERN),
                Self(syslog::Facility::LOG_USER),
                Self(syslog::Facility::LOG_MAIL),
                Self(syslog::Facility::LOG_DAEMON),
                Self(syslog::Facility::LOG_AUTH),
                Self(syslog::Facility::LOG_SYSLOG),
                Self(syslog::Facility::LOG_LPR),
                Self(syslog::Facility::LOG_NEWS),
                Self(syslog::Facility::LOG_UUCP),
                Self(syslog::Facility::LOG_CRON),
                Self(syslog::Facility::LOG_AUTHPRIV),
                Self(syslog::Facility::LOG_FTP),
                Self(syslog::Facility::LOG_LOCAL0),
                Self(syslog::Facility::LOG_LOCAL1),
                Self(syslog::Facility::LOG_LOCAL2),
                Self(syslog::Facility::LOG_LOCAL3),
                Self(syslog::Facility::LOG_LOCAL4),
                Self(syslog::Facility::LOG_LOCAL5),
                Self(syslog::Facility::LOG_LOCAL6),
                Self(syslog::Facility::LOG_LOCAL7),
            ]
        }

        fn to_possible_value(&self) -> Option<PossibleValue> {
            Some(PossibleValue::new(self.as_str()))
        }
    }
}

//------------ GlobalLogger --------------------------------------------------

/// The global logger.
///
/// A value of this type can go into a static. Until a proper logger is
/// installed, it just writes all log output to stderr.
struct GlobalLogger {
    /// The real logger. Can only be set once.
    inner: OnceLock<Dispatch>,
}

/// The static for the log crate.
static GLOBAL_LOGGER: GlobalLogger = GlobalLogger::new();

impl GlobalLogger {
    /// Creates a new provisional logger.
    const fn new() -> Self {
        GlobalLogger {
            inner: OnceLock::new(),
        }
    }

    /// Switches to the proper logger.
    fn switch(&self, logger: Dispatch) {
        if self.inner.set(logger).is_err() {
            panic!("Tried to switch logger more than once.")
        }
    }

    /// Performs a log rotation.
    fn rotate(&self) -> Result<(), Failed> {
        match self.inner.get() {
            Some(logger) => logger.rotate(),
            None => Ok(()),
        }
    }
}

impl log::Log for GlobalLogger {
    fn enabled(&self, _: &log::Metadata<'_>) -> bool {
        true
    }

    fn log(&self, record: &log::Record<'_>) {
        match self.inner.get() {
            Some(logger) => logger.log(record),
            None => {
                let _ = writeln!(
                    io::stderr().lock(),
                    "[{}] {}",
                    record.level(),
                    record.args()
                );
            }
        }
    }

    fn flush(&self) {
        if let Some(logger) = self.inner.get() {
            logger.flush()
        }
    }
}

//------------ Formatting dates ----------------------------------------------

/// Format the current local time as `9999-12-31T23:59:59`.
pub fn format_timestamp() -> impl fmt::Display {
    jiff::Zoned::now().strftime("%Y-%m-%dT%H:%M:%S")
}