mirror of https://github.com/TaKO8Ki/gobang
implement a log macro
parent
3c1e6d13e9
commit
78ba8459ad
@ -1,14 +1,63 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Deserialize)]
|
||||
pub enum LogLevel {
|
||||
Quiet,
|
||||
Error,
|
||||
Info,
|
||||
}
|
||||
|
||||
impl Default for LogLevel {
|
||||
fn default() -> Self {
|
||||
Self::Info
|
||||
}
|
||||
}
|
||||
|
||||
impl LogLevel {
|
||||
pub fn is_writable(&self, level: &Self) -> bool {
|
||||
use std::cmp::Ordering;
|
||||
matches!(self.cmp(level), Ordering::Greater | Ordering::Equal)
|
||||
}
|
||||
|
||||
pub fn write(&self, level: &Self) -> Box<dyn std::io::Write> {
|
||||
if self.is_writable(level) {
|
||||
match level {
|
||||
Self::Error => Box::from(std::io::stderr()),
|
||||
_ => Box::from(std::io::stdout()),
|
||||
}
|
||||
} else {
|
||||
Box::from(std::io::sink())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LogLevel> for &'static str {
|
||||
fn from(log_level: LogLevel) -> &'static str {
|
||||
match log_level {
|
||||
LogLevel::Quiet => "quiet",
|
||||
LogLevel::Info => "info",
|
||||
LogLevel::Error => "error",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for LogLevel {
|
||||
type Err = String;
|
||||
|
||||
fn from_str(s: &str) -> Result<LogLevel, Self::Err> {
|
||||
match s {
|
||||
"quiet" => Ok(Self::Quiet),
|
||||
"info" | "all" => Ok(Self::Info),
|
||||
"error" => Ok(Self::Error),
|
||||
level => Err(format!("I don't know the log level of {:?}", level)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! outln {
|
||||
($($expr:expr),+) => {{
|
||||
use std::io::{Write};
|
||||
use std::fs::OpenOptions;
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create(true)
|
||||
.append(true)
|
||||
.open("gobang.log")
|
||||
.unwrap();
|
||||
writeln!(file, $($expr),+).expect("Can't write output");
|
||||
($config:ident#$level:path, $($expr:expr),+) => {{
|
||||
use $crate::log::LogLevel::*;
|
||||
writeln!($config.log_level.write(&$level), $($expr),+).expect("Can't write output");
|
||||
}}
|
||||
}
|
||||
|
Loading…
Reference in New Issue