From 645bb1c2bfaecd1bca3da4b862ecffbd18bf3086 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 27 Apr 2018 03:29:59 +0200 Subject: Main: Replace the logger with log4rs --- src/lib.rs | 4 +++- src/main.rs | 51 ++++++++++----------------------------------------- src/plugins/url.rs | 11 ++++++----- 3 files changed, 19 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 67a99d0..0e14e42 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -258,7 +258,9 @@ impl ThreadedPlugins { .spawn(move || { if let Err(e) = plugin.execute_threaded(&client, &message) { log_error(e); - }; + } else { + debug!("{} sent response from thread", plugin.name()); + } }) .context(ErrorKind::ThreadSpawn) { diff --git a/src/main.rs b/src/main.rs index 3660ccd..aae6035 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ extern crate frippy; extern crate glob; extern crate irc; +extern crate log4rs; extern crate time; #[cfg(feature = "mysql")] @@ -21,7 +22,6 @@ extern crate failure; #[macro_use] extern crate log; -use log::{Level, LevelFilter, Metadata, Record}; use std::collections::HashMap; #[cfg(feature = "mysql")] use std::sync::Arc; @@ -44,58 +44,27 @@ use frippy::Config; #[cfg(feature = "mysql")] embed_migrations!(); -struct Logger; - -impl log::Log for Logger { - fn enabled(&self, metadata: &Metadata) -> bool { - metadata.target().contains("frippy") - } - - fn log(&self, record: &Record) { - if self.enabled(record.metadata()) { - if record.metadata().level() >= Level::Debug { - println!( - "[{}]({}) {} -> {}", - time::now().rfc822(), - record.level(), - record.target(), - record.args() - ); - } else { - println!( - "[{}]({}) {}", - time::now().rfc822(), - record.level(), - record.args() - ); - } +fn main() { + if let Err(e) = log4rs::init_file("log.yml", Default::default()) { + use log4rs::Error; + match e { + Error::Log(e) => eprintln!("Log4rs error: {}", e), + Error::Log4rs(e) => eprintln!("Failed to parse \"log.yml\" as log4rs config: {}", e), } - } - fn flush(&self) {} -} - -static LOGGER: Logger = Logger; + return; + } -fn main() { // Print any errors that caused frippy to shut down if let Err(e) = run() { let text = e.causes() .skip(1) .fold(format!("{}", e), |acc, err| format!("{}: {}", acc, err)); error!("{}", text); - }; + } } fn run() -> Result<(), Error> { - log::set_max_level(if cfg!(debug_assertions) { - LevelFilter::Debug - } else { - LevelFilter::Info - }); - - log::set_logger(&LOGGER).unwrap(); - // Load all toml files in the configs directory let mut configs = Vec::new(); for toml in glob("configs/*.toml").unwrap() { diff --git a/src/plugins/url.rs b/src/plugins/url.rs index a54cabf..1ea6c9c 100644 --- a/src/plugins/url.rs +++ b/src/plugins/url.rs @@ -55,8 +55,7 @@ impl Title { .and_then(|s| s.and_then(|s| s)) .ok_or(ErrorKind::MissingTitle)?; - debug!("delimiters: {:?}", delimiters); - debug!("title: {:?}", title); + debug!("Found title {:?} with delimiters {:?}", title, delimiters); htmlescape::decode_html(title) .map(|t| t.into()) @@ -132,7 +131,7 @@ impl UrlTitles { } else { og_title } - }, + } (Ok(title), _) => title, (_, Ok(title)) => title, (Err(e), _) => Err(e)?, @@ -159,7 +158,7 @@ impl Plugin for UrlTitles { } fn execute_threaded(&self, client: &IrcClient, message: &Message) -> Result<(), FrippyError> { - Ok(match message.command { + match message.command { Command::PRIVMSG(_, ref content) => match self.url(content) { Ok(title) => client .send_privmsg(message.response_target().unwrap(), &title) @@ -167,7 +166,9 @@ impl Plugin for UrlTitles { Err(e) => Err(e).context(FrippyErrorKind::Url)?, }, _ => (), - }) + } + + Ok(()) } fn command(&self, client: &IrcClient, command: PluginCommand) -> Result<(), FrippyError> { -- cgit v1.2.3-70-g09d2