src/handlers/formatters/iso8601_formatter.rs
Lines
100.00 %
Functions
100.00 %
Regions
100.00 %
| Line | Count | Source |
|---|---|---|
| 1 | // | |
| 2 | // File Name: iso8601_formatter.rs | |
| 3 | // Directory: src/handlers/formatters | |
| 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 | //! # Iso8601 Formatter | |
| 26 | //! | |
| 27 | ||
| 28 | use ::fmt; | |
| 29 | use crate::FormatTrait; | |
| 30 | ||
| 31 | /// | |
| 32 | /// ISO 8601 / RFC 3339 date & time format. | |
| 33 | /// | |
| 34 | /// Example: | |
| 35 | /// ```text | |
| 36 | /// 2001-07-08T00:34:60.026490+09:30 | |
| 37 | /// ``` | |
| 38 | /// Template: | |
| 39 | /// - `dt` in the template would be the datetime string, similar to the above. | |
| 40 | /// - `mod_path`, `fn_name`, `level`, and `message` all come out of the `LogEntry` | |
| 41 | /// provided to the [`format()`][Iso8601Formatter::format] method. | |
| 42 | /// | |
| 43 | /// ```ignore | |
| 44 | /// format!("{dt:35} {mod_path}->{fn_name} [{level:7}] {message}"); | |
| 45 | /// ``` | |
| 46 | /// Sample output: | |
| 47 | /// ```text | |
| 48 | /// 2025-07-18T14:01:01.051532664+08:00 flogging->main [WARNING] Rain is wet! | |
| 49 | /// ``` | |
| 50 | /// | |
| 51 | #[derive(,,,,)] | |
| 52 | pub struct Iso8601Formatter { | |
| 53 | dt_fmt: | |
| 54 | fmt_string: | |
| 55 | } | |
| 56 | ||
| 57 | impl Iso8601Formatter { | |
| 58 | /// | |
| 59 | /// Creates a new instance of `Iso8601Formatter`. | |
| 60 | /// | |
| 61 | 24 | pub fn new() -> Self { |
| 62 | 24 | Self { |
| 63 | 24 | : "%+"to_string(), |
| 64 | 24 | : "{dt:35} {mod_path}->{fn_name} [{level:7}] {message}"to_string(), |
| 65 | 24 | } |
| 66 | 24 | } |
| 67 | ||
| 68 | /// | |
| 69 | /// Returns the date/time format string. | |
| 70 | /// | |
| 71 | 57 | pub fn dt_fmt(&self) -> { |
| 72 | 57 | selfclone() |
| 73 | 57 | } |
| 74 | ||
| 75 | /// | |
| 76 | /// Returns the primary format string. | |
| 77 | /// | |
| 78 | 57 | pub fn fmt_string(&self) -> { |
| 79 | 57 | selfclone() |
| 80 | 57 | } |
| 81 | } | |
| 82 | ||
| 83 | impl for Iso8601Formatter { | |
| 84 | 24 | fn default() -> Self { |
| 85 | 24 | Self::() |
| 86 | 24 | } |
| 87 | } | |
| 88 | ||
| 89 | impl ::for Iso8601Formatter { | |
| 90 | 3 | fn fmt(&selff: &mut ::<_>) -> :: { |
| 91 | 3 | write!( |
| 92 | 3 | |
| 93 | 3 | "dt_fmt: \"{}\" - fmt_string: \"{}\"", |
| 94 | self, self | |
| 95 | ) | |
| 96 | 3 | } |
| 97 | } | |
| 98 | ||
| 99 | impl for Iso8601Formatter { | |
| 100 | 57 | fn format(&selflog_entry: &::) -> { |
| 101 | 57 | selfft_fmt(selfdt_fmt(), selffmt_string(),) |
| 102 | 57 | } |
| 103 | } |