acap_logging/lib.rs
1#![forbid(unsafe_code)]
2#![allow(clippy::needless_doctest_main)]
3#![doc = include_str!("../README.md")]
4#[cfg(feature = "tty")]
5use std::{env, io::IsTerminal};
6
7use log::debug;
8
9fn init_syslog() {
10 libsyslog::Syslog::builder()
11 .level(log::LevelFilter::Debug)
12 .build()
13 .init()
14 .unwrap();
15}
16
17/// Set up app-logging as appropriate for the environment, then run the provided function.
18///
19/// If stdout is a terminal, write to stderr.
20/// Otherwise, write to the system logger.
21///
22/// # Panics
23///
24/// This function will panic if
25/// it fails to initialize the appropriate logger or
26/// a global logger has already been initialized.
27pub fn init_logger() {
28 // Using `su -pc "..."` just says the "Connection to ... closed", and
29 // I have not found another way to run as the SDK user over ssh and allocate a tty, so
30 // if we detect an `env_logger` configuration, we write to stderr anyway.
31 #[cfg(feature = "tty")]
32 if std::io::stdout().is_terminal()
33 || env::var_os("RUST_LOG").is_some()
34 || env::var_os("RUST_LOG_STYLE").is_some()
35 {
36 env_logger::init();
37 debug!("Logging initialized");
38 return;
39 }
40
41 init_syslog();
42 debug!("Logging initialized");
43}