1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
//! [`sod::Service`] logging implementations via [`log`](https://crates.io/crates/log).
//!
//! ## Service Impls
//! * [`LogDebugService`] logs [`Debug`] input at a configured log level to [`log::log`], returning the input as output.
//! * [`LogDisplayService`] logs [`Display`] input at a configured log level to [`log::log`], returning the input as output.
//!
//! ## Use Case
//! These [`Service`] impls are most useful for logging an event as it passes through a service chain.
//!
//! ## Example
//! ```
//! use sod::Service;
//! use sod_log::LogDisplayService;
//!
//! let logging_service = LogDisplayService::info("my event: ");
//! logging_service.process("hello world!").unwrap();
//! ```

use std::{
    borrow::Cow,
    fmt::{Debug, Display},
};

use log::Level;
use sod::Service;

/// A [`sod::Service`] that logs [`Debug`] input at a configured log level to [`log::log`], returning the input as output.
pub struct LogDebugService<'a> {
    level: Level,
    prefix: Cow<'a, str>,
}
impl<'a> LogDebugService<'a> {
    /// Log input at the given log level
    /// # Arguments
    /// * `level` - The log level
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn new<S: Into<Cow<'a, str>>>(level: Level, prefix: S) -> Self {
        Self {
            level,
            prefix: prefix.into(),
        }
    }
    /// Log as [`Level::Debug`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn debug<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Debug, prefix)
    }
    /// Log as [`Level::Error`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn error<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Error, prefix)
    }
    /// Log as [`Level::Info`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn info<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Info, prefix)
    }
    /// Log as [`Level::Trace`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn trace<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Trace, prefix)
    }
    /// Log as [`Level::Warn`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn warn<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Warn, prefix)
    }
}
impl<'a, T: Debug> Service<T> for LogDebugService<'a> {
    type Output = T;
    type Error = ();
    fn process(&self, input: T) -> Result<Self::Output, Self::Error> {
        log::log!(self.level, "{}{:?}", self.prefix, input);
        Ok(input)
    }
}

/// A [`sod::Service`] that logs optional [`Debug`] input when it is `Some(input)` at a configured log level to [`log::log`], returning the input as output.
///
/// This service is useful for logging an event as it passed through a service chain, while ignoring non-blocking service chains that may continuously process `None` in a tight loop.
pub struct LogOptionalDebugService<'a> {
    level: Level,
    prefix: Cow<'a, str>,
}
impl<'a> LogOptionalDebugService<'a> {
    /// Log input at the given log level
    /// # Arguments
    /// * `level` - The log level
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn new<S: Into<Cow<'a, str>>>(level: Level, prefix: S) -> Self {
        Self {
            level,
            prefix: prefix.into(),
        }
    }
    /// Log as [`Level::Debug`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn debug<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Debug, prefix)
    }
    /// Log as [`Level::Error`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn error<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Error, prefix)
    }
    /// Log as [`Level::Info`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn info<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Info, prefix)
    }
    /// Log as [`Level::Trace`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn trace<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Trace, prefix)
    }
    /// Log as [`Level::Warn`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn warn<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Warn, prefix)
    }
}
impl<'a, T: Debug> Service<Option<T>> for LogOptionalDebugService<'a> {
    type Output = Option<T>;
    type Error = ();
    fn process(&self, input: Option<T>) -> Result<Self::Output, Self::Error> {
        if let Some(input) = &input {
            log::log!(self.level, "{}{:?}", self.prefix, input);
        }
        Ok(input)
    }
}

/// A [`sod::Service`] that logs [`Display`] input at a configured log level to [`log::log`], returning the input as output.
///
/// This service is useful for logging an event as it passed through a service chain.
pub struct LogDisplayService<'a> {
    level: Level,
    prefix: Cow<'a, str>,
}
impl<'a> LogDisplayService<'a> {
    /// Log input at the given log level
    /// # Arguments
    /// * `level` - The log level
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn new<S: Into<Cow<'a, str>>>(level: Level, prefix: S) -> Self {
        Self {
            level,
            prefix: prefix.into(),
        }
    }
    /// Log as [`Level::Debug`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn debug<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Debug, prefix)
    }
    /// Log as [`Level::Error`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn error<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Error, prefix)
    }
    /// Log as [`Level::Info`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn info<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Info, prefix)
    }
    /// Log as [`Level::Trace`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn trace<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Trace, prefix)
    }
    /// Log as [`Level::Warn`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn warn<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Warn, prefix)
    }
}
impl<'a, T: Display> Service<T> for LogDisplayService<'a> {
    type Output = T;
    type Error = ();
    fn process(&self, input: T) -> Result<Self::Output, Self::Error> {
        log::log!(self.level, "{}{}", self.prefix, input);
        Ok(input)
    }
}

/// A [`sod::Service`] that logs optional [`Display`] input when it is `Some(input)` at a configured log level to [`log::log`], returning the input as output.
///
/// This service is useful for logging an event as it passed through a service chain, while ignoring non-blocking service chains that may continuously process `None` in a tight loop.
pub struct LogOptionalDisplayService<'a> {
    level: Level,
    prefix: Cow<'a, str>,
}
impl<'a> LogOptionalDisplayService<'a> {
    /// Log input at the given log level
    /// # Arguments
    /// * `level` - The log level
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn new<S: Into<Cow<'a, str>>>(level: Level, prefix: S) -> Self {
        Self {
            level,
            prefix: prefix.into(),
        }
    }
    /// Log as [`Level::Debug`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn debug<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Debug, prefix)
    }
    /// Log as [`Level::Error`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn error<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Error, prefix)
    }
    /// Log as [`Level::Info`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn info<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Info, prefix)
    }
    /// Log as [`Level::Trace`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn trace<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Trace, prefix)
    }
    /// Log as [`Level::Warn`]
    /// # Arguments
    /// * `prefix` - A prefix to prepend to the beginning of the log statment
    pub fn warn<S: Into<Cow<'a, str>>>(prefix: S) -> Self {
        Self::new(Level::Warn, prefix)
    }
}
impl<'a, T: Display> Service<Option<T>> for LogOptionalDisplayService<'a> {
    type Output = Option<T>;
    type Error = ();
    fn process(&self, input: Option<T>) -> Result<Self::Output, Self::Error> {
        if let Some(input) = &input {
            log::log!(self.level, "{}{}", self.prefix, input);
        }
        Ok(input)
    }
}