src/handlers/file_handler.rs
Lines
98.66 %
Functions
100.00 %
Regions
98.50 %
| Line | Count | Source (jump to first uncovered line) |
|---|---|---|
| 1 | // | |
| 2 | // File Name: file_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 | //! # FileHandler | |
| 26 | //! | |
| 27 | ||
| 28 | use ::{ | |
| 29 | , | |
| 30 | ::{,}, | |
| 31 | ::{, ::,}, | |
| 32 | }; | |
| 33 | ||
| 34 | use crate::*; | |
| 35 | ||
| 36 | /// | |
| 37 | /// Publishes log entries to the file whose name was provided during | |
| 38 | /// initialization. | |
| 39 | /// | |
| 40 | #[derive(,)] | |
| 41 | pub struct FileHandler { | |
| 42 | filename: | |
| 43 | formatter: | |
| 44 | file: <> | |
| 45 | writer: <<u8>> | |
| 46 | } | |
| 47 | ||
| 48 | impl FileHandler { | |
| 49 | 20 | fn _create(filename: &str) -> <Self> { |
| 50 | 20 | ifis_empty() { |
| 51 | 1 | return Err(::(, "'filename' must not be empty")); |
| 52 | 19 | } |
| 53 | ||
| 54 | 19 | let={ |
| 55 | 19 | :to_string(), |
| 56 | 19 | : ::create(None), |
| 57 | : { | |
| 58 | 19 | let= ::()append(true)create(true)open()?; |
| 59 | 19 | Some() |
| 60 | }, | |
| 61 | 19 | : None, |
| 62 | }; | |
| 63 | ||
| 64 | 19 | Ok() |
| 65 | 20 | } |
| 66 | ||
| 67 | 3 | fn log(&self) -> { |
| 68 | 3 | if let Some() = selfto_owned() { |
| 69 | 1 | String::()unwrap() |
| 70 | } else { | |
| 71 | 2 | String::() |
| 72 | } | |
| 73 | 3 | } |
| 74 | } | |
| 75 | ||
| 76 | impl ::for FileHandler { | |
| 77 | 1 | fn fmt(&selff: &mut ::<_>) -> :: { |
| 78 | 1 | write!( "{} : {}", self, self) |
| 79 | 1 | } |
| 80 | } | |
| 81 | ||
| 82 | impl for FileHandler { | |
| 83 | /// | |
| 84 | /// Create a new handler instance. | |
| 85 | /// | |
| 86 | /// ## Parameters | |
| 87 | /// - `name` - This the `filename` of the log file. | |
| 88 | /// | |
| 89 | 20 | fn create(name: &str) -> <Self> { |
| 90 | 20 | ::() |
| 91 | 20 | } |
| 92 | ||
| 93 | /// | |
| 94 | /// Flushes and closes the file.\ | |
| 95 | /// Also, removes the internal buffer, if in `test_mode`.\ | |
| 96 | /// Will therefore, no longer be *in* `test_mode`. | |
| 97 | /// | |
| 98 | 3 | fn close(&mut self) { |
| 99 | 3 | selfflush(); |
| 100 | 3 | self= None; |
| 101 | 3 | } |
| 102 | ||
| 103 | 6 | fn flush(&mut self) { |
| 104 | 6 | if let Some() = &self{ |
| 105 | 6 | sync_all()expect("sync_all failed"); |
| 106 | 6 | } |
| 107 | 6 | } |
| 108 | ||
| 109 | 3 | fn get_formatter(&self) -> { |
| 110 | 3 | selfclone() |
| 111 | 3 | } |
| 112 | ||
| 113 | 3 | fn get_log(&self) -> { |
| 114 | 3 | selflog() |
| 115 | 3 | } |
| 116 | ||
| 117 | 53 | fn is_open(&self) -> bool { |
| 118 | 53 | selfis_some() |
| 119 | 53 | } |
| 120 | ||
| 121 | 49 | fn publish(&mut selflog_entry: &) { |
| 122 | 49 | if selfis_open() { |
| 123 | 48 | let mut= selfformat(); |
| 124 | 48 | push('\n'); |
| 125 | ||
| 126 | 2 | if let Some() = selfas_mut() { |
| 127 | 2 | writeln!( "{}", selfformat())expect("writeln!() failed"); |
| 128 | 46 | } else { |
| 129 | 46 | self |
| 130 | 46 | as_mut() |
| 131 | 46 | unwrap() |
| 132 | 46 | write_all(as_bytes()) |
| 133 | 46 | expect("write_all() failed"); |
| 134 | 46 | } |
| 135 | 1 | } |
| 136 | 49 | } |
| 137 | ||
| 138 | 3 | fn set_formatter(&mut selfformatter:) { |
| 139 | 3 | self=; |
| 140 | 3 | } |
| 141 | ||
| 142 | /// | |
| 143 | /// Sets the test mode to `state`. | |
| 144 | /// | |
| 145 | /// If set to `true`, use `get_log()` to obtain the | |
| 146 | /// log. | |
| 147 | /// | |
| 148 | 3 | fn set_test_mode(&mut selfstate: bool) { |
| 149 | 1 | if{ |
| 150 | 1 | // true |
| 151 | 1 | self= Some(Vec::()); |
| 152 | 2 | } else { |
| 153 | 2 | self= None; |
| 154 | 2 | } |
| 155 | 3 | } |
| 156 | } | |
| 157 | ||
| 158 | #[cfg()] | |
| 159 | mod tests { | |
| 160 | use crate::*; | |
| 161 | use ::{ | |
| 162 | ::, | |
| 163 | ::{,, Result}, | |
| 164 | }; | |
| 165 | ||
| 166 | #[test] | |
| 167 | 1 | fn file_handler() { |
| 168 | 1 | let mut= ::(module_path!(), "test_logs/file_handler.log"); |
| 169 | 1 | set_fn_name("file_handler"); |
| 170 | ||
| 171 | 1 | let=get_handler(crate::::)unwrap(); |
| 172 | 1 | set_test_mode(false); |
| 173 | ||
| 174 | 1 | assert!(is_open()); |
| 175 | 1 | assert_eq!( |
| 176 | 1 | get_formatter()to_string(), |
| 177 | 1 | "dt_fmt: \"%+\" - fmt_string: \"{dt:35} {mod_path}->{fn_name} [{level:7}] {message}\"" |
| 178 | 1 | to_string() |
| 179 | ); | |
| 180 | ||
| 181 | 1 | info("trait methods"); |
| 182 | 1 | warning("The sky is falling!"); |
| 183 | ||
| 184 | 1 | let=get_handler(crate::::)unwrap(); |
| 185 | ||
| 186 | 1 | assert_eq!(get_log(), ""to_string()); |
| 187 | ||
| 188 | 1 | flush(); |
| 189 | 1 | close(); |
| 190 | 1 | exiting_with("This should get thrown away."); |
| 191 | 1 | } |
| 192 | ||
| 193 | #[test] | |
| 194 | 1 | fn file_handler_file_test() { |
| 195 | 1 | let= "flogging::handlers::file_handler::tests->file_handler_file_test [INFO ] trait methods |
| 196 | 1 | flogging::handlers::file_handler::tests->file_handler_file_test [WARNING] The sky is falling!\n" |
| 197 | 1 | to_string(); |
| 198 | ||
| 199 | 1 | let mut= ::(module_path!()) |
| 200 | 1 | set_fn_name("file_handler_file_test") |
| 201 | 1 | remove_file("test_logs/file_handler_file_test.log") |
| 202 | 1 | add_file_handler_with( |
| 203 | 1 | "test_logs/file_handler_file_test.log", |
| 204 | 1 | ::, |
| 205 | 1 | None, |
| 206 | ) | |
| 207 | 1 | build(); |
| 208 | ||
| 209 | 1 | let=get_handler(crate::::)unwrap(); |
| 210 | 1 | set_test_mode(false); |
| 211 | ||
| 212 | 1 | assert!(is_open()); |
| 213 | 1 | assert_eq!( |
| 214 | 1 | get_formatter()to_string(), |
| 215 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 216 | 1 | to_string() |
| 217 | ); | |
| 218 | ||
| 219 | 1 | info("trait methods"); |
| 220 | 1 | warning("The sky is falling!"); |
| 221 | ||
| 222 | 1 | let=get_handler(crate::::)unwrap(); |
| 223 | ||
| 224 | 1 | assert_eq!(get_log(), ""to_string()); |
| 225 | ||
| 226 | 1 | flush(); |
| 227 | 1 | close(); |
| 228 | 1 | assert!(!is_open()); |
| 229 | ||
| 230 | 1 | severe("This should get thrown away."); |
| 231 | ||
| 232 | 1 | if let Ok(mut) = ::("test_logs/file_handler_file_test.log") { |
| 233 | 1 | let mut= String::(); |
| 234 | 1 | if let Ok() =read_to_string(&mut) { |
| 235 | 1 | assert_eq!(,); |
| 236 | 0 | } |
| 237 | 0 | } |
| 238 | 1 | } |
| 239 | ||
| 240 | #[test] | |
| 241 | 1 | fn file_handler_test_mode() { |
| 242 | 1 | let= "flogging::handlers::file_handler::tests->file_handler_test_mode [INFO ] trait methods |
| 243 | 1 | flogging::handlers::file_handler::tests->file_handler_test_mode [WARNING] The sky is falling!\n" |
| 244 | 1 | to_string(); |
| 245 | ||
| 246 | 1 | let mut= ::(module_path!()) |
| 247 | 1 | set_fn_name("file_handler_test_mode") |
| 248 | 1 | remove_file("test_logs/file_handler_test_mode.log") |
| 249 | 1 | add_file_handler_with( |
| 250 | 1 | "test_logs/file_handler_test_mode.log", |
| 251 | 1 | ::, |
| 252 | 1 | None, |
| 253 | ) | |
| 254 | 1 | build(); |
| 255 | ||
| 256 | 1 | let=get_handler(crate::::)unwrap(); |
| 257 | 1 | set_test_mode(true); |
| 258 | ||
| 259 | 1 | assert!(is_open()); |
| 260 | 1 | assert_eq!( |
| 261 | 1 | get_formatter()to_string(), |
| 262 | 1 | "dt_fmt: \"\" - fmt_string: \"{mod_path}->{fn_name} [{level:7}] {message}\"" |
| 263 | 1 | to_string() |
| 264 | ); | |
| 265 | ||
| 266 | 1 | info("trait methods"); |
| 267 | 1 | warning("The sky is falling!"); |
| 268 | ||
| 269 | 1 | let=get_handler(crate::::)unwrap(); |
| 270 | 1 | let=get_log(); |
| 271 | ||
| 272 | 1 | assert_eq!(,); |
| 273 | ||
| 274 | 1 | flush(); |
| 275 | 1 | close(); |
| 276 | 1 | } |
| 277 | ||
| 278 | #[test] | |
| 279 | #[should_panic(= "'filename' must not be empty")] | |
| 280 | 1 | fn filename_empty() { |
| 281 | 1 | let _ = ::(module_path!(), ""); |
| 282 | 1 | } |
| 283 | } |