@@ -13,97 +13,82 @@ use std::{
1313 sync:: Arc ,
1414} ;
1515
16- use thiserror:: Error ;
17-
1816pub use crate :: env_level:: EnvLevelError ;
1917#[ cfg( feature = "multi-thread" ) ]
2018use crate :: { sink:: Task , RecordOwned } ;
2119
2220/// Contains most errors of this crate.
23- #[ derive( Error , Debug ) ]
21+ #[ derive( Debug ) ]
2422#[ non_exhaustive]
2523pub enum Error {
2624 /// Returned by [`Formatter`]s when an error occurs in formatting a record.
2725 ///
2826 /// [`Formatter`]: crate::formatter::Formatter
29- #[ error( "format record error: {0}" ) ]
3027 FormatRecord ( fmt:: Error ) ,
3128
3229 /// Returned by [`Sink`]s when an error occurs in writing a record to the
3330 /// target.
3431 ///
3532 /// [`Sink`]: crate::sink::Sink
36- #[ error( "write record error: {0}" ) ]
3733 WriteRecord ( io:: Error ) ,
3834
3935 /// Returned by [`Sink`]s when an error occurs in flushing the buffer.
4036 ///
4137 /// [`Sink`]: crate::sink::Sink
42- #[ error( "flush buffer error: {0}" ) ]
4338 FlushBuffer ( io:: Error ) ,
4439
4540 /// Returned by [`Sink`]s when an error occurs in creating a directory.
4641 ///
4742 /// [`Sink`]: crate::sink::Sink
48- #[ error( "create directory error: {0}" ) ]
4943 CreateDirectory ( io:: Error ) ,
5044
5145 /// Returned by [`Sink`]s when an error occurs in opening a file.
5246 ///
5347 /// [`Sink`]: crate::sink::Sink
54- #[ error( "open file error: {0}" ) ]
5548 OpenFile ( io:: Error ) ,
5649
5750 /// Returned by [`Sink`]s when an error occurs in querying the metadata of a
5851 /// file.
5952 ///
6053 /// [`Sink`]: crate::sink::Sink
61- #[ error( "query file metadata error: {0}" ) ]
6254 QueryFileMetadata ( io:: Error ) ,
6355
6456 /// Returned by [`Sink`]s when an error occurs in renaming a file.
6557 ///
6658 /// [`Sink`]: crate::sink::Sink
67- #[ error( "rename file error: {0}" ) ]
6859 RenameFile ( io:: Error ) ,
6960
7061 /// Returned by [`Sink`]s when an error occurs in removing a file.
7162 ///
7263 /// [`Sink`]: crate::sink::Sink
73- #[ error( "remove file error: {0}" ) ]
7464 RemoveFile ( io:: Error ) ,
7565
7666 /// Returned by [`from_str`] when the string doesn't match any of the log
7767 /// levels.
7868 ///
7969 /// [`from_str`]: std::str::FromStr::from_str
80- #[ error( "attempted to convert a string that doesn't match an existing log level: {0}" ) ]
8170 ParseLevel ( String ) ,
8271
8372 /// Returned if an invalid argument was passed in.
84- #[ error( "invalid argument {0}" ) ]
85- InvalidArgument ( #[ from] InvalidArgumentError ) ,
73+ InvalidArgument ( InvalidArgumentError ) ,
8674
8775 /// Returned by [`Sink`]s when an error occurs in sending to the channel.
8876 ///
8977 /// [`Sink`]: crate::sink::Sink
9078 #[ cfg( feature = "multi-thread" ) ]
91- #[ error( "failed to send message to channel: {0}" ) ]
9279 SendToChannel ( SendToChannelError , SendToChannelErrorDropped ) ,
9380
9481 /// Returned by [`runtime_pattern!`] when the pattern is failed to be built
9582 /// at runtime.
9683 ///
9784 /// [`runtime_pattern!`]: crate::formatter::runtime_pattern
9885 #[ cfg( feature = "runtime-pattern" ) ]
99- #[ error( "failed to build pattern at runtime: {0}" ) ]
10086 BuildPattern ( BuildPatternError ) ,
10187
10288 /// Returned by [`Formatter`]s when an error occurs in serializing a log.
10389 ///
10490 /// [`Formatter`]: crate::formatter::Formatter
10591 #[ cfg( feature = "serde" ) ]
106- #[ error( "failed to serialize log: {0}" ) ]
10792 SerializeRecord ( io:: Error ) ,
10893
10994 /// Returned from a downstream implementation of `spdlog-rs`. Its actual
@@ -112,21 +97,55 @@ pub enum Error {
11297 /// When downstream crates encounter errors, other more specific error
11398 /// variants should be used first, this variant should only be used as a
11499 /// last option when other variant types are incompatible.
115- #[ error( "{0}" ) ]
116100 Downstream ( Box < dyn StdError + Send + Sync > ) ,
117101
118102 /// Returned when multiple errors occurred.
119- #[ error( "{0:?}" ) ]
120103 Multiple ( Vec < Error > ) ,
121104
122105 #[ cfg( test) ]
123106 #[ doc( hidden) ]
124- #[ error( "{0}" ) ]
125107 __ForInternalTestsUseOnly( i32 ) ,
126108}
127109
110+ impl StdError for Error { }
111+
112+ impl Display for Error {
113+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
114+ match self {
115+ Self :: FormatRecord ( err) => write ! ( f, "format record error: {err}" ) ,
116+ Self :: WriteRecord ( err) => write ! ( f, "write record error: {err}" ) ,
117+ Self :: FlushBuffer ( err) => write ! ( f, "flush buffer error: {err}" ) ,
118+ Self :: CreateDirectory ( err) => write ! ( f, "create directory error: {err}" ) ,
119+ Self :: OpenFile ( err) => write ! ( f, "open file error: {err}" ) ,
120+ Self :: QueryFileMetadata ( err) => write ! ( f, "query file metadata error: {err}" ) ,
121+ Self :: RenameFile ( err) => write ! ( f, "rename file error: {err}" ) ,
122+ Self :: RemoveFile ( err) => write ! ( f, "remove file error: {err}" ) ,
123+ Self :: ParseLevel ( level_str) => {
124+ write ! ( f, "attempted to convert a string that doesn't match an existing log level: {level_str}" )
125+ }
126+ Self :: InvalidArgument ( err) => write ! ( f, "invalid argument {err}" ) ,
127+ #[ cfg( feature = "multi-thread" ) ]
128+ Self :: SendToChannel ( err, _) => write ! ( f, "failed to send message to channel: {err}" ) ,
129+ #[ cfg( feature = "runtime-pattern" ) ]
130+ Self :: BuildPattern ( err) => write ! ( f, "failed to build pattern at runtime: {err}" ) ,
131+ #[ cfg( feature = "serde" ) ]
132+ Self :: SerializeRecord ( err) => write ! ( f, "failed to serialize log: {err}" ) ,
133+ Self :: Downstream ( err) => write ! ( f, "{err}" ) ,
134+ Self :: Multiple ( errs) => write ! ( f, "{errs:?}" ) ,
135+ #[ cfg( test) ]
136+ Self :: __ForInternalTestsUseOnly( i) => write ! ( f, "{i}" ) ,
137+ }
138+ }
139+ }
140+
141+ impl From < InvalidArgumentError > for Error {
142+ fn from ( err : InvalidArgumentError ) -> Self {
143+ Self :: InvalidArgument ( err)
144+ }
145+ }
146+
128147/// Indicates that an invalid parameter was specified.
129- #[ derive( Error , Debug ) ]
148+ #[ derive( Debug ) ]
130149#[ non_exhaustive]
131150pub enum InvalidArgumentError {
132151 /// Invalid logger name.
@@ -135,32 +154,48 @@ pub enum InvalidArgumentError {
135154 /// requirements.
136155 ///
137156 /// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
138- #[ error( "'logger name': {0}" ) ]
139- LoggerName ( #[ from] SetLoggerNameError ) ,
157+ LoggerName ( SetLoggerNameError ) ,
140158
141159 /// Invalid [`RotationPolicy`].
142160 ///
143161 /// See the documentation of [`RotationPolicy`] for the input requirements.
144162 ///
145163 /// [`RotationPolicy`]: crate::sink::RotationPolicy
146- #[ error( "'rotation policy': {0}" ) ]
147164 RotationPolicy ( String ) ,
148165
149166 /// Invalid thread pool capacity.
150167 #[ deprecated(
151168 since = "0.5.0" ,
152169 note = "non-zero thread pool capacity is now guarded by NonZeroUsize type"
153170 ) ]
154- #[ error( "'thread pool capacity': {0}" ) ]
155171 ThreadPoolCapacity ( String ) ,
156172}
157173
174+ impl StdError for InvalidArgumentError { }
175+
176+ impl Display for InvalidArgumentError {
177+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
178+ match self {
179+ Self :: LoggerName ( err) => write ! ( f, "'logger name': {err}" ) ,
180+ Self :: RotationPolicy ( value) => write ! ( f, "'rotation policy': {value}" ) ,
181+ #[ allow( deprecated) ]
182+ Self :: ThreadPoolCapacity ( value) => write ! ( f, "'thread pool capacity': {value}" ) ,
183+ }
184+ }
185+ }
186+
187+ impl From < SetLoggerNameError > for InvalidArgumentError {
188+ fn from ( err : SetLoggerNameError ) -> Self {
189+ Self :: LoggerName ( err)
190+ }
191+ }
192+
158193/// Indicates that an invalid logger name was set.
159194///
160195/// See the documentation of [`LoggerBuilder::name`] for the name requirements.
161196///
162197/// [`LoggerBuilder::name`]: crate::LoggerBuilder::name
163- #[ derive( Error , Debug ) ]
198+ #[ derive( Debug ) ]
164199pub struct SetLoggerNameError {
165200 name : String ,
166201}
@@ -178,6 +213,8 @@ impl SetLoggerNameError {
178213 }
179214}
180215
216+ impl StdError for SetLoggerNameError { }
217+
181218impl Display for SetLoggerNameError {
182219 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
183220 write ! ( f, "name '{}' contains disallowed characters" , self . name)
@@ -186,22 +223,33 @@ impl Display for SetLoggerNameError {
186223
187224/// Indicates that an error occurred while sending to channel.
188225#[ cfg( feature = "multi-thread" ) ]
189- #[ derive( Error , Debug ) ]
226+ #[ derive( Debug ) ]
190227#[ non_exhaustive]
191228pub enum SendToChannelError {
192229 /// The channel is full.
193230 ///
194231 /// The variant returned only when [`OverflowPolicy::DropIncoming`] is used.
195232 ///
196233 /// [`OverflowPolicy::DropIncoming`]: crate::sink::async_sink::OverflowPolicy::DropIncoming
197- #[ error( "the channel is full" ) ]
198234 Full ,
199235
200236 /// The channel is disconnected.
201- #[ error( "the channel is disconnected" ) ]
202237 Disconnected ,
203238}
204239
240+ #[ cfg( feature = "multi-thread" ) ]
241+ impl StdError for SendToChannelError { }
242+
243+ #[ cfg( feature = "multi-thread" ) ]
244+ impl Display for SendToChannelError {
245+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
246+ match self {
247+ Self :: Full => write ! ( f, "the channel is full" ) ,
248+ Self :: Disconnected => write ! ( f, "the channel is disconnected" ) ,
249+ }
250+ }
251+ }
252+
205253/// Contains data that is dropped after sending to the channel failed.
206254///
207255/// You can handle them manually or just ignore them.
@@ -271,10 +319,19 @@ impl SendToChannelErrorDropped {
271319
272320/// Indicates that an error occurred while building a pattern at compile-time.
273321#[ cfg( feature = "runtime-pattern" ) ]
274- #[ derive( Error , Debug ) ]
275- #[ error( "{0}" ) ]
322+ #[ derive( Debug ) ]
276323pub struct BuildPatternError ( pub ( crate ) spdlog_internal:: pattern_parser:: Error ) ;
277324
325+ #[ cfg( feature = "runtime-pattern" ) ]
326+ impl StdError for BuildPatternError { }
327+
328+ #[ cfg( feature = "runtime-pattern" ) ]
329+ impl Display for BuildPatternError {
330+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
331+ write ! ( f, "{}" , self . 0 )
332+ }
333+ }
334+
278335/// The result type of this crate.
279336pub type Result < T > = result:: Result < T , Error > ;
280337
0 commit comments