Skip to main content

syslog_rs/
lib.rs

1/*-
2 * syslog-rs - a syslog client translated from libc to rust
3 * 
4 * Copyright 2025(c) 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
16//! syslog-rs
17//! 
18//!  <img src="https://cdn.4neko.org/syslog_logo.webp" width="280"/> <img src="https://cdn.4neko.org/source_avail.webp" width="280"/> <img src="https://cdn.4neko.org/mit_mpl_eupl_2.webp" width="280"/>
19//!  <br/>
20//!  <img src="https://codeberg.org/4neko/syslog-rs/badges/workflows/multi-os.yml/badge.svg" />
21//! An implementation of the syslog from glibc/libc like it was designed in
22//! in both system libraries. The API is almost compatible with what is in
23//! libc/glibc.
24//! 
25//! Also supports windows Event Log and/or remote syslog server.
26//! 
27//! ## Supports
28//! 
29//! * GNU/Linux RFC3164 (UTF-8 by default)
30//! * *BSD and OSX RFC5424 (BOM UTF-8 by default) (including msgid and parametes).
31//! * Windows Event Log via `WindowsEvent` provider and `RFC3164`/`RFC5424` via network or file.
32//! * Tokio async
33//! * Smol async
34//! * TLS over TCP syslog server connection
35//! * TCP/UDP syslog server connection
36//! * Local file writer
37//! * Logger
38//! 
39//! Directories:
40//! * a_sync - all async code
41//! * formatters - syslog message formatter.
42//! * sync - all sync code
43//! * src - all common code for both sync and async.
44//! 
45//! Features: 
46//! * feature = `build_with_thread_local` enables the lockless syslog client for single threaded or `thread_local` instances.
47//! * feature = "build_sync"  a base feature which enables all SYNC code. Just enabling the `use_sync`
48//!      would enable only basic SharedSyslog.
49//! * feature = "build_async_tokio" a Tokio based async IO. Includes the tokio and tokio-rustls deps.
50//! * feature = "build_async_smol" for synchronious with async processing (use syslog_rs::sy_async_queue::{Syslog};)
51//! * feature = "build_async_interface" for asynchronious interface only without any realisations.
52//! * feature = "build_with_queue" a Smol based async IO. Includes the smol and futures-rustls dep.
53//!     moves the syslog client to separate thread and by default uses 
54//!     crossbeam-channel to receive messages.
55//!     Depending on which async procvider is selected, the channel realization 
56//!     will be switched suring compilation. In this case, an async code can be 
57//!     attached to the sync queue which allows to write to the same syslog
58//!     connection with the same setup.
59//! * feature = "build_ext_net" adds the networking support! (TCP, UDP)
60//! * feature = "build_ext_tls" adds the TLS support over network (TCP).
61//! * feature = "build_ext_file" writing directly to file.
62//! * feature = "build_with_logger" enables the ready to use logger. See below.
63//! 
64//! --
65//! * feature = "udp_truncate_1024_bytes" - Forces truncation of the (whole) syslog message to 1024
66//! * feature = udp_truncate_1440_bytes - Forces truncation of the (whole) syslog message to 1440
67//! * feature = tcp_truncate_1024_bytes - Forces truncation of the (whole) syslog message to 1024
68//! * feature = tcp_truncate_2048_bytes - Forces truncation of the (whole) syslog message to 2048 
69//! * feature = tcp_truncate_4096_bytes - Forces truncation of the (whole) syslog message to 4096
70//! * feature = tcp_truncate_max_bytes - Forces truncation of the (whole) syslog message to 8192 (max)
71//! * feature = dgram_sysctl_failure_panic - BSD only! By default, if crate fails to obtain a sysctl net.local.dgram.maxdgram, 
72//!     it returns a default value 2048. If this feature is enabled, then it will panic.
73//! 
74//! `build_async_tokio` and `build_async_smol` cannot be used together.
75//! 
76//! `build_with_queue` behaves differently if enabled ine of the async providers.
77//! 
78//! **For mor info, please see README.md!**
79//! 
80//! # Usage Example
81//! 
82//! syslog-rs = {version = "7.0", default-features = false, features = ["build_sync", "truncate_default", "build_with_logger"]}
83//! 
84//! By default, the following features are enabled: `build_sync`, `truncate_default`, `build_with_logger`
85//! 
86//! ## Examples
87//! 
88//! See the directory `./examples`
89//! 
90//! ## build_with_logger
91//! 
92//! `logger.rs`
93//!  
94//! A logger can be controlled via environmental variables:
95//! 
96//! * `SYSLOG_LOGGER_DST_URI` - enables the logger and configures it.
97//! 
98//! * `SYSLOG_LOGGER_LOG_FACILITY` - optional, overrides the default which is LOG_DAEMON
99//! 
100//! * `SYSLOG_LOGGER_LOG_FORMATTER` - optional, overrides the default formatter dep on the OS.
101//! 
102//! * `SYSLOG_LOGGER_LOG_IDENTITY` - optional, if not set, will be determined automatically.
103//! 
104//! The vars can have suffix, for example `XIO`, so SYSLOG_LOGGER_DST_URI_XIO, SYSLOG_LOGGER_LOG_FACILITY_XIO, ...
105//! 
106//! In the crate or executable, a logger should be initialized. It can be done using:
107//! 
108//! ```ignore
109//!     init_event_log(None).unwrap();
110//! ```
111//! 
112//! if `suffix` was used then it should be specified:
113//! 
114//! ```ignore
115//!     init_event_log(Some("XIO")).unwrap();
116//! ```
117//! 
118//! Below are the macroses and functions which can be used to send messages
119//! 
120//! ```ignore
121//!     logger_dbg!("debug: test_logger_simple");
122//!     logger_info!("info: test1test_logger_simple23");
123//!     logger_error!("error: test_logger_simple");
124//!     logger_warn!("warn: test_logger_simple");
125//!     logger_emerg!("emerg: test_logger_simple");
126//!     logger_custom!(Priority::LOG_EMERG, "custom");
127//!     logger_log(Priority::LOG_EMERG, format!("abc"));
128//! ```
129//! 
130//! Also the logger can be attached to the existing syslog instance i.e in the larger work:
131//! 
132//! Example:
133//! ```ignore
134//!     attach_event_log_shared(&syslog).unwrap()
135//! ```
136//! 
137//! attaches the crate's syslog to the created one in a larger program. But the crate which
138//! uses logger must provide a function which would call this routine internally!
139//! 
140//! There are two attach methods: 
141//! 
142//! * `attach_event_log_shared` - for a [SyShared] instance 
143//! 
144//! * `attach_event_log_queued` - for a [SyQueue] instance
145//! 
146
147// make sure that the smol and tokio are not used together.
148#[cfg(all(feature = "build_async_tokio", feature = "build_async_smol"))]
149compile_error!("use_async_tokio and use_async_smol can not be used at the same time");
150
151// make sure that the build_async_interface and either async_embedded are not used together.
152#[cfg(all(feature = "build_async_interface", feature = "async_embedded"))]
153compile_error!("build_async_interface and either tokio/smol can not be used at the same time");
154
155//-- REEXPORT --
156#[macro_use] 
157extern crate bitflags;
158pub extern crate chrono;
159extern crate socket2;
160
161
162#[cfg(target_family = "unix")]
163pub extern crate nix;
164
165/// winDAws crp...
166#[cfg(target_family = "windows")]
167pub extern crate windows;
168
169
170// -- TLS --
171
172#[cfg(all(feature = "build_sync", feature = "build_ext_tls"))]
173extern crate rustls;
174
175#[cfg(all(feature = "build_async_tokio", feature = "build_ext_tls"))]
176extern crate tokio_rustls;
177
178#[cfg(all(feature = "build_async_smol", feature = "build_ext_tls"))]
179extern crate futures_rustls;
180
181// -- ASYNC --
182
183#[cfg(feature = "build_async_tokio")]
184extern crate tokio;
185
186#[cfg(feature = "build_async_smol")]
187extern crate smol;
188
189// -- SYNC --
190#[cfg(feature = "build_with_queue")]
191extern crate crossbeam_channel;
192
193#[cfg(feature = "build_sync")]
194extern crate crossbeam_utils;
195
196/// A sync syslog realization including shared and queued syslog client.
197#[cfg(feature = "build_sync")]
198pub mod sync;
199
200/// An async interface and/or realisations. The main async syslog struct is located there.
201#[cfg(feature = "async_enabled")]
202pub mod a_sync;
203
204/// A message formatters i.e RFC5424 and other. Also a message formatter interface.
205pub mod formatters;
206
207pub mod syslog_provider;
208
209/// A syslog destnation. i.e local, remote, remote TLS, local file. Needed to initialize the 
210/// (both sync and async) instances.
211pub use syslog_provider::*;
212
213/// A common things for the sockets managment.
214pub mod socket;
215
216/// A logger which can be setup from env.
217#[cfg(all(unix, feature = "build_with_logger"))]
218pub mod logger;
219
220/// A common code which holds all flags and macroses.
221#[macro_use] 
222pub mod common;
223
224/// Unified error interface.
225#[macro_use] 
226pub mod error;
227
228
229//-- NON EXPORT
230/// A portable code which is different for different OSes.
231mod portable;
232
233/// Public use of the type of the tap.
234pub use socket::TapType;
235
236/// A streamable sync instance (both).
237#[cfg(feature = "build_sync")]
238pub use sync::{syslog_sync::{Syslog, SyShared, DynSyslog}, SyStreamPriWarning, SyStreamPriNotice, SyStreamPriInfo,
239    SyStreamPriEmerg, SyStreamPriCrit, SyStreamPriAlert, SyStreamPriDebug, SyStreamPriErr,
240    DefaultLocalSyslogDestination};
241
242#[cfg(feature = "build_with_logger")]
243pub use logger::{init_event_log, logger_log, attach_event_log_shared};
244
245#[cfg(all(feature = "build_with_logger", feature = "build_with_queue"))]
246pub use logger::attach_event_log_queued;
247
248/// A `sy_async` is a shortcut to the file `syslog_async.rs` in dir `a_sync`.
249#[cfg(feature = "async_enabled")]
250pub use a_sync::syslog_async as sy_async;
251
252/// A main async syslog instance.
253#[cfg(feature = "async_enabled")]
254pub use sy_async::AsyncSyslog;
255
256/// A shortcut for the internals of the `async_syslog` and IO for user implementation.
257#[cfg(feature = "build_async_interface")]
258pub use a_sync::syslog_async_internal::{AsyncSyslogInternal, AsyncSyslogInternalIO};
259
260/// A shortcut to the trait `AsyncMutex` which unifies the mutex realiztion of different 
261/// exec environments.
262#[cfg(feature = "build_async_interface")]
263pub use a_sync::syslog_async_internal::AsyncMutex;
264
265/// Use this trait to access queue interface for async code.
266#[cfg(all(feature = "async_enabled", feature = "build_with_queue"))]
267pub use a_sync::syslog_trait::AsyncSyslogQueueApi;
268
269
270/// A shortcut to the file `syslog_sync_queue.rs` in dir `sync`.
271#[cfg(feature = "build_with_queue")]
272pub use sync::syslog_sync_queue as sy_sync_queue;
273
274/// A (previosuly SyslogQueue) queued sync type of the syslog client. Also async queue.
275#[cfg(feature = "build_with_queue")]
276pub use sync::syslog_sync::SyQueued;
277
278#[cfg(feature = "build_with_thread_local")]
279pub use sync::syslog_sync::SyThreadLocal;
280
281/// A shortcut use of all in the `common.rs`.
282pub use common::*;