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