src/handlers/console_handler.rs
Lines
100.00 %
Functions
100.00 %
Regions
99.43 %
| Line | Count | Source (jump to first uncovered line) |
|---|---|---|
| 1 | // | |
| 2 | // File Name: console_handler.rs | |
| 3 | // Directory: src/handlers | |
| 4 | // Project Name: flogging | |
| 5 | // | |
| 6 | // Copyright (C) 2025 Bradley Willcott | |
| 7 | // | |
| 8 | // SPDX-License-Identifier: GPL-3.0-or-later | |
| 9 | // | |
| 10 | // This library (crate) is free software: you can redistribute it and/or modify | |
| 11 | // it under the terms of the GNU General Public License as published by | |
| 12 | // the Free Software Foundation, either version 3 of the License, or | |
| 13 | // (at your option) any later version. | |
| 14 | // | |
| 15 | // This library (crate) is distributed in the hope that it will be useful, | |
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 18 | // GNU General Public License for more details. | |
| 19 | // | |
| 20 | // You should have received a copy of the GNU General Public License | |
| 21 | // along with this library (crate). If not, see <https://www.gnu.org/licenses/>. | |
| 22 | // | |
| 23 | ||
| 24 | //! | |
| 25 | //! # ConsoleHandler | |
| 26 | //! | |
| 27 | //! Publishes log entries to the console: `[std::io::stderr]`. | |
| 28 | //! | |
| 29 | ||
| 30 | pub mod console_type; | |
| 31 | ||
| 32 | use crate::*; | |
| 33 | use ::ConsoleType; | |
| 34 | use ::{ | |
| 35 | , | |
| 36 | ::{self,,}, | |
| 37 | }; | |
| 38 | ||
| 39 | /// | |
| 40 | /// Publishes log entries to the console. | |
| 41 | /// | |
| 42 | /// If `console_type` is: | |
| 43 | /// | |
| 44 | /// - `ConsoleType::StdOut` - print to `stdout`, | |
| 45 | /// - `ConsoleType::StdErr` - print to `stderr`, | |
| 46 | /// - `ConsoleType::Production`:\ | |
| 47 | /// If `log_entry.level` is `LeveL::INFO`, then\ | |
| 48 | /// prints unformatted `log_entry.msg` to `stdout`, else\ | |
| 49 | /// prints formatted `log_entry.msg` to `stderr`. | |
| 50 | /// | |
| 51 | #[derive(,)] | |
| 52 | pub struct ConsoleHandler { | |
| 53 | console_type: | |
| 54 | formatter: | |
| 55 | writer: <<u8>> | |
| 56 | } | |
| 57 | ||
| 58 | impl ConsoleHandler { | |
| 59 | 31 | fn _create(console_type:) -> Self { |
| 60 | 31 | { |
| 61 | 31 | , |
| 62 | 31 | : ::create(None), |
| 63 | 31 | : None, |
| 64 | 31 | } |
| 65 | 31 | } |
| 66 | ||
| 67 | 8 | fn log(&self) -> { |
| 68 | 8 | if let Some() = selfto_owned() { |
| 69 | 5 | String::()unwrap() |
| 70 | } else { | |
| 71 | 3 | String::() |
| 72 | } | |
| 73 | 8 | } |
| 74 | } | |
| 75 | ||
| 76 | impl ::for ConsoleHandler { | |
| 77 | 1 | fn fmt(&selff: &mut ::<_>) -> :: { |
| 78 | 1 | selffmt() |
| 79 | 1 | } |
| 80 | } | |
| 81 | ||
| 82 | impl for ConsoleHandler { | |
| 83 | 31 | fn create(console_type: &str) -> <Self> |
| 84 | 31 | where |
| 85 | 31 | Self: |
| 86 | { | |
| 87 | 31 | Ok(::(parse()unwrap())) |
| 88 | 31 | } |
| 89 | ||
| 90 | /// | |
| 91 | /// Removes the internal buffer, if in `test_mode`.\ | |
| 92 | /// Will therefore, no longer be *in* `test_mode`. | |
| 93 | /// | |
| 94 | 6 | fn close(&mut self) { |
| 95 | 3 | if selfis_some() { |
| 96 | 3 | self= None; |
| 97 | 3 | } |
| 98 | 6 | } |
| 99 | ||
| 100 | /// | |
| 101 | /// Clears the internal buffer, if in `test_mode`. | |
| 102 | /// | |
| 103 | 6 | fn flush(&mut self) { |
| 104 | 6 | if let Some() = selfas_mut() { |
| 105 | 3 | clear() |
| 106 | 3 | }; |
| 107 | 6 | } |
| 108 | ||
| 109 | 6 | fn get_formatter(&self) -> { |
| 110 | 6 | selfclone() |
| 111 | 6 | } |
| 112 | ||
| 113 | 8 | fn get_log(&self) -> { |
| 114 | 8 | selflog() |
| 115 | 8 | } |
| 116 | ||
| 117 | /// | |
| 118 | /// `ConsoleHandler` is *always* open. | |
| 119 | /// | |
| 120 | 6 | fn is_open(&self) -> bool { |
| 121 | 6 | true |
| 122 | 6 | } |
| 123 | ||
| 124 | 75 | fn publish(&mut selflog_entry: &) { |
| 125 | 75 | match selfas_mut() { |
| 126 | 10 | Some() => { |
| 127 | 10 | let _ = match self{ |
| 128 | 6 | ::=> writeln!( "{}", selfformat()), |
| 129 | 2 | ::=> writeln!( "{}", selfformat()), |
| 130 | 2 | ::=> production_test(, &self,), |
| 131 | }; | |
| 132 | } | |
| 133 | 65 | None => match self{ |
| 134 | 50 | ::=> println!("{}", selfformat()), |
| 135 | 9 | ::=> eprintln!("{}", selfformat()), |
| 136 | 6 | ::=> production(&self,), |
| 137 | }, | |
| 138 | } | |
| 139 | 75 | } |
| 140 | ||
| 141 | 7 | fn set_formatter(&mut selfformatter:) { |
| 142 | 7 | self=; |
| 143 | 7 | } |
| 144 | ||
| 145 | /// | |
| 146 | /// Sets the test mode to `state`. | |
| 147 | /// | |
| 148 | /// If set to `true`, use `get_log()` to obtain the | |
| 149 | /// log. | |
| 150 | /// | |
| 151 | 8 | fn set_test_mode(&mut selfstate: bool) { |
| 152 | 5 | if{ |
| 153 | 5 | // true |
| 154 | 5 | self= Some(Vec::()); |
| 155 | 3 | } else { |
| 156 | 3 | self= None; |
| 157 | 3 | } |
| 158 | 8 | } |
| 159 | } | |
| 160 | ||
| 161 | 6 | fn production(formatter: &log_entry: &) { |
| 162 | 3 | iflevel() == ::INFO { |
| 163 | 3 | println!("{}",message()); |
| 164 | 3 | } else { |
| 165 | 3 | eprintln!("{}",format()); |
| 166 | 3 | } |
| 167 | 6 | } |
| 168 | ||
| 169 | 2 | fn production_test( |
| 170 | 2 | writer: &mut <u8> |
| 171 | 2 | formatter: & |
| 172 | 2 | log_entry: & |
| 173 | 2 | ) -> ::<()> { |
| 174 | 2 | iflevel() == ::INFO { |
| 175 | 1 | writeln!( "{}",message())?; |
| 176 | } else { | |
| 177 | 1 | writeln!( "{}",format())?; |
| 178 | } | |
| 179 | ||
| 180 | 2 | Ok(()) |
| 181 | 2 | } |
| 182 | ||
| 183 | #[cfg()] | |
| 184 | mod tests { | |
| 185 | use crate::*; | |
| 186 | ||
| 187 | #[test] | |
| 188 | 1 | fn stdout_handler() { |
| 189 | 1 | let mut= ::(module_path!()); |
| 190 | ||
| 191 | 1 | info("trait methods"); |
| 192 | 1 | warning("The sky is falling!"); |
| 193 | ||
| 194 | 1 | let=get_handler(crate::::)unwrap(); |
| 195 | 1 | set_test_mode(false); |
| 196 | ||
| 197 | 1 | assert!(is_open()); |
| 198 | 1 | assert_eq!( |
| 199 | 1 | get_formatter()to_string(), |
| 200 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 201 | 1 | to_string() |
| 202 | ); | |
| 203 | ||
| 204 | 1 | assert_eq!(get_log(), ""to_string()); |
| 205 | ||
| 206 | 1 | flush(); |
| 207 | 1 | close(); |
| 208 | 1 | } |
| 209 | ||
| 210 | #[test] | |
| 211 | 1 | fn stdout_handler_test_mode() { |
| 212 | 1 | let= "flogging::handlers::console_handler::tests-> [INFO ] trait methods |
| 213 | 1 | flogging::handlers::console_handler::tests-> [WARNING] The sky is falling! |
| 214 | 1 | " |
| 215 | 1 | to_string(); |
| 216 | ||
| 217 | 1 | let mut= ::(module_path!()); |
| 218 | ||
| 219 | 1 | let=get_handler(crate::::)unwrap(); |
| 220 | 1 | set_test_mode(true); |
| 221 | ||
| 222 | 1 | assert!(is_open()); |
| 223 | 1 | assert_eq!( |
| 224 | 1 | get_formatter()to_string(), |
| 225 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 226 | 1 | to_string() |
| 227 | ); | |
| 228 | ||
| 229 | 1 | info("trait methods"); |
| 230 | 1 | warning("The sky is falling!"); |
| 231 | ||
| 232 | 1 | let=get_handler(crate::::)unwrap(); |
| 233 | 1 | let=get_log(); |
| 234 | ||
| 235 | 1 | assert_eq!(,); |
| 236 | ||
| 237 | 1 | flush(); |
| 238 | 1 | close(); |
| 239 | 1 | } |
| 240 | ||
| 241 | #[test] | |
| 242 | 1 | fn stderr_handler() { |
| 243 | 1 | let mut= ::(module_path!()); |
| 244 | ||
| 245 | 1 | info("trait methods"); |
| 246 | 1 | warning("The sky is falling!"); |
| 247 | ||
| 248 | 1 | let=get_handler(crate::::)unwrap(); |
| 249 | 1 | set_test_mode(false); |
| 250 | ||
| 251 | 1 | assert!(is_open()); |
| 252 | 1 | assert_eq!( |
| 253 | 1 | get_formatter()to_string(), |
| 254 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 255 | 1 | to_string() |
| 256 | ); | |
| 257 | ||
| 258 | 1 | assert_eq!(get_log(), ""to_string()); |
| 259 | ||
| 260 | 1 | flush(); |
| 261 | 1 | close(); |
| 262 | 1 | } |
| 263 | ||
| 264 | #[test] | |
| 265 | 1 | fn stderr_handler_test_mode() { |
| 266 | 1 | let= "flogging::handlers::console_handler::tests-> [INFO ] trait methods |
| 267 | 1 | flogging::handlers::console_handler::tests-> [WARNING] The sky is falling!\n" |
| 268 | 1 | to_string(); |
| 269 | ||
| 270 | 1 | let mut= ::(module_path!()); |
| 271 | ||
| 272 | 1 | let=get_handler(crate::::)unwrap(); |
| 273 | 1 | set_test_mode(true); |
| 274 | ||
| 275 | 1 | assert!(is_open()); |
| 276 | 1 | assert_eq!( |
| 277 | 1 | get_formatter()to_string(), |
| 278 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 279 | 1 | to_string() |
| 280 | ); | |
| 281 | ||
| 282 | 1 | info("trait methods"); |
| 283 | 1 | warning("The sky is falling!"); |
| 284 | ||
| 285 | 1 | let=get_handler(crate::::)unwrap(); |
| 286 | 1 | let=get_log(); |
| 287 | ||
| 288 | 1 | assert_eq!(,); |
| 289 | ||
| 290 | 1 | flush(); |
| 291 | 1 | close(); |
| 292 | 1 | } |
| 293 | ||
| 294 | #[test] | |
| 295 | 1 | fn production_handler() { |
| 296 | 1 | let mut= ::(module_path!()); |
| 297 | ||
| 298 | 1 | info("trait methods"); |
| 299 | 1 | warning("The sky is falling!"); |
| 300 | ||
| 301 | 1 | let=get_handler(crate::::)unwrap(); |
| 302 | 1 | set_test_mode(false); |
| 303 | ||
| 304 | 1 | assert!(is_open()); |
| 305 | 1 | assert_eq!( |
| 306 | 1 | get_formatter()to_string(), |
| 307 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 308 | 1 | to_string() |
| 309 | ); | |
| 310 | ||
| 311 | 1 | assert_eq!(get_log(), ""to_string()); |
| 312 | ||
| 313 | 1 | flush(); |
| 314 | 1 | close(); |
| 315 | 1 | } |
| 316 | ||
| 317 | #[test] | |
| 318 | 1 | fn production_handler_test_mode() { |
| 319 | 1 | let= "trait methods |
| 320 | 1 | flogging::handlers::console_handler::tests-> [WARNING] The sky is falling!\n" |
| 321 | 1 | to_string(); |
| 322 | ||
| 323 | 1 | let mut= ::(module_path!()); |
| 324 | ||
| 325 | 1 | let=get_handler(crate::::)unwrap(); |
| 326 | 1 | set_test_mode(true); |
| 327 | ||
| 328 | 1 | assert!(is_open()); |
| 329 | 1 | assert_eq!( |
| 330 | 1 | get_formatter()to_string(), |
| 331 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 332 | 1 | to_string() |
| 333 | ); | |
| 334 | ||
| 335 | 1 | info("trait methods"); |
| 336 | 1 | warning("The sky is falling!"); |
| 337 | ||
| 338 | 1 | let=get_handler(crate::::)unwrap(); |
| 339 | 1 | let=get_log(); |
| 340 | ||
| 341 | 1 | assert_eq!(,); |
| 342 | ||
| 343 | 1 | flush(); |
| 344 | 1 | close(); |
| 345 | 1 | } |
| 346 | } |