Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrono patch #19

Merged
merged 1 commit into from Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -20,4 +20,4 @@ include = [
env_logger = "0.5"
log = "0.4"
ansi_term = "0.11"

chrono = "0.4.4"
82 changes: 82 additions & 0 deletions src/lib.rs
Expand Up @@ -24,10 +24,12 @@
extern crate ansi_term;
extern crate env_logger;
extern crate log;
extern crate chrono;

use std::fmt;
use std::sync::atomic::{AtomicUsize, Ordering, ATOMIC_USIZE_INIT};

use chrono::Local;
use ansi_term::{Color, Style};
use env_logger::Builder;
use log::Level;
Expand Down Expand Up @@ -62,6 +64,20 @@ pub fn init() {
try_init().unwrap();
}

/// Initializes the global logger with a timed pretty env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Panics
///
/// This function fails to set the global logger if one has already been set.
#[inline]
pub fn init_timed() {
try_init_timed().unwrap();
}

/// Initializes the global logger with a pretty env logger.
///
/// This should be called early in the execution of a Rust program, and the
Expand All @@ -75,6 +91,19 @@ pub fn try_init() -> Result<(), log::SetLoggerError> {
try_init_custom_env("RUST_LOG")
}

/// Initializes the global logger with a timed pretty env logger.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Errors
///
/// This function fails to set the global logger if one has already been set.
pub fn try_init_timed() -> Result<(), log::SetLoggerError> {
try_init_timed_custom_env("RUST_LOG")
}

/// Initialized the global logger with a pretty env logger, with a custom variable name.
///
/// This should be called early in the execution of a Rust program, and the
Expand Down Expand Up @@ -107,6 +136,25 @@ pub fn try_init_custom_env(environment_variable_name: &str) -> Result<(), log::S
builder.try_init()
}

/// Initialized the global logger with a timed pretty env logger, with a custom variable name.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Errors
///
/// This function fails to set the global logger if one has already been set.
pub fn try_init_timed_custom_env(environment_variable_name: &str) -> Result<(), log::SetLoggerError> {
let mut builder = formatted_timed_builder()?;

if let Ok(s) = ::std::env::var(environment_variable_name) {
builder.parse(&s);
}

builder.try_init()
}

/// Returns a `env_logger::Builder` for further customization.
///
/// This method will return a colored and formatted) `env_logger::Builder`
Expand Down Expand Up @@ -139,3 +187,37 @@ pub fn formatted_builder() -> Result<Builder, log::SetLoggerError> {

Ok(builder)
}

/// Returns a `env_logger::Builder` for further customization.
///
/// This method will return a colored and time formatted) `env_logger::Builder`
/// for further customization. Tefer to env_logger::Build crate documentation
/// for further details and usage.
///
/// This should be called early in the execution of a Rust program, and the
/// global logger may only be initialized once. Future initialization attempts
/// will return an error.
///
/// # Errors
///
/// This function fails to set the global logger if one has already been set.
pub fn formatted_timed_builder() -> Result<Builder, log::SetLoggerError> {
let mut builder = Builder::new();

builder.format(|f, record| {
use std::io::Write;
let target = record.target();
let mut max_width = MAX_MODULE_WIDTH.load(Ordering::Relaxed);
if max_width < target.len() {
MAX_MODULE_WIDTH.store(target.len(), Ordering::Relaxed);
max_width = target.len();
}
writeln!(f, " {}:[{}] {} > {}",
ColorLevel(record.level()),
Local::now().format("%Y-%m-%dT%H:%M:%S"),
Style::new().bold().paint(format!("{: <width$}", target, width=max_width)),
record.args())
});

Ok(builder)
}