syslog_rs/lib.rs
1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 *
4 * Copyright 2025 Aleksandr Morozov
5 *
6 * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
7 * the European Commission - subsequent versions of the EUPL (the "Licence").
8 *
9 * You may not use this work except in compliance with the Licence.
10 *
11 * You may obtain a copy of the Licence at:
12 *
13 * https://joinup.ec.europa.eu/software/page/eupl
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the Licence is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 * Licence for the specific language governing permissions and limitations
19 * under the Licence.
20 */
21
22//! syslog-rs
23//!
24//! Since v 0.2.4 this project is relicensed with MPLv2.0.
25//! The contributors and authors agreed to change license:
26//! Aleksandr Morozov
27//! RELKOM s.r.o
28//!
29//! An implementation of the syslog from glibc/libc like it was designed in
30//! in both system libraries. The API is almost compatible with what is in
31//! libc/glibc.
32//!
33//! ## Supports
34//!
35//! - GNU/Linux RFC3164 (UTF-8 by default)
36//! - *BSD and OSX RFC5424 (BOM UTF-8 by default)
37//!
38//! Files:
39//! - syslog_sync.rs - contains the thread-safe realization of the syslog (sync).
40//! Thread safe.
41//! - syslog_async.rs - contains the async realization of the syslog (async)
42//! Thread safe. Tokio mutex are used for sync.
43//! - syslog_sync_queue.rs - constains the sync realization, with asynchronious
44//! processing. Can be used to pair sync and async into single syslog instance.
45//! - syslog_sync_internal.rs - a `use_sync` and `use_sync_queue` common code.
46//! - unsafe_cell.rs - a file contains a Cell which can be used to share the syslog
47//! instance. See examples.
48//! - portable.rs - all system level code which is portable
49//! - common.rs - a common items mostly exported
50//! - socket.rs - contains socket realization
51//! - async_socket.rs - contains socket realization
52//! - error.rs - an error wrapper and mapper
53//!
54//! Features:
55//! - feature = "use_async" for asynchronious code (use syslog_rs::sy_async::{Syslog};)
56//! - feature = "use_sync" for synchronious code (use syslog_rs::sy_sync::{Syslog};)
57//! - feature = "use_sync_queue" for synchronious with async processing (use syslog_rs::sy_async_queue::{Syslog};)
58//! - feature = "build_with_net" adds network syslog support
59//! - feature = "build_with_file" writing to local file feature
60//! - feature = "build_with_tls" writing to TLS syslog server
61//!
62//! All features can be used simultaniously.
63//! # Usage
64//!
65//! syslog-rs = {version = "0.4", default-features = false, features = ["use_sync"]}
66//!
67//! By default, the following features are enabled: use_async, use_sync, use_sync_queue
68//!
69//! ```
70//! use std::{sync::LazyLock, thread};
71//! use std::time::Duration;
72//!
73//! #[cfg(feature = "use_sync")]
74//! use syslog_rs::sy_sync::Syslog;
75//!
76//! use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
77//!
78//! pub static SYSLOG: LazyLock<Syslog> = LazyLock::new(||
79//! {
80//! Syslog::openlog(
81//! Some("example"),
82//! LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
83//! LogFacility::LOG_DAEMON, SyslogLocal::new()
84//! )
85//! .unwrap()
86//! }
87//! );
88//!
89//!
90//! macro_rules! logdebug
91//! {
92//! ($($arg:tt)*) => (
93//! SYSLOG.syslog(Priority::LOG_DEBUG, format!($($arg)*))
94//! )
95//! }
96//!
97//! pub fn main()
98//! {
99//! logdebug!("test message1!");
100//!
101//! SYSLOG.change_identity("example2").unwrap();
102//!
103//! logdebug!("test message from new ident");
104//!
105//! thread::sleep(Duration::from_micros(10));
106//!
107//! return;
108//! }
109//! ```
110//! ```
111//! use syslog_rs::sy_async::AsyncSyslog;
112//! use tokio::sync::OnceCell;
113//! use tokio::time::{Duration, sleep};
114//!
115//! use syslog_rs::{LogFacility, LogStat, Priority, SyslogLocal};
116//!
117//!
118//! pub static SYSLOG: OnceCell<AsyncSyslog> = OnceCell::const_new();
119//!
120//!
121//! macro_rules! logdebug
122//! {
123//! ($($arg:tt)*) => (
124//! SYSLOG.get().unwrap().syslog(Priority::LOG_DEBUG, format!($($arg)*)).await
125//! )
126//! }
127//!
128//! #[tokio::main]
129//! async fn main()
130//! {
131//! let syslog =
132//! AsyncSyslog::openlog(
133//! Some("example"),
134//! LogStat::LOG_CONS | LogStat::LOG_NDELAY | LogStat::LOG_PID,
135//! LogFacility::LOG_DAEMON,
136//! SyslogLocal::new()
137//! ).await.unwrap();
138//!
139//!
140//! SYSLOG.get_or_init(|| async { syslog }).await;
141//!
142//!
143//! logdebug!("test message async start!");
144//!
145//! SYSLOG.get().unwrap().vsyslog(Priority::LOG_DEBUG, "test 2").await;
146//!
147//! sleep(Duration::from_micros(10)).await;
148//!
149//! SYSLOG.get().unwrap().change_identity("new_identity").await.unwrap();
150//!
151//! logdebug!("test message new identity!");
152//!
153//! sleep(Duration::from_micros(10)).await;
154//!
155//! logdebug!("test 123!");
156//! logdebug!("test 123123! end ");
157//! return;
158//! }
159//!
160//! ```
161
162//-- REEXPORT --
163#[macro_use]
164extern crate bitflags;
165pub extern crate chrono;
166extern crate nix;
167
168#[cfg(all(any(feature = "use_sync", feature = "use_sync_queue"), feature = "build_with_tls"))]
169extern crate rustls;
170
171#[cfg(all(feature = "use_async", feature = "build_with_tls"))]
172extern crate tokio_rustls;
173
174//-- ASYNC --
175//#[cfg(feature = "use_async")]
176//extern crate async_recursion;
177#[cfg(feature = "use_async")]
178extern crate async_trait;
179#[cfg(any(feature = "use_async", feature = "use_sync_queue"))]
180extern crate tokio;
181
182//-- SYNC --
183#[cfg(any(feature = "use_sync", feature = "use_sync_queue"))]
184pub mod sync;
185#[cfg(feature = "use_async")]
186pub mod a_sync;
187
188pub mod formatters;
189
190pub mod syslog_provider;
191pub use syslog_provider::*;
192
193
194//-- NON EXPORT
195mod portable;
196
197pub mod socket;
198
199//-- PUBLIC
200
201#[macro_use]
202pub mod common;
203#[macro_use]
204pub mod error;
205
206pub use socket::TapType;
207
208#[cfg(feature = "use_sync")]
209pub use sync::syslog as sy_sync;
210#[cfg(feature = "use_sync")]
211pub use sync::syslog_sync_shared::SyslogShared;
212
213#[cfg(feature = "use_async")]
214pub use a_sync::syslog_async as sy_async;
215#[cfg(feature = "use_async")]
216pub use a_sync::syslog_async_shared::AsyncSyslogShared;
217#[cfg(all(feature = "use_async", feature = "use_sync_queue"))]
218pub use a_sync::syslog_async_queue::AsyncSyslogQueue;
219
220#[cfg(feature = "use_sync_queue")]
221pub use sync::syslog_sync_queue as sy_sync_queue;
222#[cfg(feature = "use_sync_queue")]
223pub use sync::syslog_sync_queue::SyslogQueue;
224
225pub use common::*;