Skip to content

Commit

Permalink
Add start_logger function and support for color output
Browse files Browse the repository at this point in the history
  • Loading branch information
dotellie committed Jul 8, 2018
1 parent 3292b09 commit 6480a7a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ amethyst_input = { path = "amethyst_input", version = "0.3" }
amethyst_ui = { path = "amethyst_ui", version = "0.2" }
amethyst_utils = { path = "amethyst_utils", version = "0.2" }
derivative = "1.0"
fern = "0.5"
fern = { version = "0.5", features = ["colored"] }
log = "0.4"
rayon = "1.0.1"
rustc_version_runtime = "0.1"
Expand Down
24 changes: 6 additions & 18 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//! The core engine framework.
use std::error::Error as StdError;
use std::io;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

use core::shrev::{EventChannel, ReaderId};
use fern;
use log::LevelFilter;
use log::Level;
use rayon::ThreadPoolBuilder;
use shred::Resource;
#[cfg(feature = "profiler")]
Expand Down Expand Up @@ -377,21 +375,11 @@ impl<S> ApplicationBuilder<S> {
pub fn new<P: AsRef<Path>>(path: P, initial_state: S) -> Result<Self> {
use rustc_version_runtime;

fern::Dispatch::new()
.format(|out, message, record| {
out.finish(format_args!(
"[{}][{}] {}",
record.target(),
record.level(),
message
))
})
.level(LevelFilter::Debug)
.chain(io::stdout())
.apply()
.unwrap_or_else(|_| {
debug!("Global logger already set, default amethyst logger will not be used")
});
if !log_enabled!(Level::Error) {
println!(
"WARNING: No logger detected! Did you forget to call `amethyst::start_logger()`?"
);
}

info!("Initializing Amethyst...");
info!("Version: {}", env!("CARGO_PKG_VERSION"));
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extern crate rustc_version_runtime;
pub use self::app::{Application, ApplicationBuilder};
pub use self::error::{Error, Result};
pub use self::game_data::{DataInit, GameData, GameDataBuilder};
pub use self::logger::start_logger;
pub use self::state::{State, StateData, StateMachine, Trans};
pub use core::shred;
pub use core::shrev;
Expand All @@ -90,5 +91,6 @@ pub mod prelude;
mod app;
mod error;
mod game_data;
mod logger;
mod state;
mod vergen;
33 changes: 33 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::io;

use fern;
use log::LevelFilter;

/// Starts a basic logger outputting to stdout with color on supported platforms.
///
/// If you do not intend on using the logger builtin to Amethyst, it's highly recommended you
/// initialise your own.
pub fn start_logger() {
let color_config = fern::colors::ColoredLevelConfig::new();

fern::Dispatch::new()
.format(move |out, message, record| {
out.finish(format_args!(
"{color}[{level}][{target}] {message}{color_reset}",
color = format_args!(
"\x1B[{}m",
color_config.get_color(&record.level()).to_fg_str()
),
level = record.level(),
target = record.target(),
message = message,
color_reset = "\x1B[0m"
))
})
.level(LevelFilter::Debug)
.chain(io::stdout())
.apply()
.unwrap_or_else(|_| {
debug!("Global logger already set, default Amethyst logger will not be used")
});
}

0 comments on commit 6480a7a

Please sign in to comment.