Skip to content

Commit

Permalink
Merge pull request #15 from SpriteOvO/fork-logger
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO authored Oct 12, 2022
2 parents d126efe + d04e7ee commit 6c13c37
Show file tree
Hide file tree
Showing 5 changed files with 283 additions and 65 deletions.
7 changes: 6 additions & 1 deletion spdlog/benches/compare_with_cpp_spdlog_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ use test::black_box;
use clap::Parser;
use once_cell::sync::Lazy;

use spdlog::{prelude::*, sink::*, Error, SendToChannelError, ThreadPool};
use spdlog::{
error::{Error, SendToChannelError},
prelude::*,
sink::*,
ThreadPool,
};

required_multi_thread_feature!();

Expand Down
7 changes: 6 additions & 1 deletion spdlog/benches/spdlog_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ use test::Bencher;

use once_cell::sync::Lazy;

use spdlog::{prelude::*, sink::*, Error, ErrorHandler, SendToChannelError, ThreadPool};
use spdlog::{
error::{Error, ErrorHandler, SendToChannelError},
prelude::*,
sink::*,
ThreadPool,
};

required_multi_thread_feature!();

Expand Down
55 changes: 38 additions & 17 deletions spdlog/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! Provides error types.
use std::{fmt, io, result};
use std::{
fmt::{self, Display},
io, result,
};

use atomic::Atomic;
use static_assertions::const_assert;
Expand All @@ -9,6 +12,8 @@ use thiserror::Error;
#[cfg(feature = "multi-thread")]
use crate::{sink::Task, RecordOwned};

pub use crate::env_level::EnvLevelError;

/// The error type of this crate.
#[derive(Error, Debug)]
#[non_exhaustive]
Expand Down Expand Up @@ -76,12 +81,15 @@ pub enum Error {
#[error("attempted to convert a string that doesn't match an existing log level: {0}")]
ParseLevel(String),

/// The variant returned by [`LoggerBuilder::build`] when an error occurs in
/// building a logger.
/// The variant returned if an error occurs in setting a invalid logger
/// name.
///
/// See the documentation of [`LoggerBuilder::name`] for the name
/// requirements.
///
/// [`LoggerBuilder::build`]: crate::LoggerBuilder::build
#[error("failed to build a logger: {0}")]
BuildLogger(BuildLoggerError),
/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
#[error("failed to set logger name: {0}")]
SetLoggerName(#[from] SetLoggerNameError),

/// The variant returned by [`Sink`]s when an error occurs in sending to the
/// channel.
Expand All @@ -92,18 +100,31 @@ pub enum Error {
SendToChannel(SendToChannelError, SendToChannelErrorDropped),
}

/// The more detailed error type of building a logger.
/// This error indicates that an invalid logger name was set.
///
/// See the documentation of [`LoggerBuilder::name`] for the name requirements.
///
/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum BuildLoggerError {
/// The name of the logger is invalid.
///
/// See the documentation of [`LoggerBuilder::name`] for the name
/// requirements.
///
/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
#[error("invalid logger name: {0}")]
InvalidName(String),
pub struct SetLoggerNameError {
name: String,
}

impl SetLoggerNameError {
pub(crate) fn new(name: impl Into<String>) -> Self {
Self { name: name.into() }
}

#[cfg(test)]
pub(crate) fn name(&self) -> &str {
&self.name
}
}

impl Display for SetLoggerNameError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "name '{}' contains disallowed characters", self.name)
}
}

/// The more detailed error type of sending to channel.
Expand Down
20 changes: 7 additions & 13 deletions spdlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
#![warn(missing_docs)]

mod env_level;
mod error;
pub mod error;
pub mod formatter;
mod level;
#[cfg(feature = "log")]
Expand All @@ -215,8 +215,7 @@ mod test_utils;
mod thread_pool;
mod utils;

pub use env_level::EnvLevelError;
pub use error::*;
pub use error::{Error, ErrorHandler, Result};
pub use level::*;
#[cfg(feature = "log")]
pub use log_crate_proxy::*;
Expand All @@ -242,6 +241,7 @@ use std::{

use cfg_if::cfg_if;

use error::EnvLevelError;
use sink::{
Sink, {StdStream, StdStreamSink},
};
Expand Down Expand Up @@ -523,16 +523,10 @@ pub fn set_default_logger(logger: Arc<Logger>) {
///
/// ```
/// # std::env::set_var("SPDLOG_RS_LEVEL", "network=Warn,network=Warn");
/// let result = spdlog::init_env_level();
/// # // TODO: commented out since `assert_matches` is currently unstable.
/// # // change to use `assert_matches` when it is stable.
/// # // assert_matches!(result, Err(spdlog::EnvLevelError::ParseEnvVar(_)));
/// if let Err(spdlog::EnvLevelError::ParseEnvVar(_)) = result {
/// // expected
/// } else {
/// // unexpected
/// assert!(false);
/// }
/// assert!(matches!(
/// spdlog::init_env_level(),
/// Err(spdlog::error::EnvLevelError::ParseEnvVar(_))
/// ));
/// ```
pub fn init_env_level() -> StdResult<bool, EnvLevelError> {
init_env_level_from("SPDLOG_RS_LEVEL")
Expand Down
Loading

0 comments on commit 6c13c37

Please sign in to comment.