pub struct SyncSyslog<F = DefaultSyslogFormatter, D = DefaultLocalSyslogDestination>where
F: SyslogFormatter,
D: SyslogDestination,{ /* private fields */ }Expand description
A sync, shared instance of the syslog client which is shared between many
threads. Previously a mutex was used, but since the v5.0.0 a CoW experimental
approach is used. The CoW creates clones of the instance without holding
a long mutex locks. When writing, the instance does not hold a long lock on
updated item. Only exclusive lock locks the readers, because the instance is
updated and not usable anyway.
If the program has fixed amount of threads, probably the syslog_threadlocal
will be better alternative. It has the same functionality, but avoids
any sync locks by working in current thread.
§Traits
For this isntance a [SyslogApi] and [SyStreamApi] are implemented.
§Examples
let log =
SyncSyslog::openlog(
Some("test1"),
LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
LogFacility::LOG_DAEMON,
SyslogLocal::new()
);let log =
SyncSyslog
::<DefaultSyslogFormatter, SyslogLocal>
::openlog_with(
Some("test1"),
LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
LogFacility::LOG_DAEMON,
SyslogLocal::new()
);pub static SYSLOG3: LazyLock<SyncSyslog<DefaultSyslogFormatter, SyslogLocal,>> =
LazyLock::new(||
{
SyncSyslog
::<DefaultSyslogFormatter, SyslogLocal>
::openlog_with(
Some("test1"),
LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
LogFacility::LOG_DAEMON,
SyslogLocal::new()
)
.unwrap()
}
);§Streaming
A stream is availble via [SyStreamApi].
let _ = write!(SYSLOG.stream(Priority::LOG_DEBUG), "test {} 123 stream test ", d);§Generics
-
F- a SyslogFormatter which sets the instance which would format the message. -
D- a SyslogDestination instance which is either: SyslogLocal, [SyslogFile], [SyslogNet], [SyslogTls]. By default aSyslogLocalis selected.
Implementations§
Source§impl SyncSyslog
impl SyncSyslog
Sourcepub fn openlog(
ident: Option<&str>,
logstat: LogStat,
facility: LogFacility,
net_tap_prov: DefaultLocalSyslogDestination,
) -> SyRes<Self>
pub fn openlog( ident: Option<&str>, logstat: LogStat, facility: LogFacility, net_tap_prov: DefaultLocalSyslogDestination, ) -> SyRes<Self>
Opens a default connection to the local syslog server with default formatter.
In order to access the syslog API, use the [SyslogApi].
§Arguments
-
ident- A program name which will appear on the logs. If none, will be determined automatically. -
logstat- LogStat an instance config. -
facility- LogFacility a syslog facility. -
net_tap_prov- a SyslogLocal instance with configuration.
§Returns
A SyRes is returned (Result) with:
-
Result::Ok - with instance
-
Result::Err - with error description.
Source§impl<F: SyslogFormatter, D: SyslogDestination> SyncSyslog<F, D>
impl<F: SyslogFormatter, D: SyslogDestination> SyncSyslog<F, D>
Sourcepub fn openlog_with(
ident: Option<&str>,
logstat: LogStat,
facility: LogFacility,
net_tap_prov: D,
) -> SyRes<Self>
pub fn openlog_with( ident: Option<&str>, logstat: LogStat, facility: LogFacility, net_tap_prov: D, ) -> SyRes<Self>
Opens a special connection to the destination syslog server with specific formatter.
All struct generic should be specified before calling this function.
In order to access the syslog API, use the [SyslogApi].
§Arguments
-
ident- A program name which will appear on the logs. If none, will be determined automatically. -
logstat- LogStat an instance config. -
facility- LogFacility a syslog facility. -
net_tap_prov- a destination server. A specificDinstance which contains infomation about the destination server. Seesyslog_provider.rs.
§Returns
A SyRes is returned (Result) with:
-
Result::Ok - with instance
-
Result::Err - with error description.
Source§impl<F: SyslogFormatter, D: SyslogDestination> SyncSyslog<F, D>
impl<F: SyslogFormatter, D: SyslogDestination> SyncSyslog<F, D>
Sourcepub fn connectlog(&self) -> SyRes<()>
pub fn connectlog(&self) -> SyRes<()>
Connects the current instance to the syslog server (destination).
Sourcepub fn setlogmask(&self, logmask: i32) -> SyRes<i32>
pub fn setlogmask(&self, logmask: i32) -> SyRes<i32>
Sets the logmask to filter out the syslog calls.
See macroses [LOG_MASK] and [LOG_UPTO] to generate mask
§Example
LOG_MASK!(Priority::LOG_EMERG) | LOG_MASK!(Priority::LOG_ERROR)
or
~(LOG_MASK!(Priority::LOG_INFO)) LOG_UPTO!(Priority::LOG_ERROR)
Sourcepub fn syslog(&self, pri: Priority, fmt: F) -> SyRes<()>
pub fn syslog(&self, pri: Priority, fmt: F) -> SyRes<()>
Similar to libc, syslog() sends data to syslog server.
§Arguments
-
pri- a priority Priority -
fmt- a formatter SyslogFormatter message. In C exists a functions with variable argumets amount. In Rust you should create your own macros like format!() or use format!()]. The String and ref'static[str] can be passed directly.
§Returns
A SyRes is returned which may describe an error.
Sourcepub fn change_identity(&self, ident: Option<&str>) -> SyRes<()>
pub fn change_identity(&self, ident: Option<&str>) -> SyRes<()>
This function can be used to update the facility name, for example after fork().
§Arguments
ident- an Option optional new identity (up to 48 UTF8 chars) If set to Option::None would request the program name from OS.
Sourcepub fn reconnect(&self) -> SyRes<()>
pub fn reconnect(&self) -> SyRes<()>
Re-opens the connection to the syslog server. Can be used to rotate logs(handle SIGHUP).
§Returns
-
Result::Ok - with empty inner type.
-
Result::Err - an error code and description
Sourcepub fn update_tap_data(&self, tap_data: D) -> SyRes<()>
pub fn update_tap_data(&self, tap_data: D) -> SyRes<()>
Updates the instance’s socket. tap_data [TapTypeData] should be of
the same variant (type) as current.
Source§impl<F, D> SyncSyslog<F, D>where
F: SyslogFormatter,
D: SyslogDestination,
impl<F, D> SyncSyslog<F, D>where
F: SyslogFormatter,
D: SyslogDestination,
Sourcepub fn get_stream<'t, PRI>(&'t self) -> SyStream<'t, PRI, D, F, &'t Self> ⓘwhere
PRI: SyStreamPri,
pub fn get_stream<'t, PRI>(&'t self) -> SyStream<'t, PRI, D, F, &'t Self> ⓘwhere
PRI: SyStreamPri,
Returns the streamable SyStream instance which can be used with write!.
It implements both std::fmt::Write and std::io::Write.
§Example
let log =
SingleSyslog::openlog(
Some("test1"),
LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
LogFacility::LOG_DAEMON,
SyslogLocal::new()
).unwrap();
write!(log.get_stream::<SyStreamPriDebug>(), "test stream singlesyslog {}", i).unwrap();Trait Implementations§
Source§impl<F, D> Clone for SyncSyslog<F, D>
impl<F, D> Clone for SyncSyslog<F, D>
Source§fn clone(&self) -> SyncSyslog<F, D>
fn clone(&self) -> SyncSyslog<F, D>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more