|
|
@ -1,4 +1,5 @@
|
|
|
|
//! Global logging state
|
|
|
|
//! Global logging state
|
|
|
|
|
|
|
|
use super::util::*;
|
|
|
|
|
|
|
|
|
|
|
|
use std::{
|
|
|
|
use std::{
|
|
|
|
fmt::{
|
|
|
|
fmt::{
|
|
|
@ -10,6 +11,7 @@ use std::{
|
|
|
|
Write,
|
|
|
|
Write,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
borrow::Borrow,
|
|
|
|
borrow::Borrow,
|
|
|
|
|
|
|
|
sync::{Arc,RwLock},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
|
|
|
|
|
|
|
@ -24,6 +26,136 @@ pub enum Level
|
|
|
|
Debug,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Contains backtrace info from where the log macro was invoked
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
|
|
|
pub struct Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
line: Option<usize>,
|
|
|
|
|
|
|
|
file: Option<&'static str>,
|
|
|
|
|
|
|
|
column: Option<usize>,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
display: Opaque<RwLock<Option<String>>>,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Clone for Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
fn clone(&self) -> Self
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Self {
|
|
|
|
|
|
|
|
line: self.line.clone(),
|
|
|
|
|
|
|
|
file: self.file.clone(),
|
|
|
|
|
|
|
|
column: self.column.clone(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
display: Opaque::new(RwLock::new(None)),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl std::hash::Hash for Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
self.line.hash(state);
|
|
|
|
|
|
|
|
self.file.hash(state);
|
|
|
|
|
|
|
|
self.column.hash(state);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl std::cmp::PartialEq for Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn eq(&self, other: &Self)->bool
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
self.line == other.line &&
|
|
|
|
|
|
|
|
self.file == other.file &&
|
|
|
|
|
|
|
|
self.column == other.column
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Create a new `Trace` manually. Generally not desireable, use `get_trace!()` instead.
|
|
|
|
|
|
|
|
pub fn new(file: Option<&'static str>, line: Option<u32>, column: Option<u32>) -> Self
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
use std::convert::TryInto;
|
|
|
|
|
|
|
|
let this = Self{
|
|
|
|
|
|
|
|
file,
|
|
|
|
|
|
|
|
line: line.and_then(|x| x.try_into().ok()),
|
|
|
|
|
|
|
|
column: column.and_then(|x| x.try_into().ok()),
|
|
|
|
|
|
|
|
display: Opaque::new(RwLock::new(None)),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn genstring(&self) -> Result<String, fmt::Error>
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let mut out = String::new();
|
|
|
|
|
|
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
write!(out, "{}", self.file.unwrap_or("(unbound)"))?;
|
|
|
|
|
|
|
|
if let Some(line) = self.line {
|
|
|
|
|
|
|
|
write!(&mut out, ":{}", line)?;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
write!(&mut out, ":?")?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(column) = self.column {
|
|
|
|
|
|
|
|
write!(&mut out, ":{}>", column)?;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
write!(&mut out, ":?")?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let read = self.display.read().expect("Poisoned");
|
|
|
|
|
|
|
|
if let Some(opt) = read.as_ref()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
write!(f, "{}", opt)?;
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let out = self.genstring()?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
write!(f, "{}", out)?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut write = self.display.write().expect("Poisoned");
|
|
|
|
|
|
|
|
*write = Some(out);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for Trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
|
|
|
fn default() -> Self
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
Self{
|
|
|
|
|
|
|
|
line:None,
|
|
|
|
|
|
|
|
file:None,
|
|
|
|
|
|
|
|
column:None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
display: Opaque::new(RwLock::new(None)),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Get a source code trace of the current file, line and column.
|
|
|
|
|
|
|
|
#[macro_export] macro_rules! get_trace
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
() => {
|
|
|
|
|
|
|
|
$crate::log::Trace::new(Some(file!()), Some(line!()), Some(column!()))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Append this trait to allow you to
|
|
|
|
/// Append this trait to allow you to
|
|
|
|
pub trait AsLevel: fmt::Display + Borrow<Level>{
|
|
|
|
pub trait AsLevel: fmt::Display + Borrow<Level>{
|
|
|
|
/// Used for derived levels
|
|
|
|
/// Used for derived levels
|
|
|
@ -105,10 +237,11 @@ impl Logger
|
|
|
|
Logger::global()
|
|
|
|
Logger::global()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn println<W,L,D>(&self, mut to: W, level: L, what: D) -> io::Result<()>
|
|
|
|
pub fn println<W,L,D,T>(&self, mut to: W, level: L, what: D, trace: T) -> io::Result<()>
|
|
|
|
where W: Write,
|
|
|
|
where W: Write,
|
|
|
|
L: AsLevel,
|
|
|
|
L: AsLevel,
|
|
|
|
D: Display,
|
|
|
|
D: Display,
|
|
|
|
|
|
|
|
T: Borrow<Trace>
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//lol
|
|
|
|
//lol
|
|
|
|
enum Date {
|
|
|
|
enum Date {
|
|
|
@ -152,7 +285,13 @@ impl Logger
|
|
|
|
chrono::offset::Utc::now().into()
|
|
|
|
chrono::offset::Utc::now().into()
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
write!(to, "{} [", now)?;
|
|
|
|
write!(to, "{} ", now)?;
|
|
|
|
|
|
|
|
if self.title.len() > 0 {
|
|
|
|
|
|
|
|
write!(to, " <{}>\t", self.title)?;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
write!(to, " <{}>\t", trace.borrow())?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(to," [")?;
|
|
|
|
|
|
|
|
|
|
|
|
struct Prefix<'a,L: AsLevel>(&'a L);
|
|
|
|
struct Prefix<'a,L: AsLevel>(&'a L);
|
|
|
|
|
|
|
|
|
|
|
@ -167,12 +306,7 @@ impl Logger
|
|
|
|
|
|
|
|
|
|
|
|
write!(to, "{}", Prefix(&level))?; //what a mess...
|
|
|
|
write!(to, "{}", Prefix(&level))?; //what a mess...
|
|
|
|
write!(to, "{}", level)?;
|
|
|
|
write!(to, "{}", level)?;
|
|
|
|
write!(to, "]")?;
|
|
|
|
write!(to, "]: ")?;
|
|
|
|
if self.title.len() > 0 {
|
|
|
|
|
|
|
|
write!(to, " <{}>: ", self.title)?;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
write!(to, ": ")?;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
writeln!(to, "{}", what)?;
|
|
|
|
writeln!(to, "{}", what)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
Ok(())
|
|
|
@ -184,7 +318,7 @@ impl Logger
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
$crate::log::Logger::global().println(stdout, $crate::log::Level::Debug, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stdout, $crate::log::Level::Debug, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -221,7 +355,7 @@ impl Logger
|
|
|
|
|
|
|
|
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
$crate::log::Logger::global().println(stdout, Status, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stdout, Status, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -235,7 +369,7 @@ impl Logger
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
$crate::log::Logger::global().println(stdout, $crate::log::Level::Info, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stdout, $crate::log::Level::Info, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -270,7 +404,7 @@ impl Logger
|
|
|
|
|
|
|
|
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
$crate::log::Logger::global().println(stdout, Important, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stdout, Important, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -284,7 +418,7 @@ impl Logger
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let stderr = stderr.lock();
|
|
|
|
let stderr = stderr.lock();
|
|
|
|
$crate::log::Logger::global().println(stderr, $crate::log::Level::Warn, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stderr, $crate::log::Level::Warn, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -320,7 +454,7 @@ impl Logger
|
|
|
|
|
|
|
|
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let stderr = stderr.lock();
|
|
|
|
let stderr = stderr.lock();
|
|
|
|
$crate::log::Logger::global().println(stderr, Dangerous, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stderr, Dangerous, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -333,7 +467,7 @@ impl Logger
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let stderr = std::io::stderr();
|
|
|
|
let stderr = stderr.lock();
|
|
|
|
let stderr = stderr.lock();
|
|
|
|
$crate::log::Logger::global().println(stderr, $crate::log::Level::Error, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stderr, $crate::log::Level::Error, $obj, get_trace!()).expect("i/o error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
@ -346,13 +480,13 @@ impl Logger
|
|
|
|
{
|
|
|
|
{
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = std::io::stdout();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
let stdout = stdout.lock();
|
|
|
|
$crate::log::Logger::global().println(stdout, $crate::log::Level::Silent, $obj).expect("i/o error");
|
|
|
|
$crate::log::Logger::global().println(stdout, $crate::log::Level::Silent, $obj, get_trace!()).expect("i/o error");
|
|
|
|
|
|
|
|
|
|
|
|
std::process::exit(-1)
|
|
|
|
std::process::exit(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
($fmt:literal, $($args:expr),*) => {
|
|
|
|
error!(format!($fmt, $($args,)*));
|
|
|
|
fatal!(format!($fmt, $($args,)*));
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|