# fake-log Examples
This directory contains examples demonstrating various aspects of the fake-log library.
## Available Examples
### 1. Basic Usage (`basic.rs`)
Demonstrates the fundamental usage of fake-log macros.
```bash
cargo run --example basic
```
**What it shows:**
- Basic logging macros (error!, warn!, info!, debug!, trace!)
- Formatted logging with variables
- Target-specific logging
- Complex formatting examples
**Expected behavior:**
- All log calls are silently ignored
- Only println! statements produce output
### 2. no_std Support (`no_std.rs`)
Shows how to use fake-log in embedded/no_std environments.
```bash
# Note: This example is designed for no_std but can be run in std environments
cargo run --example no_std
```
**What it shows:**
- `#![no_std]` attribute usage
- Custom panic handler for no_std
- Simulated embedded application with sensors
- Hardware initialization logging
**Expected behavior:**
- Demonstrates no_std compatibility
- All log calls are optimized away
### 3. Custom Logger (`custom_logger.rs`)
Demonstrates implementing the Log trait with fake-log.
```bash
cargo run --example custom_logger
```
**What it shows:**
- Custom Logger struct implementation
- Log trait methods (enabled, log, flush)
- Manual record creation and logging
- Service simulation with logging
**Expected behavior:**
- Manual logger calls produce output (for demonstration)
- Log macros are still optimized away
### 4. API Compatibility (`compatibility.rs`)
Shows complete API compatibility with the standard log crate.
```bash
cargo run --example compatibility
```
**What it shows:**
- Level and LevelFilter types
- Logger management functions
- Metadata and Record builders
- log_enabled! macro usage
- Generic log! macro
**Expected behavior:**
- Demonstrates API parity with log crate
- All logging operations return expected values
- Log macros produce no output
### 5. Performance Benchmark (`benchmark.rs`)
Measures the performance impact of fake-log operations.
```bash
# Run in release mode for accurate performance measurements
cargo run --example benchmark --release
```
**What it shows:**
- Performance of simple log calls
- Formatted logging performance
- Conditional logging overhead
- Complex scenario benchmarks
**Expected behavior:**
- Extremely low execution times (near zero)
- Demonstrates zero-overhead abstraction
## Running All Examples
You can run all examples at once using:
```bash
cargo run --example basic
cargo run --example custom_logger
cargo run --example compatibility
cargo run --example benchmark --release
```
Note: The `no_std` example requires additional setup for cross-compilation to embedded targets.
## Key Observations
When running these examples, you should notice:
1. **Zero Log Output**: Despite numerous log! calls, no actual log messages appear
2. **Fast Execution**: All examples run extremely quickly
3. **API Compatibility**: All log crate APIs work as expected
4. **Compilation Success**: Examples compile without warnings about unused log calls
## Using Examples as Templates
These examples can serve as templates for integrating fake-log into your own projects:
- Use `basic.rs` patterns for simple applications
- Use `no_std.rs` patterns for embedded projects
- Use `custom_logger.rs` patterns when you need to implement Log trait
- Use `compatibility.rs` for testing compatibility with existing log-based code
- Use `benchmark.rs` for performance testing
## Performance Notes
The benchmark example should show near-zero execution times for all logging operations when run in release mode (`--release`). This demonstrates that the Rust compiler successfully optimizes away all fake-log calls, achieving true zero-overhead logging.
## Integration Testing
These examples also serve as integration tests for the fake-log library, ensuring that:
- All public APIs work correctly
- Compilation succeeds in various scenarios
- Runtime behavior matches expectations
- Performance characteristics are maintained