From 54a3cc088071f37cdaacbb2a173542894521a397 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Fri, 28 Aug 2020 00:13:01 +0000 Subject: [PATCH] Add basic logging to the dice bot. --- Cargo.lock | 59 ++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/bin/dicebot.rs | 3 +++ src/bot.rs | 15 +++++++----- src/commands.rs | 4 ++-- 5 files changed, 75 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a368fc1..bd7d62f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,6 +95,17 @@ dependencies = [ "autocfg", ] +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi 0.3.9", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -160,7 +171,9 @@ version = "0.3.0" dependencies = [ "async-trait", "dirs", + "env_logger", "itertools", + "log", "matrix-sdk", "matrix-sdk-base 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)", "matrix-sdk-common 0.1.0 (git+https://github.com/matrix-org/matrix-rust-sdk?rev=0.1.0)", @@ -308,6 +321,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "fnv" version = "1.0.7" @@ -587,6 +613,15 @@ version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +[[package]] +name = "humantime" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" +dependencies = [ + "quick-error", +] + [[package]] name = "hyper" version = "0.13.7" @@ -1141,6 +1176,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.7" @@ -1795,6 +1836,15 @@ dependencies = [ "winapi 0.3.9", ] +[[package]] +name = "termcolor" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f" +dependencies = [ + "winapi-util", +] + [[package]] name = "thiserror" version = "1.0.20" @@ -2161,6 +2211,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 7c3322f..706c1e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,8 @@ keywords = ["games", "dice", "matrix", "bot"] categories = ["games"] [dependencies] +log = "0.4" +env_logger = "0.7" toml = "0.5" nom = "5" rand = "0.7" diff --git a/src/bin/dicebot.rs b/src/bin/dicebot.rs index 51875ec..9e04713 100644 --- a/src/bin/dicebot.rs +++ b/src/bin/dicebot.rs @@ -1,5 +1,6 @@ use chronicle_dicebot::bot::run_bot; use chronicle_dicebot::bot::Config; +use env_logger::Env; use std::fs; use std::path::PathBuf; @@ -15,6 +16,8 @@ fn read_config>(config_path: P) -> Result Result<(), Box> { + env_logger::from_env(Env::default().default_filter_or("chronicle_dicebot=info")).init(); + let config_path = std::env::args() .skip(1) .next() diff --git a/src/bot.rs b/src/bot.rs index 5bc70ef..d13440a 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -1,5 +1,6 @@ use crate::commands::parse_command; use dirs; +use log::{error, info, warn}; use matrix_sdk::{ self, events::{ @@ -66,10 +67,10 @@ impl EventEmitter for DiceBot { } let room = room.read().await; - println!("Autojoining room {}", room.display_name()); + info!("Autojoining room {}", room.display_name()); match self.client.join_room_by_id(&room.room_id).await { - Err(e) => println!("Could not join room: {}", e.to_string()), + Err(e) => warn!("Could not join room: {}", e.to_string()), _ => (), } } @@ -110,12 +111,14 @@ impl EventEmitter for DiceBot { NoticeMessageEventContent::html(plain, html), )); + info!("{} executed: {}", sender_username, msg_body); + //we clone here to hold the lock for as little time as possible. let room_id = room.read().await.room_id.clone(); let result = self.client.room_send(&room_id, content, None).await; match result { - Err(e) => println!("Error sending message: {}", e.to_string()), + Err(e) => error!("Error sending message: {}", e.to_string()), Ok(_) => (), } } @@ -157,10 +160,10 @@ pub async fn run_bot(config: MatrixConfig) -> Result<(), Box Result<(), Box Result>, String> { match parser::parse_command(s) { - //The first clause prevents bot from spamming messages to itself - //after executing a previous command. Ok((input, result)) => match (input, &result) { + //This clause prevents bot from spamming messages to itself + //after executing a previous command. ("", Some(_)) | (_, None) => Ok(result), _ => Err(format!("{}: malformed dice expression", s)), },