From f1a8f3ad61374f80b9f925e9aadb91d80653b4a2 Mon Sep 17 00:00:00 2001 From: David Ross Date: Thu, 23 Mar 2023 00:01:31 -0700 Subject: [PATCH 1/3] Reformat code --- rustfmt.toml | 6 ------ src/lib.rs | 7 ++++--- src/log_impl.rs | 5 ++++- 3 files changed, 8 insertions(+), 10 deletions(-) delete mode 100644 rustfmt.toml diff --git a/rustfmt.toml b/rustfmt.toml deleted file mode 100644 index 994dd04..0000000 --- a/rustfmt.toml +++ /dev/null @@ -1,6 +0,0 @@ -condense_wildcard_suffixes = true -wrap_comments = true -format_code_in_doc_comments = true -merge_imports = true -edition = "2018" -version = "One" diff --git a/src/lib.rs b/src/lib.rs index f78df14..5f215c8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -394,9 +394,10 @@ pub fn log_reopen(path: &Path, signal: Option) -> io::Result>(path: &Path, signals: S) - -> io::Result> -{ +pub fn log_reopen1>( + path: &Path, + signals: S, +) -> io::Result> { let p = path.to_owned(); let r = reopen1::Reopen::new(Box::new(move || log_file(&p)))?; diff --git a/src/log_impl.rs b/src/log_impl.rs index d026df4..ce27342 100644 --- a/src/log_impl.rs +++ b/src/log_impl.rs @@ -644,7 +644,10 @@ impl Log for Sender { fn flush(&self) {} } -#[cfg(all(not(windows), any(feature = "syslog-3", feature = "syslog-4", feature = "syslog-6")))] +#[cfg(all( + not(windows), + any(feature = "syslog-3", feature = "syslog-4", feature = "syslog-6") +))] macro_rules! send_syslog { ($logger:expr, $level:expr, $message:expr) => { use log::Level; From f752256278a5e744ee2b0b73297130566cf81fc5 Mon Sep 17 00:00:00 2001 From: David Ross Date: Thu, 23 Mar 2023 00:03:17 -0700 Subject: [PATCH 2/3] Fix clippy warnings. --- examples/pretty-colored.rs | 2 +- src/builders.rs | 26 +++++++++----------------- src/colors.rs | 7 ++----- src/errors.rs | 2 -- src/lib.rs | 14 ++++++++++---- src/log_impl.rs | 26 +++++++------------------- tests/file_logging.rs | 6 +++--- tests/global_logging.rs | 20 +++++++------------- tests/meta_logging.rs | 2 +- tests/reopen_logging.rs | 4 ++-- 10 files changed, 42 insertions(+), 67 deletions(-) diff --git a/examples/pretty-colored.rs b/examples/pretty-colored.rs index 566de7f..8aee0b6 100644 --- a/examples/pretty-colored.rs +++ b/examples/pretty-colored.rs @@ -55,7 +55,7 @@ fn set_up_logging() { // configure colors for the name of the level. // since almost all of them are the same as the color for the whole line, we // just clone `colors_line` and overwrite our changes - let colors_level = colors_line.clone().info(Color::Green); + let colors_level = colors_line.info(Color::Green); // here we set up our fern Dispatch fern::Dispatch::new() .format(move |out, message, record| { diff --git a/src/builders.rs b/src/builders.rs index 1fe5136..55bb8d4 100644 --- a/src/builders.rs +++ b/src/builders.rs @@ -19,10 +19,10 @@ use crate::{log_impl, Filter, FormatCallback, Formatter}; use crate::log_impl::DateBasedState; #[cfg(all(not(windows), feature = "syslog-4"))] -use crate::{Syslog4Rfc3164Logger, Syslog4Rfc5424Logger}; +use crate::{Syslog4Rfc3164Logger, Syslog4Rfc5424Logger, Syslog4TransformFn}; #[cfg(all(not(windows), feature = "syslog-6"))] -use crate::{Syslog6Rfc3164Logger, Syslog6Rfc5424Logger}; +use crate::{Syslog6Rfc3164Logger, Syslog6Rfc5424Logger, Syslog6TransformFn}; /// The base dispatch logger. /// @@ -294,7 +294,7 @@ impl Dispatch { .levels .iter() .enumerate() - .find(|&(_, &(ref name, _))| name == &module) + .find(|(_, (name, _))| *name == module) { self.levels.remove(index); } @@ -576,11 +576,11 @@ impl Dispatch { filters.shrink_to_fit(); let dispatch = log_impl::Dispatch { - output: output, - default_level: default_level, + output, + default_level, levels: levels.into(), - format: format, - filters: filters, + format, + filters, }; (real_min, dispatch) @@ -696,11 +696,7 @@ enum OutputInner { #[cfg(all(not(windows), feature = "syslog-4"))] Syslog4Rfc5424 { logger: Syslog4Rfc5424Logger, - transform: Box< - dyn Fn(&log::Record) -> (i32, HashMap>, String) - + Sync - + Send, - >, + transform: Box, }, #[cfg(all(not(windows), feature = "syslog-6"))] Syslog6Rfc3164(Syslog6Rfc3164Logger), @@ -708,11 +704,7 @@ enum OutputInner { #[cfg(all(not(windows), feature = "syslog-6"))] Syslog6Rfc5424 { logger: Syslog6Rfc5424Logger, - transform: Box< - dyn Fn(&log::Record) -> (u32, HashMap>, String) - + Sync - + Send, - >, + transform: Box, }, /// Panics with messages text for all messages. Panic, diff --git a/src/colors.rs b/src/colors.rs index 33e7b70..5824a0e 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -273,10 +273,7 @@ impl Default for ColoredLevelConfig { impl ColoredLogLevel for Level { fn colored(&self, color: Color) -> WithFgColor { - WithFgColor { - text: *self, - color: color, - } + WithFgColor { text: *self, color } } } @@ -314,7 +311,7 @@ mod test { "{}", WithFgColor { text: "test", - color: color, + color, } ) ); diff --git a/src/errors.rs b/src/errors.rs index d33752c..ffaebe0 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,7 +1,5 @@ use std::{error, fmt, io}; -use log; - /// Convenience error combining possible errors which could occur while /// initializing logging. /// diff --git a/src/lib.rs b/src/lib.rs index 5f215c8..419217b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -260,7 +260,7 @@ use std::{ path::Path, }; -#[cfg(all(not(windows), feature = "syslog-4"))] +#[cfg(all(not(windows), any(feature = "syslog-4", feature = "syslog-6")))] use std::collections::HashMap; pub use crate::{ @@ -316,6 +316,14 @@ type Syslog6Rfc3164Logger = syslog6::Logger; +#[cfg(all(not(windows), feature = "syslog-4"))] +type Syslog4TransformFn = + dyn Fn(&log::Record) -> (i32, HashMap>, String) + Send + Sync; + +#[cfg(all(not(windows), feature = "syslog-6"))] +type Syslog6TransformFn = + dyn Fn(&log::Record) -> (u32, HashMap>, String) + Send + Sync; + /// Convenience method for opening a log file with common options. /// /// Equivalent to: @@ -367,9 +375,7 @@ pub fn log_reopen(path: &Path, signal: Option) -> io::Result, - pub transform: Box< - dyn Fn(&log::Record) -> (i32, HashMap>, String) - + Sync - + Send, - >, + pub transform: Box, } #[cfg(all(not(windows), feature = "syslog-6"))] @@ -155,11 +147,7 @@ pub struct Syslog6Rfc3164 { #[cfg(all(not(windows), feature = "syslog-6"))] pub struct Syslog6Rfc5424 { pub inner: Mutex, - pub transform: Box< - dyn Fn(&log::Record) -> (u32, HashMap>, String) - + Sync - + Send, - >, + pub transform: Box, } pub struct Panic; @@ -256,7 +244,7 @@ impl DateBasedConfig { } pub fn open_current_log_file(&self, suffix: &str) -> io::Result { - Self::open_log_file(&self.compute_file_path(&suffix)) + Self::open_log_file(&self.compute_file_path(suffix)) } } @@ -317,8 +305,8 @@ impl LevelConfiguration { LevelConfiguration::JustDefault => None, LevelConfiguration::Minimal(ref levels) => levels .iter() - .find(|&&(ref test_module, _)| test_module == module) - .map(|&(_, level)| level), + .find(|(test_module, _)| test_module == module) + .map(|(_, level)| *level), LevelConfiguration::Many(ref levels) => levels.get(module).cloned(), } } diff --git a/tests/file_logging.rs b/tests/file_logging.rs index f4f5286..12a5f7a 100644 --- a/tests/file_logging.rs +++ b/tests/file_logging.rs @@ -34,7 +34,7 @@ fn test_basic_logging_file_logging() { { let result = { - let mut log_read = fs::File::open(&temp_log_dir.path().join("test.log")).unwrap(); + let mut log_read = fs::File::open(temp_log_dir.path().join("test.log")).unwrap(); let mut buf = String::new(); log_read.read_to_string(&mut buf).unwrap(); buf @@ -80,7 +80,7 @@ fn test_custom_line_separators() { // default log level is 'trace' if not specified (logs all messages) // output to the log file with the "\r\n" line separator. .chain(fern::Output::file( - fern::log_file(&log_file).expect("Failed to open log file"), + fern::log_file(log_file).expect("Failed to open log file"), "\r\n", )) .into_log(); @@ -95,7 +95,7 @@ fn test_custom_line_separators() { { let result = { let mut log_read = - fs::File::open(&temp_log_dir.path().join("test_custom_line_sep.log")).unwrap(); + fs::File::open(temp_log_dir.path().join("test_custom_line_sep.log")).unwrap(); let mut buf = String::new(); log_read.read_to_string(&mut buf).unwrap(); buf diff --git a/tests/global_logging.rs b/tests/global_logging.rs index cc5c894..ee3f07e 100644 --- a/tests/global_logging.rs +++ b/tests/global_logging.rs @@ -22,15 +22,15 @@ impl LogVerify { let formatted_message = format!("{}", record.args()); match &*formatted_message { "[INFO] Test information message" => { - assert_eq!(self.info, false, "expected only one info message"); + assert!(!self.info, "expected only one info message"); self.info = true; } "[WARN] Test warning message" => { - assert_eq!(self.warn, false, "expected only one warn message"); + assert!(!self.warn, "expected only one warn message"); self.warn = true; } "[ERROR] Test error message" => { - assert_eq!(self.error, false, "expected only one error message"); + assert!(!self.error, "expected only one error message"); self.error = true; } other => panic!("unexpected message: '{}'", other), @@ -85,16 +85,10 @@ fn test_global_logger() { log::logger().flush(); let verify_acquired = verify.0.lock().unwrap(); - assert_eq!( - verify_acquired.info, true, - "expected info message to be received" - ); - assert_eq!( - verify_acquired.warn, true, - "expected warn message to be received" - ); - assert_eq!( - verify_acquired.error, true, + assert!(verify_acquired.info, "expected info message to be received"); + assert!(verify_acquired.warn, "expected warn message to be received"); + assert!( + verify_acquired.error, "expected error message to be received" ); } diff --git a/tests/meta_logging.rs b/tests/meta_logging.rs index cce793a..b84c240 100644 --- a/tests/meta_logging.rs +++ b/tests/meta_logging.rs @@ -65,7 +65,7 @@ fn file_deadlock() { { let contents = { - let mut log_read = fs::File::open(&temp_log_dir.path().join("test.log")).unwrap(); + let mut log_read = fs::File::open(temp_log_dir.path().join("test.log")).unwrap(); let mut buf = String::new(); log_read.read_to_string(&mut buf).unwrap(); buf diff --git a/tests/reopen_logging.rs b/tests/reopen_logging.rs index 7c86592..4d86a5d 100644 --- a/tests/reopen_logging.rs +++ b/tests/reopen_logging.rs @@ -35,7 +35,7 @@ fn test_basic_logging_reopen_logging() { { let result = { - let mut log_read = fs::File::open(&temp_log_dir.path().join("test.log")).unwrap(); + let mut log_read = fs::File::open(temp_log_dir.path().join("test.log")).unwrap(); let mut buf = String::new(); log_read.read_to_string(&mut buf).unwrap(); buf @@ -96,7 +96,7 @@ fn test_custom_line_separators() { { let result = { let mut log_read = - fs::File::open(&temp_log_dir.path().join("test_custom_line_sep.log")).unwrap(); + fs::File::open(temp_log_dir.path().join("test_custom_line_sep.log")).unwrap(); let mut buf = String::new(); log_read.read_to_string(&mut buf).unwrap(); buf From 32a4d909498a904e2417fe90a94e00b9450c0f56 Mon Sep 17 00:00:00 2001 From: David Ross Date: Thu, 23 Mar 2023 02:22:05 -0700 Subject: [PATCH 3/3] Add CI task for checking format,clippy --- .github/workflows/rust.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 554f849..065ab54 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -169,3 +169,15 @@ jobs: toolchain: 1.59.0 override: true - run: cargo build --features syslog-6 + fmt_and_clippy: + name: Optional Lints + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + - run: cargo fmt --check + - run: cargo clippy --all-features --all-targets -- -D warnings