// SPDX-FileCopyrightText: Peter Pentchev <roam@ringlet.net>
// SPDX-License-Identifier: BSD-2-Clause
//! Helper functions for logging.
//!
//! Provide a helper that makes sure that a logger is always initialized
//! when running tests.

use core::iter;

#[cfg(feature = "test-log-simple")]
/// Implement `setup_logging` using the `simple_logger` crate.
mod simple {
    use std::sync::LazyLock;

    use log::{LevelFilter, debug};
    use simple_logger::SimpleLogger;

    /// Initialize a logger.
    pub fn setup_logging() {
        static LOGGER_INIT: LazyLock<()> = LazyLock::new(|| {
            if SimpleLogger::new()
                .with_level(LevelFilter::Trace)
                .init()
                .is_err()
            {
                debug!("Somebody set up us the logger");
            }
        });

        *LOGGER_INIT;
    }
}

/// Get the list of features supported by the module.
#[inline]
pub fn features() -> impl Iterator<Item = (&'static str, &'static str)> {
    let it_base = iter::once(("test-log", "0.1"));

    #[cfg(not(feature = "test-log-simple"))]
    let it_test_log_simple = it_base;
    #[cfg(feature = "test-log-simple")]
    let it_test_log_simple = it_base.chain(iter::once(("test-log-simple", "0.1")));

    it_test_log_simple
}

#[cfg(feature = "test-log-simple")]
/// Initialize a logger.
#[inline]
pub fn setup_logging() {
    simple::setup_logging();
}
