fake-log 0.1.0

A fake logging implementation with the same interface as log crate but with no-op implementation for embedded scenarios
//! Custom Logger implementation example
//!
//! This example demonstrates how to implement the Log trait using fake-log.
//! Even though the logger implementation is provided, fake-log will still
//! optimize away all logging calls.

use fake_log::{debug, error, info, trace, warn};
use fake_log::{Level, Log, Metadata, Record};

struct CustomLogger {
    name: &'static str,
}

impl CustomLogger {
    fn new(name: &'static str) -> Self {
        Self { name }
    }
}

impl Log for CustomLogger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        // In fake-log, this will always be called with metadata,
        // but the actual logging will be optimized away
        println!(
            "Logger '{}': checking if level {:?} is enabled",
            self.name,
            metadata.level()
        );

        // Return false to indicate no logging should happen
        // (though fake-log will optimize it away anyway)
        false
    }

    fn log(&self, record: &Record) {
        // This method will never actually be called in fake-log
        // because the macros are optimized away, but we implement
        // it for completeness
        println!(
            "Logger '{}': [{}] {}",
            self.name,
            record.level(),
            record.args()
        );
    }

    fn flush(&self) {
        // Flush implementation - also a no-op in fake-log
        println!("Logger '{}': flushing", self.name);
    }
}

fn main() {
    println!("=== Custom Logger Example ===");
    println!("Note: Even with a custom logger, fake-log optimizes away all calls");
    println!();

    // Create a custom logger
    let logger = CustomLogger::new("MyApp");

    // Demonstrate logger methods
    let metadata = Metadata::builder()
        .level(Level::Info)
        .target("example")
        .build();

    println!("Testing logger.enabled(): {}", logger.enabled(&metadata));

    // Create a record manually (for demonstration)
    let record = Record::builder()
        .level(Level::Info)
        .target("example")
        .args(format_args!("Manual log record"))
        .build();

    logger.log(&record);
    logger.flush();

    println!();
    println!("Now using log macros (these will be optimized away):");

    // Use the logging macros - these will all be optimized away
    error!("This error will not appear");
    warn!("This warning will not appear");
    info!("This info will not appear");
    debug!("This debug will not appear");
    trace!("This trace will not appear");

    // Complex example with a simulated service
    simulate_service();

    println!();
    println!("Program completed!");
    println!("Notice: The log macros produced no output despite the custom logger");
}

fn simulate_service() {
    info!("Starting service simulation");

    for i in 1..=5 {
        debug!("Processing item {}", i);

        if i % 2 == 0 {
            warn!("Even number detected: {}", i);
        }

        if i == 3 {
            error!("Simulated error at item {}", i);
        }
    }

    info!("Service simulation completed");
}