From 796084d13e8a1c3e333721ba48091a7ecee9c352 Mon Sep 17 00:00:00 2001 From: MedzikUser Date: Thu, 28 Jul 2022 22:31:10 +0200 Subject: [PATCH] chore: some changes - add macros for Mutex (currently unused) - change logger to tracing --- Cargo.lock | 126 ++++++++++++++++++++++++++--------- Cargo.toml | 5 +- src/lib.rs | 1 + src/macros.rs | 39 +++++++++++ src/main.rs | 19 +----- src/plugins/loader.rs | 18 +++-- src/tcp/client.rs | 6 +- src/tcp/handle_connection.rs | 2 +- src/tcp/handle_websocket.rs | 2 +- 9 files changed, 156 insertions(+), 62 deletions(-) create mode 100644 src/macros.rs diff --git a/Cargo.lock b/Cargo.lock index c3c9808..4d37bff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.58" @@ -348,6 +357,12 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.126" @@ -416,15 +431,6 @@ dependencies = [ "libc", ] -[[package]] -name = "num_threads" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44" -dependencies = [ - "libc", -] - [[package]] name = "object" version = "0.28.4" @@ -565,11 +571,12 @@ dependencies = [ "better-panic", "clap", "futures-util", + "lazy_static", "libloading", - "log", - "simplelog", "tokio", "tokio-tungstenite", + "tracing", + "tracing-subscriber", "tungstenite", ] @@ -585,14 +592,12 @@ dependencies = [ ] [[package]] -name = "simplelog" -version = "0.12.0" +name = "sharded-slab" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48dfff04aade74dd495b007c831cd6f4e0cee19c344dd9dc0884c0289b70a786" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" dependencies = [ - "log", - "termcolor", - "time", + "lazy_static", ] [[package]] @@ -601,6 +606,12 @@ version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32" +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + [[package]] name = "socket2" version = "0.4.4" @@ -674,23 +685,14 @@ dependencies = [ ] [[package]] -name = "time" -version = "0.3.11" +name = "thread_local" +version = "1.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72c91f41dcb2f096c05f0873d667dceec1087ce5bcf984ec8ffb19acddbb3217" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" dependencies = [ - "itoa", - "libc", - "num_threads", - "time-macros", + "once_cell", ] -[[package]] -name = "time-macros" -version = "0.2.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "42657b1a6f4d817cda8e7a0ace261fe0cc946cf3a80314390b22cc61ae080792" - [[package]] name = "tinyvec" version = "1.6.0" @@ -748,6 +750,64 @@ dependencies = [ "tungstenite", ] +[[package]] +name = "tracing" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a400e31aa60b9d44a52a8ee0343b5b18566b03a8321e0d321f695cf56e940160" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c75893af559bc8e10716548bdef5cb2b983f8e637db9d0e15126b61b484ee2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b7358be39f2f274f322d2aaed611acc57f382e8eb1e5b48cb9ae30933495ce7" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b" +dependencies = [ + "ansi_term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + [[package]] name = "tungstenite" version = "0.17.3" @@ -812,6 +872,12 @@ version = "0.7.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/Cargo.toml b/Cargo.toml index 6d3ca76..3fb2364 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,14 +17,15 @@ opt-level = 'z' codegen-units = 1 [dependencies] +lazy_static = "1.4.0" anyhow = "1.0.58" async-trait = "0.1.56" better-panic = "0.3.0" libloading = "0.7.3" -simplelog = "0.12.0" tokio-tungstenite = "0.17.2" tungstenite = "0.17.3" clap = { version = "3.2.15", features = ["derive"] } futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] } -log = { version = "0.4.17", features = ["release_max_level_info", "max_level_debug"] } tokio = { version = "1.20.1", features = ["rt-multi-thread", "macros", "net"] } +tracing-subscriber = "0.3.15" +tracing = "0.1.35" diff --git a/src/lib.rs b/src/lib.rs index f6bdd6c..2b80c0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,3 +25,4 @@ pub mod commands; pub mod plugins; pub mod tcp; +mod macros; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..097a38d --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,39 @@ +/// Update value in the Mutex +/// +/// ``` +/// use std::sync::Mutex; +/// use servers::update_mutex; +/// +/// // create a new Mutex +/// let mutex: Mutex = Mutex::new(String::new()); +/// +/// // update the value in the Mutex +/// update_mutex!(mutex, "new value".to_string()); +/// ``` +#[macro_export] +macro_rules! update_mutex { + ($mutex: expr, $($new_value:tt)+) => { + *$crate::lock_mutex!($mutex) = $($new_value)+; + }; +} + +/// Lock value in the Mutex +/// +/// ``` +/// use std::sync::Mutex; +/// use servers::lock_mutex; +/// +/// // create a new Mutex +/// let mutex: Mutex = Mutex::new("value".to_string()); +/// +/// // lock the Mutex +/// let value = lock_mutex!(mutex); +/// +/// println!("{}", value); +/// ``` +#[macro_export] +macro_rules! lock_mutex { + ($mutex: expr) => { + $mutex.lock().expect("failed to lock mutex") + }; +} diff --git a/src/main.rs b/src/main.rs index 6f9451b..bf95d45 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,10 @@ -use std::fs::File; - use clap::Parser; -use log::{error, info, LevelFilter}; use servers::{ plugins::loader, tcp::{handle_connection, handle_websocket, Client}, }; -use simplelog::{ColorChoice, CombinedLogger, Config, TermLogger, TerminalMode, WriteLogger}; use tokio::net::TcpListener; +use tracing::{info, error}; #[derive(Parser)] #[clap( @@ -54,19 +51,7 @@ async fn main() -> anyhow::Result<()> { // init better panic better_panic::install(); // init logger - CombinedLogger::init(vec![ - TermLogger::new( - LevelFilter::Trace, - Config::default(), - TerminalMode::Mixed, - ColorChoice::Auto, - ), - WriteLogger::new( - LevelFilter::Debug, - Config::default(), - File::create("server.log").unwrap(), - ), - ])?; + tracing_subscriber::fmt().init(); // parse cli args let cli = Cli::parse(); diff --git a/src/plugins/loader.rs b/src/plugins/loader.rs index 96e1e40..ff6ef1b 100644 --- a/src/plugins/loader.rs +++ b/src/plugins/loader.rs @@ -1,7 +1,7 @@ -use std::{fs, sync::Arc}; +use std::{fs, sync::Arc, path::Path}; use libloading::{Library, Symbol}; -use log::{debug, trace}; +use tracing::{debug, trace}; use crate::{commands, plugins::Registrar}; @@ -9,8 +9,14 @@ use super::{PluginManager, PluginManagerType}; /// Plugins and Commands loader pub fn loader() -> anyhow::Result { - // get path to .so lib from command argument let config_dir = "./plugins"; + + // if config directory doesn't exists, create it + if !Path::new(config_dir).exists() { + fs::create_dir_all(config_dir)?; + } + + // get path to .so lib from command argument let paths = fs::read_dir(config_dir)?; // create a plugin manager @@ -25,13 +31,11 @@ pub fn loader() -> anyhow::Result { for path in paths { // get library file path let path = path?.path(); + let plugin_path = path.to_str().unwrap(); - let plugin_path = path.to_str().unwrap_or("unknown"); - - // log debug info debug!("Loading plugin `{}`", plugin_path); - // loading library with .so is unsafe + // loading library from .so is unsafe unsafe { // load library // Box::new and Box::leak must be there because if it isn't there it throws a segmentation fault diff --git a/src/tcp/client.rs b/src/tcp/client.rs index 56555ab..875ec66 100644 --- a/src/tcp/client.rs +++ b/src/tcp/client.rs @@ -1,5 +1,3 @@ -#![allow(clippy::unused_io_amount)] - use tokio::{net::TcpStream, io::{self, AsyncWriteExt, AsyncReadExt}}; /// Max size of a TCP packet @@ -17,7 +15,7 @@ impl Client { Self { stream } } - /// Read message/buffer from client + /// Read message/buffer from the client pub async fn read(&mut self) -> anyhow::Result { // allocate an empty buffer let mut buf = [0; MAX_PACKET_LEN]; @@ -34,7 +32,7 @@ impl Client { Ok(decoded) } - /// Send message to client + /// Send message to the client pub async fn send(&mut self, content: &str) -> io::Result<()> { // add a new line at the end of the content let content = format!("{content}\n\r"); diff --git a/src/tcp/handle_connection.rs b/src/tcp/handle_connection.rs index fef8e85..c499791 100644 --- a/src/tcp/handle_connection.rs +++ b/src/tcp/handle_connection.rs @@ -1,4 +1,4 @@ -use log::{error, info, trace}; +use tracing::{error, info, trace}; use tokio::io::AsyncWriteExt; use crate::plugins::PluginManagerType; diff --git a/src/tcp/handle_websocket.rs b/src/tcp/handle_websocket.rs index 2e6898e..d5d9c15 100644 --- a/src/tcp/handle_websocket.rs +++ b/src/tcp/handle_websocket.rs @@ -4,7 +4,7 @@ use futures_util::{ stream::{SplitSink, SplitStream}, SinkExt, StreamExt, }; -use log::info; +use tracing::info; use tokio::{ io::{AsyncReadExt, AsyncWriteExt}, net::{