Expand description
syslog-rs
Since v 0.2.4 this project is relicensed with MPLv2.0. The contributors and authors agreed to change license: Aleksandr Morozov RELKOM s.r.o
An implementation of the syslog from glibc/libc like it was designed in in both system libraries. The API is almost compatible with what is in libc/glibc.
§Supports
- GNU/Linux RFC3164 (UTF-8 by default)
- *BSD and OSX RFC5424 (BOM UTF-8 by default)
Files:
- syslog_sync.rs - contains the thread-safe realization of the syslog (sync). Thread safe. The Atomic and crossbeam backoff are used for sync.
- syslog_async.rs - contains the async realization of the syslog (async) Thread safe. Tokio mutex are used for sync.
- syslog_sync_queue.rs - constains the sync realization, with asynchronious processing. The internal sych of crossbeam are used to push data to queue.
- syslog_sync_internal.rs - a
use_sync
anduse_sync_queue
common code. - unsafe_cell.rs - a file contains a Cell which can be used to share the syslog instance. See examples.
- portable.rs - all system level code which is portable
- common.rs - a common items mostly exported
- socket.rs - contains socket realization
- async_socket.rs - contains socket realization
- error.rs - an error wrapper and mapper
Features:
- feature = “use_async” for asynchronious code (use syslog_rs::sy_async::{Syslog};)
- feature = “use_sync” for synchronious code (use syslog_rs::sy_sync::{Syslog};)
- feature = “use_sync_queue” for synchronious with async processing (use syslog_rs::sy_async_queue::{Syslog};)
All features can be used simultaniously.
§Usage
syslog-rs = {version = “0.4”, default-features = false, features = [“use_sync”]}
By default, the following features are enabled: use_async, use_sync, use_sync_queue
use std::{sync::LazyLock, thread};
use std::time::Duration;
#[cfg(feature = "use_sync")]
use syslog_rs::sy_sync::{Syslog, SyslogStd};
use syslog_rs::{LogStat, LogFacility, Priority};
pub static SYSLOG: LazyLock<Syslog> = LazyLock::new(||
{
Syslog::openlog(
Some("example"),
LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
LogFacility::LOG_DAEMON
)
.unwrap()
}
);
macro_rules! logdebug
{
($($arg:tt)*) => (
SYSLOG.syslog(Priority::LOG_DEBUG, format!($($arg)*))
)
}
pub fn main()
{
logdebug!("test message!");
thread::sleep(Duration::from_micros(10));
return;
}
use tokio::time::{Duration, sleep};
use syslog_rs::sy_async::{AsyncSyslog, AsyncSyslogStd, AsyncSyslogExt};
use syslog_rs::{LogStat, LogFacility, Priority};
pub static SYSLOG: OnceCell<AsyncSyslog> = OnceCell::const_new();
macro_rules! logdebug
{
($($arg:tt)*) => (
SYSLOG.get().unwrap().syslog(Priority::LOG_DEBUG, format!($($arg)*)).await
)
}
#[tokio::main]
async fn main()
{
let syslog =
AsyncSyslog::openlog(
Some("example"),
LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
LogFacility::LOG_DAEMON
).await.unwrap();
SYSLOG.get_or_init(|| async { syslog }).await;
logdebug!("test message!");
SYSLOG.get().unwrap().vsyslog(Priority::LOG_DEBUG, "test 2").await;
sleep(Duration::from_micros(10)).await;
SYSLOG.get().unwrap().change_identity("new_identity").await;
logdebug!("test message new identity!");
sleep(Duration::from_micros(10)).await;
return;
}
Re-exports§
pub use sync::syslog_sync as sy_sync;
pub use a_sync::syslog_async as sy_async;
pub use sync::syslog_sync_queue as sy_sync_queue;
pub use common::*;