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. --- src/plugin.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/plugin.rs') 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); -- 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(-) (limited to 'src/plugin.rs') 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