refactor(log): apply clippy findings
cargo devel CI / cargo CI (push) Has been cancelled Details

This commit is contained in:
Christoph J. Scherr 2024-07-09 18:22:41 +02:00
parent 511fd69ab6
commit 9b94c25234
1 changed files with 30 additions and 27 deletions

View File

@ -13,6 +13,7 @@
//! - [`tracing`]: base logging crate //! - [`tracing`]: base logging crate
//! - [`tracing_appender`]: Used to log to files //! - [`tracing_appender`]: Used to log to files
//! - [`tracing_subscriber`]: Used to do actual logging, formatting, to stdout //! - [`tracing_subscriber`]: Used to do actual logging, formatting, to stdout
#![warn(clippy::pedantic, clippy::style, clippy::nursery)]
use std::{ use std::{
fmt, fmt,
@ -21,7 +22,7 @@ use std::{
}; };
pub mod error; pub mod error;
use error::*; use error::Error;
pub use tracing; pub use tracing;
pub use tracing::{debug, error, info, trace, warn, Level}; pub use tracing::{debug, error, info, trace, warn, Level};
@ -56,6 +57,8 @@ static INITIALIZED: AtomicBool = AtomicBool::new(false);
/// ///
/// ``` /// ```
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[allow(clippy::struct_excessive_bools)] // it's just true/false values, not states, and I don't
// need to reinvent the wheel
pub struct LoggerBuilder { pub struct LoggerBuilder {
/// create and log to logfiles /// create and log to logfiles
log_to_file: bool, log_to_file: bool,
@ -179,7 +182,7 @@ impl LoggerBuilder {
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
} }
(true, false, false, _) => { (true, false, false, _) => {
let file_appender = tracing_appender::rolling::daily(self.log_dir.clone(), "log"); let file_appender = tracing_appender::rolling::daily(self.log_dir, "log");
let (file_writer, _guard) = tracing_appender::non_blocking(file_appender); let (file_writer, _guard) = tracing_appender::non_blocking(file_appender);
let subscriber = subscriber.with_writer(file_writer).without_time().finish(); let subscriber = subscriber.with_writer(file_writer).without_time().finish();
tracing::subscriber::set_global_default(subscriber)?; tracing::subscriber::set_global_default(subscriber)?;
@ -214,16 +217,18 @@ impl LoggerBuilder {
} }
/// enable or disable logging to and creating of logfiles /// enable or disable logging to and creating of logfiles
pub fn log_to_file(mut self, log_to_file: bool) -> Self { #[must_use]
pub const fn log_to_file(mut self, log_to_file: bool) -> Self {
self.log_to_file = log_to_file; self.log_to_file = log_to_file;
self self
} }
/// set a directory where logfiles would be created in /// set a directory where logfiles would be created in
/// ///
/// Enable or disable creation and logging to logfiles with [log_to_file](Self::log_to_file). /// Enable or disable creation and logging to logfiles with [`log_to_file`](Self::log_to_file).
/// ///
/// The default logdir is [DEFAULT_LOG_DIR]. /// The default logdir is [`DEFAULT_LOG_DIR`].
#[must_use]
pub fn log_dir(mut self, log_dir: PathBuf) -> Self { pub fn log_dir(mut self, log_dir: PathBuf) -> Self {
self.log_dir = log_dir; self.log_dir = log_dir;
self self
@ -235,67 +240,64 @@ impl LoggerBuilder {
/// are displayed by a program that does not interpret them. /// are displayed by a program that does not interpret them.
/// ///
/// Keeping ANSI control sequences enabled has the disadvantage of added colors for the logs. /// Keeping ANSI control sequences enabled has the disadvantage of added colors for the logs.
pub fn ansi(mut self, ansi: bool) -> Self { #[must_use]
pub const fn ansiconst(mut self, ansi: bool) -> Self {
self.ansi = ansi; self.ansi = ansi;
self self
} }
/// when making a log, display the source file in which a log was crated in /// when making a log, display the source file in which a log was crated in
pub fn display_filename(mut self, display_filename: bool) -> Self { #[must_use]
pub const fn display_filename(mut self, display_filename: bool) -> Self {
self.display_filename = display_filename; self.display_filename = display_filename;
self self
} }
/// when making a log, display the log level of the message /// when making a log, display the log level of the message
pub fn display_level(mut self, display_level: bool) -> Self { #[must_use]
pub const fn display_level(mut self, display_level: bool) -> Self {
self.display_level = display_level; self.display_level = display_level;
self self
} }
/// show target context /// show target context
pub fn display_target(mut self, display_target: bool) -> Self { #[must_use]
pub const fn display_target(mut self, display_target: bool) -> Self {
self.display_target = display_target; self.display_target = display_target;
self self
} }
/// set the maximum verbosity level.
pub fn max_level(mut self, max_level: Level) -> Self {
self.max_level = max_level;
self
}
/// show the id of the thread that created this message /// show the id of the thread that created this message
pub fn display_thread_ids(mut self, display_thread_ids: bool) -> Self { #[must_use]
pub const fn display_thread_ids(mut self, display_thread_ids: bool) -> Self {
self.display_thread_ids = display_thread_ids; self.display_thread_ids = display_thread_ids;
self self
} }
/// show the name of the thread that created this message /// show the name of the thread that created this message
pub fn display_thread_names(mut self, display_thread_names: bool) -> Self { #[must_use]
pub const fn display_thread_names(mut self, display_thread_names: bool) -> Self {
self.display_thread_names = display_thread_names; self.display_thread_names = display_thread_names;
self self
} }
/// show which line in the source file produces a log /// show which line in the source file produces a log
pub fn display_line_number(mut self, display_line_number: bool) -> Self { #[must_use]
pub const fn display_line_number(mut self, display_line_number: bool) -> Self {
self.display_line_number = display_line_number; self.display_line_number = display_line_number;
self self
} }
/// splits a log over multiple lines, looks like a python traceback /// splits a log over multiple lines, looks like a python traceback
pub fn pretty(mut self, pretty: bool) -> Self { #[must_use]
pub const fn pretty(mut self, pretty: bool) -> Self {
self.pretty = pretty; self.pretty = pretty;
self self
} }
/// show a timestamp describing when the log was created
pub fn show_time(mut self, show_time: bool) -> Self {
self.show_time = show_time;
self
}
/// show timestamps as uptime (duration since the logger was initialized) /// show timestamps as uptime (duration since the logger was initialized)
pub fn uptime(mut self, uptime: bool) -> Self { #[must_use]
pub const fn uptime(mut self, uptime: bool) -> Self {
self.uptime = uptime; self.uptime = uptime;
self self
} }
@ -365,7 +367,8 @@ pub struct Logger;
/// ## Main implementation /// ## Main implementation
impl Logger { impl Logger {
/// Get a new [LoggerBuilder] /// Get a new [`LoggerBuilder`]
#[must_use]
pub fn builder() -> LoggerBuilder { pub fn builder() -> LoggerBuilder {
LoggerBuilder::default() LoggerBuilder::default()
} }