flogging/handlers/handler/handler_trait.rs
1//
2// File Name: handler_trait.rs
3// Project Name: flogging
4//
5// Copyright (C) 2025 Bradley Willcott
6//
7// SPDX-License-Identifier: GPL-3.0-or-later
8//
9// This library (crate) is free software: you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation, either version 3 of the License, or
12// (at your option) any later version.
13//
14// This library (crate) is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this library (crate). If not, see <https://www.gnu.org/licenses/>.
21//
22
23//!
24//! # HandlerTrait
25//!
26
27use super::*;
28
29///
30/// Provides common methods required for all handlers.
31///
32pub trait HandlerTrait: fmt::Display + Send + Sync {
33 ///
34 /// Create a new handler instance.
35 ///
36 /// ## Parameters
37 /// - `name` - Can be used as needed.
38 ///
39 fn create(name: &str) -> Result<Self, Error>
40 where
41 Self: Sized;
42
43 ///
44 /// Close the Handler and free all associated resources.
45 ///
46 /// The close method will perform a flush and then close the Handler.
47 /// After `close` has been called, this Handler should no longer be used.
48 /// Method calls will be silently ignored.
49 ///
50 fn close(&mut self);
51
52 ///
53 /// Flush any buffered output.
54 ///
55 fn flush(&mut self);
56
57 ///
58 /// Return a copy of the internal buffer as a `String`.
59 ///
60 fn get_log(&self) -> String;
61
62 ///
63 /// Return the Formatter for this Handler.
64 ///
65 fn get_formatter(&self) -> Formatter;
66
67 ///
68 /// Check status of this handler.
69 ///
70 fn is_open(&self) -> bool;
71
72 ///
73 /// Publish a LogEntry.
74 ///
75 /// The logging request was made initially to a Logger object, which initialized
76 /// the LogEntry and forwarded it here.
77 ///
78 /// The Handler is responsible for formatting the message, when and if necessary.
79 ///
80 /// ## Parameters
81 /// - `log_entry` - The `LogEntry` to be published.
82 ///
83 fn publish(&mut self, log_entry: &LogEntry);
84
85 ///
86 /// Set a Formatter.
87 ///
88 /// ## Parameters
89 /// - `formatter` The `Formatter` to use.
90 ///
91 fn set_formatter(&mut self, formatter: Formatter);
92}