scribe/examples/logger.rs
Jason Travis Smith cdb4efe1b1 Now correctly tests not using the Rust standard library.
Added the ability to correctly create a TestLogger when not
using the standard library. This was done by creating a constant
TestLogger and a set_logger function that returned a constant pointer
to the constant logger.
2017-01-12 15:07:53 -05:00

123 lines
1.9 KiB
Rust

#[macro_use]
extern crate scribe;
use scribe::{Levels, Logger, Record};
#[cfg(not(feature="use_std"))]
const core_logger: TestLogger = TestLogger {};
pub struct TestLogger
{
}
impl TestLogger
{
pub fn start()
{
match scribe::set_logger(TestLogger::set_logger)
{
Ok(_) =>
{
println!("Logger sucessfully set.");
}
Err(error) =>
{
println!("ERROR: {}", error);
}
}
}
pub fn stop()
{
match scribe::clear_logger()
{
Ok(_) =>
{
println!("Logger cleared.");
}
Err(error) =>
{
println!("ERROR: {}", error);
}
}
}
#[cfg(feature="use_std")]
fn set_logger() -> Box<Logger>
{
let logger: TestLogger;
logger =
TestLogger
{
};
Box::new(logger)
}
#[cfg(not(feature="use_std"))]
fn set_logger() -> *const Logger
{
&core_logger
}
}
impl Logger for TestLogger
{
fn is_domain_enabled(&self, domain_name: &'static str) -> bool
{
println!("Domain '{}': {}", domain_name, true);
true
}
fn is_level_enabled(&self, lvl: Levels) -> bool
{
println!("Level '{}': {}", lvl, true);
true
}
fn log(&self, record: &Record)
{
println!("Record: {}", record);
}
}
mod temp_mod
{
pub fn print()
{
info!("Mod test.");
}
}
pub fn main()
{
TestLogger::start();
temp_mod::print();
debug!("This seemed to work alright.");
debug!(domain: "Temp Domain", "This seemed to work alright.");
info!("Jason is awesome.");
info!(domain: "Temp Domain", "Jason is awesome.");
warn!("Danger Will Robinson, danger!");
warn!(domain: "Temp Domain", "Danger Will Robinson, danger!");
//error!("There was an error!");
//error!(domain: "Temp Domain", "There was an error!");
TestLogger::stop();
}