src/handlers/formatters/unixtimestamp_formatter.rs
Lines
100.00 %
Functions
100.00 %
Regions
100.00 %
| Line | Count | Source (jump to first uncovered line) |
|---|---|---|
| 1 | // | |
| 2 | // File Name: unixtimestamp_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 | //! # UnixTimeStamp Formatter | |
| 26 | //! | |
| 27 | ||
| 28 | use ::fmt; | |
| 29 | use crate::FormatTrait; | |
| 30 | ||
| 31 | #[derive(,,,,)] | |
| 32 | ||
| 33 | /// | |
| 34 | /// Unix Timestamp format. | |
| 35 | /// | |
| 36 | /// The first part (before the decimal point) is | |
| 37 | /// the number of seconds since 1970-01-01 00:00 UTC. | |
| 38 | /// | |
| 39 | /// The second part is the number of nanoseconds since | |
| 40 | /// the last whole second. | |
| 41 | /// | |
| 42 | /// Example: | |
| 43 | /// ```text | |
| 44 | /// 1752817859.157970496 | |
| 45 | /// ``` | |
| 46 | /// Template: | |
| 47 | /// - `dt` in the template would be the datetime string, similar to the above. | |
| 48 | /// - `mod_path`, `fn_name`, `level`, and `message` all come out of the `LogEntry` | |
| 49 | /// provided to the [`format()`][UnixTimestampFormatter::format] method. | |
| 50 | /// | |
| 51 | /// ```ignore | |
| 52 | /// format!("{dt} {mod_path}->{fn_name} [{level:7}] {message}"); | |
| 53 | /// ``` | |
| 54 | /// Sample output: | |
| 55 | /// ```text | |
| 56 | /// 1752818461.051538870 flogging->main [SEVERE ] Hurricanes are windy! | |
| 57 | /// ``` | |
| 58 | /// | |
| 59 | pub struct UnixTimestampFormatter { | |
| 60 | dt_fmt: | |
| 61 | fmt_string: | |
| 62 | } | |
| 63 | ||
| 64 | impl UnixTimestampFormatter { | |
| 65 | /// | |
| 66 | /// Creates a new instance of `UnixTimestampFormatter`. | |
| 67 | /// | |
| 68 | 2 | pub fn new() -> Self { |
| 69 | 2 | Self { |
| 70 | 2 | : "%s.%f"to_string(), |
| 71 | 2 | : "{dt} {mod_path}->{fn_name} [{level:7}] {message}"to_string(), |
| 72 | 2 | } |
| 73 | 2 | } |
| 74 | ||
| 75 | /// | |
| 76 | /// Returns the date/time format string. | |
| 77 | /// | |
| 78 | 3 | pub fn dt_fmt(&self) -> { |
| 79 | 3 | selfclone() |
| 80 | 3 | } |
| 81 | ||
| 82 | /// | |
| 83 | /// Returns the primary format string. | |
| 84 | /// | |
| 85 | 3 | pub fn fmt_string(&self) -> { |
| 86 | 3 | selfclone() |
| 87 | 3 | } |
| 88 | } | |
| 89 | ||
| 90 | impl for UnixTimestampFormatter { | |
| 91 | 2 | fn default() -> Self { |
| 92 | 2 | Self::() |
| 93 | 2 | } |
| 94 | } | |
| 95 | ||
| 96 | impl ::for UnixTimestampFormatter { | |
| 97 | 1 | fn fmt(&selff: &mut ::<_>) -> :: { |
| 98 | 1 | write!( |
| 99 | 1 | |
| 100 | 1 | "dt_fmt: \"{}\" - fmt_string: \"{}\"", |
| 101 | self, self | |
| 102 | ) | |
| 103 | 1 | } |
| 104 | } | |
| 105 | ||
| 106 | impl for UnixTimestampFormatter { | |
| 107 | 3 | fn format(&selflog_entry: &::) -> { |
| 108 | 3 | selfft_fmt(selfdt_fmt(), selffmt_string(),) |
| 109 | 3 | } |
| 110 | } |