From 8e573c5a7a6747dd3f01c3ed97c88a3c5a57d7ec Mon Sep 17 00:00:00 2001 From: Jokler Date: Wed, 1 Nov 2017 17:15:54 +0100 Subject: Add debug logging The log level gets set to debug or info based on the build profile automatically now. Debug messages also print their target to the console. --- Cargo.lock | 40 ++++++++++++++++++++-------------------- bin/main.rs | 29 ++++++++++++++++++++++++----- src/plugin.rs | 11 ++++++++++- src/plugins/currency.rs | 1 - 4 files changed, 54 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 239bad5..8fe0acc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,23 +1,3 @@ -[root] -name = "frippy" -version = "0.2.0" -dependencies = [ - "clippy 0.0.166 (registry+https://github.com/rust-lang/crates.io-index)", - "futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", - "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "irc 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)", - "lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", - "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", - "plugin_derive 0.1.0", - "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "reqwest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", - "tokio-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "unicode_names 0.1.7 (git+https://github.com/Jokler/unicode_names?branch=update-to-latest-unicode)", -] - [[package]] name = "adler32" version = "1.0.2" @@ -292,6 +272,26 @@ name = "foreign-types" version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "frippy" +version = "0.3.0" +dependencies = [ + "clippy 0.0.166 (registry+https://github.com/rust-lang/crates.io-index)", + "futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "irc 0.12.5 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "plugin_derive 0.1.0", + "regex 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "reqwest 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.16 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-core 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode_names 0.1.7 (git+https://github.com/Jokler/unicode_names?branch=update-to-latest-unicode)", +] + [[package]] name = "fuchsia-zircon" version = "0.2.1" diff --git a/bin/main.rs b/bin/main.rs index 18f362e..86910c0 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -8,21 +8,40 @@ struct Logger; impl log::Log for Logger { fn enabled(&self, metadata: &LogMetadata) -> bool { - metadata.level() <= LogLevel::Info + metadata.target().contains("frippy") } fn log(&self, record: &LogRecord) { if self.enabled(record.metadata()) { - println!("[{}]({}) {}", time::now().rfc822(), record.level(), record.args()); + if record.metadata().level() >= LogLevel::Debug { + println!("[{}]({}) {} -> {}", + time::now().rfc822(), + record.level(), + record.target(), + record.args()); + } else { + println!("[{}]({}) {}", + time::now().rfc822(), + record.level(), + record.args()); + } } } } fn main() { + + let log_level = if cfg!(debug_assertions) { + LogLevelFilter::Debug + } else { + LogLevelFilter::Info + }; + log::set_logger(|max_log_level| { - max_log_level.set(LogLevelFilter::Info); - Box::new(Logger) - }).unwrap(); + max_log_level.set(log_level); + Box::new(Logger) + }) + .unwrap(); frippy::run(); } diff --git a/src/plugin.rs b/src/plugin.rs index e0a4ce2..fb35668 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -98,6 +98,10 @@ impl ThreadedPlugins { // Send the message to the plugin if the plugin needs it if lock_plugin!(plugin).is_allowed(server, &message) { + debug!("Executing {} with {}", + name, + message.to_string().replace("\r\n", "")); + // Clone everything before the move // The server uses an Arc internally too let plugin = Arc::clone(&plugin); @@ -114,7 +118,10 @@ impl ThreadedPlugins { } } - pub fn handle_command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { + pub fn handle_command(&mut self, + server: &IrcServer, + mut command: PluginCommand) + -> Result<(), IrcError> { if !command.tokens.iter().any(|s| !s.is_empty()) { let help = format!("Use \"{} help\" to get help", server.current_nickname()); @@ -127,6 +134,8 @@ impl ThreadedPlugins { // The first token contains the name of the plugin let name = command.tokens.remove(0); + debug!("Sending command \"{:?}\" to {}", command, name); + // Clone for the move - the server uses an Arc internally let server = server.clone(); let plugin = Arc::clone(plugin); diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 78ae593..4ba2531 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -1,7 +1,6 @@ extern crate reqwest; extern crate serde; extern crate serde_json; -extern crate regex; use std::io::Read; use std::num::ParseFloatError; -- cgit v1.2.3-70-g09d2 From 4c18f59ac4dfe1372374ecc63a5976ad58a5e272 Mon Sep 17 00:00:00 2001 From: Jokler Date: Fri, 3 Nov 2017 01:14:37 +0100 Subject: Add systemd service --- systemd/frippy.service | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 systemd/frippy.service diff --git a/systemd/frippy.service b/systemd/frippy.service new file mode 100644 index 0000000..1fb6cd7 --- /dev/null +++ b/systemd/frippy.service @@ -0,0 +1,15 @@ +[Unit] +Description=IRC Bot written in Rust +After=network-online.target + +[Service] +Type=simple +User=frippy +Group=frippy +WorkingDirectory=/etc/frippy +ExecStart=/usr/bin/frippy +Restart=always +RestartSec=30 + +[Install] +WantedBy=multi-user.target -- cgit v1.2.3-70-g09d2 From e591ce43d3f9b85572f7ca179799fd6c3191f3ab Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 16 Nov 2017 02:36:12 +0100 Subject: Remove mutexes around plugins Mutexes are supposed to only mark critical sections so locking the entire plugin is too general. Any plugins that need mutable data should use mutexes internally instead. --- src/plugin.rs | 28 +++++++++------------------- src/plugins/currency.rs | 4 ++-- src/plugins/emoji.rs | 4 ++-- src/plugins/help.rs | 4 ++-- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/plugin.rs b/src/plugin.rs index fb35668..d1f849a 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -1,15 +1,15 @@ use std::fmt; use std::collections::HashMap; use std::thread::spawn; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use irc::client::prelude::*; use irc::error::Error as IrcError; pub trait Plugin: PluginName + Send + Sync + fmt::Debug { fn is_allowed(&self, server: &IrcServer, message: &Message) -> bool; - fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError>; - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>; + fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError>; + fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError>; } pub trait PluginName: Send + Sync + fmt::Debug { @@ -65,19 +65,9 @@ impl PluginCommand { } } -// Lock the mutex and ignore if it is poisoned -macro_rules! lock_plugin { - ($e:expr) => { - match $e.lock() { - Ok(plugin) => plugin, - Err(poisoned) => poisoned.into_inner(), - } - } -} - #[derive(Clone, Debug)] pub struct ThreadedPlugins { - plugins: HashMap>>, + plugins: HashMap>, } impl ThreadedPlugins { @@ -87,7 +77,7 @@ impl ThreadedPlugins { pub fn add(&mut self, plugin: T) { let name = plugin.name().to_lowercase(); - let safe_plugin = Arc::new(Mutex::new(plugin)); + let safe_plugin = Arc::new(plugin); self.plugins.insert(name, safe_plugin); } @@ -96,7 +86,7 @@ impl ThreadedPlugins { for (name, plugin) in self.plugins.clone() { // Send the message to the plugin if the plugin needs it - if lock_plugin!(plugin).is_allowed(server, &message) { + if plugin.is_allowed(server, &message) { debug!("Executing {} with {}", name, @@ -110,7 +100,7 @@ impl ThreadedPlugins { // Execute the plugin in another thread spawn(move || { - if let Err(e) = lock_plugin!(plugin).execute(&server, &message) { + if let Err(e) = plugin.execute(&server, &message) { error!("Error in {} - {}", name, e); }; }); @@ -140,7 +130,7 @@ impl ThreadedPlugins { let server = server.clone(); let plugin = Arc::clone(plugin); spawn(move || { - if let Err(e) = lock_plugin!(plugin).command(&server, command) { + if let Err(e) = plugin.command(&server, command) { error!("Error in {} command - {}", name, e); }; }); @@ -162,7 +152,7 @@ impl fmt::Display for ThreadedPlugins { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let plugin_names = self.plugins .iter() - .map(|(_, p)| lock_plugin!(p).name().to_string()) + .map(|(_, p)| p.name().to_string()) .collect::>(); write!(f, "{}", plugin_names.join(", ")) } diff --git a/src/plugins/currency.rs b/src/plugins/currency.rs index 4ba2531..d6cf928 100644 --- a/src/plugins/currency.rs +++ b/src/plugins/currency.rs @@ -130,11 +130,11 @@ impl Plugin for Currency { false } - fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { + fn execute(&self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { panic!("Currency does not implement the execute function!") } - fn command(&mut self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, server: &IrcServer, mut command: PluginCommand) -> Result<(), IrcError> { if command.tokens.is_empty() { return self.invalid_command(server, &command); diff --git a/src/plugins/emoji.rs b/src/plugins/emoji.rs index 09d5c27..1bb714c 100644 --- a/src/plugins/emoji.rs +++ b/src/plugins/emoji.rs @@ -105,7 +105,7 @@ impl Plugin for Emoji { } } - fn execute(&mut self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { + fn execute(&self, server: &IrcServer, message: &Message) -> Result<(), IrcError> { match message.command { Command::PRIVMSG(_, ref content) => { self.emoji(server, content, message.response_target().unwrap()) @@ -114,7 +114,7 @@ impl Plugin for Emoji { } } - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { server.send_notice(&command.source, "This Plugin does not implement any commands.") } diff --git a/src/plugins/help.rs b/src/plugins/help.rs index c4ddcd4..8f3fb4d 100644 --- a/src/plugins/help.rs +++ b/src/plugins/help.rs @@ -21,11 +21,11 @@ impl Plugin for Help { false } - fn execute(&mut self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { + fn execute(&self, _: &IrcServer, _: &Message) -> Result<(), IrcError> { panic!("Help does not implement the execute function!") } - fn command(&mut self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { + fn command(&self, server: &IrcServer, command: PluginCommand) -> Result<(), IrcError> { self.help(server, command) } } -- cgit v1.2.3-70-g09d2 From b8c25860ad769a466e7c50bbbb51b653dd55ccfb Mon Sep 17 00:00:00 2001 From: Jokler Date: Thu, 16 Nov 2017 02:51:51 +0100 Subject: Bump version to 0.3.1 --- Cargo.lock | 2 +- Cargo.toml | 2 +- configs/config.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8fe0acc..a721a1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,7 +274,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "frippy" -version = "0.3.0" +version = "0.3.1" dependencies = [ "clippy 0.0.166 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index 4c6928e..eecfa86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "frippy" -version = "0.3.0" +version = "0.3.1" authors = ["Jokler "] repository = "https://github.com/Mavulp/frippy" readme = "README.md" diff --git a/configs/config.toml b/configs/config.toml index 510e1ae..5ad66e2 100644 --- a/configs/config.toml +++ b/configs/config.toml @@ -12,7 +12,7 @@ encoding = "UTF-8" channels = ["#frippy"] umodes = "+B" user_info = "IRC Bot" -version = "frippy v0.3.0" +version = "frippy v0.3.1" source = "https://github.com/Mavulp/frippy" #ping_time = 180 #ping_timeout = 10 -- cgit v1.2.3-70-g09d2