syslog_rs/sync/
syslog_trait.rs

1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 * 
4 * Copyright 2025 Aleksandr Morozov
5 * 
6 * The syslog-rs crate can be redistributed and/or modified
7 * under the terms of either of the following licenses:
8 *
9 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
10 *                     
11 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
12 */
13
14use crate::{error::SyRes, formatters::SyslogFormatter, Priority, SyslogDestination};
15
16/// A trait whcih describes common syslog API. The generic `D` [SyslogDestination] instance
17/// contains the information about the destination and from which the syslog instance should 
18/// be constructed.
19#[allow(dead_code)]
20pub trait SyslogApi<F: SyslogFormatter, D: SyslogDestination>: std::fmt::Debug  + 'static
21{
22    /// Connects the current instance to the syslog server (destination).
23    fn connectlog(&self) -> SyRes<()>;
24
25    /// Sets the logmask to filter out the syslog calls.
26    /// 
27    /// See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
28    ///
29    /// # Example
30    ///
31    /// LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
32    ///
33    /// or
34    ///
35    /// ~(LOG_MASK!(Priority::LOG_INFO))
36    /// LOG_UPTO!(Priority::LOG_ERROR)
37    fn setlogmask(&self, logmask: i32) -> SyRes<i32>;
38
39    /// Closes connection to the syslog server (destination).
40    fn closelog(&self) -> SyRes<()>;
41
42    /// Similar to libc, syslog() sends data to syslog server.
43    /// 
44    /// # Arguments
45    ///
46    /// * `pri` - a priority [Priority]
47    ///
48    /// * `fmt` - a formatter [SyslogFormatter] message. In C exists a functions with
49    ///     variable argumets amount. In Rust you should create your
50    ///     own macros like format!() or use format!()]. The [String] and ref `'static` 
51    ///     [str] can be passed directly.
52    fn syslog(&self, pri: Priority, fmt: F);
53
54    /// This function can be used to update the facility name, for example
55    /// after fork().
56    /// 
57    /// # Arguments
58    /// 
59    /// * `ident` - an [Option] optional new identity (up to 48 UTF8 chars)
60    ///     If set to [Option::None] would request the program name from OS.
61    fn change_identity(&self, ident: Option<&str>) -> SyRes<()>;
62
63    /// Re-opens the connection to the syslog server. Can be used to 
64    /// rotate logs(handle SIGHUP).
65    /// 
66    /// # Returns
67    /// 
68    /// A [Result] is retured as [SyRes].
69    /// 
70    /// * [Result::Ok] - with empty inner type.
71    /// 
72    /// * [Result::Err] - an error code and description 
73    fn reconnect(&self) -> SyRes<()>;
74
75    /// Updates the instance's socket. `tap_data` [TapTypeData] should be of
76    /// the same variant (type) as current.
77    fn update_tap_data(&self, tap_data: D) -> SyRes<()>;
78}
79