Crate syslog_rs

Source
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.
  • 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. Can be used to pair sync and async into single syslog instance.
  • syslog_sync_internal.rs - a use_sync and use_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};)
  • feature = “build_with_net” adds network syslog support
  • feature = “build_with_file” writing to local file feature
  • feature = “build_with_tls” writing to TLS syslog server

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;
 
use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
 
pub static SYSLOG: LazyLock<Syslog> = LazyLock::new(|| 
   {
       Syslog::openlog(
           Some("example"), 
           LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID, 
           LogFacility::LOG_DAEMON, SyslogLocal::new()
       )
       .unwrap()
   }
);
 
 
macro_rules! logdebug 
{
    ($($arg:tt)*) => (
        SYSLOG.syslog(Priority::LOG_DEBUG, format!($($arg)*))
    )
}
 
pub fn main()
{
    logdebug!("test message1!");
 
    SYSLOG.change_identity("example2").unwrap();
 
    logdebug!("test message from new ident");
 
    thread::sleep(Duration::from_micros(10));
 
    return;
}
use syslog_rs::sy_async::AsyncSyslog;
use tokio::sync::OnceCell;
use tokio::time::{Duration, sleep};
 
use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
 
 
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,
            SyslogLocal::new()
        ).await.unwrap();
 
 
    SYSLOG.get_or_init(|| async { syslog }).await;
 
 
    logdebug!("test message async start!");
     
    SYSLOG.get().unwrap().vsyslog(Priority::LOG_DEBUG, "test 2").await;
 
    sleep(Duration::from_micros(10)).await;
 
    SYSLOG.get().unwrap().change_identity("new_identity").await.unwrap();
 
    logdebug!("test message new identity!");
 
    sleep(Duration::from_micros(10)).await;
 
    logdebug!("test 123!");
    logdebug!("test 123123! end ");
    return;
}
 

Re-exports§

pub extern crate chrono;
pub use socket::TapType;
pub use sync::syslog as sy_sync;
pub use sync::syslog_sync_shared::SyslogShared;
pub use a_sync::syslog_async as sy_async;
pub use a_sync::syslog_async_shared::AsyncSyslogShared;
pub use a_sync::syslog_async_queue::AsyncSyslogQueue;
pub use sync::syslog_sync_queue as sy_sync_queue;
pub use sync::syslog_sync_queue::SyslogQueue;
pub use syslog_provider::*;
pub use common::*;

Modules§

a_sync
common
error
formatters
socket
sync
syslog_provider

Macros§

LOG_MASK
LOG_MASK is used to create the priority mask in setlogmask. For a single Priority mask used with Priority can be used with | & ! bit operations LOG_MASK()
LOG_UPTO
LOG_MASK is used to create the priority mask in setlogmask For a mask UPTO specified used with Priority
map_error
map_error_code
map_error_os
throw_error
throw_error_code
throw_error_errno
throw_error_os