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="220"/> <img src="https://cdn.4neko.org/source_avail.webp" width="220"/> <img src="https://cdn.4neko.org/mit_mpl_eupl_2.webp" width="220"/>
19//! <br/>
20//! <a href = "https://codeberg.org/4neko/syslog-rs/actions"><img src="https://codeberg.org/4neko/syslog-rs/badges/workflows/linux-os.yml/badge.svg" /></a>
21//! <a href = "https://codeberg.org/4neko/syslog-rs/actions"><img src="https://codeberg.org/4neko/syslog-rs/badges/workflows/freebsd-os.yml/badge.svg" /></a>
22//! <a href = "https://codeberg.org/4neko/syslog-rs/actions"><img src="https://codeberg.org/4neko/syslog-rs/badges/workflows/openbsd-os.yml/badge.svg" /></a>
23//! <a href = "https://codeberg.org/4neko/syslog-rs/actions"><img src="https://codeberg.org/4neko/syslog-rs/badges/workflows/netbsd-os.yml/badge.svg" /></a>
24//! <a href = "https://codeberg.org/4neko/syslog-rs/actions"><img src="https://codeberg.org/4neko/syslog-rs/badges/workflows/linux-os-arm.yml/badge.svg" /></a>
25//!
26//! An implementation of the syslog from glibc/libc like it was designed in
27//! in both system libraries. The API is almost compatible with what is in
28//! libc/glibc.
29//!
30//! Also supports windows Event Log and/or remote syslog server.
31//!
32//! ## Supports
33//!
34//! * GNU/Linux RFC3164 (UTF-8 by default)
35//! * *BSD and OSX RFC5424 (BOM UTF-8 by default) (including msgid and parametes).
36//! * Windows Event Log via `WindowsEvent` provider and `RFC3164`/`RFC5424` via network or file.
37//! * Tokio async
38//! * Smol async
39//! * TLS over TCP syslog server connection
40//! * TCP/UDP syslog server connection
41//! * Local file writer
42//! * Logger
43//!
44//! Directories:
45//! * a_sync - all async code
46//! * formatters - syslog message formatter.
47//! * sync - all sync code
48//! * src - all common code for both sync and async.
49//!
50//! Features:
51//! * feature = `build_with_thread_local` enables the lockless syslog client for single threaded or `thread_local` instances.
52//! * feature = "build_sync" a base feature which enables all SYNC code. Just enabling the `use_sync`
53//! would enable only basic SharedSyslog.
54//! * feature = "build_async_tokio" a Tokio based async IO. Includes the tokio and tokio-rustls deps.
55//! * feature = "build_async_smol" for synchronious with async processing (use syslog_rs::sy_async_queue::{Syslog};)
56//! * feature = "build_async_interface" for asynchronious interface only without any realisations.
57//! * feature = "build_with_queue" a Smol based async IO. Includes the smol and futures-rustls dep.
58//! moves the syslog client to separate thread and by default uses
59//! crossbeam-channel to receive messages.
60//! Depending on which async procvider is selected, the channel realization
61//! will be switched suring compilation. In this case, an async code can be
62//! attached to the sync queue which allows to write to the same syslog
63//! connection with the same setup.
64//! * feature = "build_ext_net" adds the networking support! (TCP, UDP)
65//! * feature = "build_ext_tls" adds the TLS support over network (TCP).
66//! * feature = "build_ext_file" writing directly to file.
67//! * feature = "build_with_logger" enables the ready to use logger. See below.
68//! * feature = "logger_lazy" switches the mode to the lazy init which does not require to call
69//! init_* method from the code. However, it is not possible to attach the logging to the
70//! created instance outside of the crate.
71//! * feature = "logger_lazy_silence_panic" changes the behaviour in case of error when the connection to
72//! the syslog server is established.
73//!
74//! --
75//! * feature = "udp_truncate_1024_bytes" - Forces truncation of the (whole) syslog message to 1024
76//! * feature = udp_truncate_1440_bytes - Forces truncation of the (whole) syslog message to 1440
77//! * feature = tcp_truncate_1024_bytes - Forces truncation of the (whole) syslog message to 1024
78//! * feature = tcp_truncate_2048_bytes - Forces truncation of the (whole) syslog message to 2048
79//! * feature = tcp_truncate_4096_bytes - Forces truncation of the (whole) syslog message to 4096
80//! * feature = tcp_truncate_max_bytes - Forces truncation of the (whole) syslog message to 8192 (max)
81//! * feature = dgram_sysctl_failure_panic - BSD only! By default, if crate fails to obtain a sysctl net.local.dgram.maxdgram,
82//! it returns a default value 2048. If this feature is enabled, then it will panic.
83//!
84//! `build_async_tokio` and `build_async_smol` cannot be used together.
85//!
86//! `build_with_queue` behaves differently if enabled ine of the async providers.
87//!
88//! **For mor info, please see README.md!**
89//!
90//! # Usage Example
91//!
92//! syslog-rs = {version = "7.0", default-features = false, features = ["build_sync", "truncate_default", "build_with_logger"]}
93//!
94//! By default, the following features are enabled: `build_sync`, `truncate_default`, `build_with_logger`
95//!
96//! ## Examples
97//!
98//! See the directory `./examples`
99//!
100//! # build_with_logger
101//!
102//! `logger.rs`
103//!
104//! A logger can be controlled via environmental variables:
105//!
106//! * `SYSLOG_LOGGER_DST_URI` - enables the logger and configures it.
107//!
108//! * `SYSLOG_LOGGER_LOG_FACILITY` - optional, overrides the default which is LOG_DAEMON
109//!
110//! * `SYSLOG_LOGGER_LOG_FORMATTER` - optional, overrides the default formatter dep on the OS.
111//!
112//! * `SYSLOG_LOGGER_LOG_IDENTITY` - optional, if not set, will be determined automatically.
113//!
114//! * `SYSLOG_LOGGER_LOG_UPTO` - optional, sets the LOGUPTO priority.
115//!
116//! The vars can have suffix, for example `XIO`, so SYSLOG_LOGGER_DST_URI_XIO, SYSLOG_LOGGER_LOG_FACILITY_XIO, ...
117//!
118//! In the crate or executable, a logger should be initialized when using non-lazy mode.
119//! Below initializes the event log in shared mode.
120//!
121//! ```ignore
122//! init_event_log(None).unwrap();
123//! ```
124//!
125//! if `suffix` was used then it should be specified:
126//!
127//! ```ignore
128//! init_event_log(Some("XIO")).unwrap();
129//! ```
130//!
131//! Below initializes the event log in thread local mode:
132//!
133//! ```ignore
134//! init_thread_local_event_log(None).unwrap();
135//! ```
136//!
137//! In the `lazy` mode, it is not required to call init_*.
138//!
139//! Below are the macroses and functions which can be used to send messages
140//!
141//! ```ignore
142//! logger_dbg!("debug: test_logger_simple");
143//! logger_info!("info: test1test_logger_simple23");
144//! logger_error!("error: test_logger_simple");
145//! logger_warn!("warn: test_logger_simple");
146//! logger_emerg!("emerg: test_logger_simple");
147//! logger_custom!(Priority::LOG_EMERG, "custom");
148//! logger_log(Priority::LOG_EMERG, format!("abc"));
149//! ```
150//!
151//! Also the logger can be attached to the existing syslog instance i.e in the larger work:
152//!
153//! Example:
154//! ```ignore
155//! attach_event_log_shared(&syslog).unwrap()
156//! ```
157//!
158//! attaches the crate's syslog to the created one in a larger program. But the crate which
159//! uses logger must provide a function which would call this routine internally! It is not
160//! possible when crate is compiled in the lazy mode.
161//!
162//! There are two attach methods:
163//!
164//! * `attach_event_log_shared` - for a [SyShared] instance
165//!
166//! * `attach_event_log_queued` - for a [SyQueue] instance
167//!
168//! ### SYSLOG_LOGGER_DST_URI
169//!
170//! #### Local
171//! * `local#` - for default
172//!
173//! * `local#/var/run/syslogcustom` - for custom path
174//!
175//! Example:
176//!
177//! ```text
178//! SYSLOG_LOGGER_DST_URI="local#"
179//! SYSLOG_LOGGER_DST_URI="local#/var/run/log"
180//! ```
181//!
182//! #### UDP/TCP
183//!
184//! * `udp#127.0.0.1:80` - for IPv4 + port
185//!
186//! * `udp#[fe00:::0]:80` - for IPv6 + port
187//!
188//! * `udp#www.mydomain.tdl:80` - for hostname + port
189//!
190//! * `tcp#127.0.0.1:80` - for IPv4 + port
191//!
192//! Example:
193//!
194//! ```text
195//! SYSLOG_LOGGER_DST_URI="udp#127.0.0.1:80"
196//! ```
197//!
198//! #### TLS
199//!
200//! * `tls#127.0.0.1:80#servername#root_cert_path` - for IPv4 + port
201//!
202//! * `tls#[fe00:::0]:80#servername#root_cert_path` - for IPv6 + port
203//!
204//! * `tls#www.mydomain.tdl:80#servername#root_cert_path` - for hostname + port
205//!
206//! Example:
207//!
208//! ```text
209//! SYSLOG_LOGGER_DST_URI="tls#127.0.0.1:80#prod0.syslogse.tld#/etc/ssl/rootcerts.crt"
210//! ```
211//!
212//! #### FILE
213//!
214//! * `file#/tmp/file1.txt` - for file append mode
215//!
216//! Example:
217//!
218//! ```text
219//! SYSLOG_LOGGER_DST_URI="file#/tmp/file1.txt"
220//! ```
221//!
222//! ### SYSLOG_LOGGER_LOG_FACILITY
223//!
224//! Default: LOG_DAEMON
225//!
226//! ```text
227//! LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS,
228//! LOG_UUCP, LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5,
229//! LOG_LOCAL6, LOG_LOCAL7
230//! ```
231//!
232//! ### SYSLOG_LOGGER_LOG_FORMATTER
233//!
234//! Default: autoselected
235//!
236//! ```text
237//! RFC3146, RFC5424, FILE
238//! ```
239//!
240//! ! Don't use `FILE` with syslog server, this is for local file only.
241//!
242//! ### SYSLOG_LOGGER_LOG_UPTO
243//!
244//! ```text
245//! LOG_ALERT, LOG_CRIT, LOG_DEBUG, LOG_EMERG, LOG_ERR, LOG_INFO, LOG_NOTICE, LOG_WARNING
246//! ```
247//!
248
249// make sure that the smol and tokio are not used together.
250#[cfg(all(feature = "build_async_tokio", feature = "build_async_smol"))]
251compile_error!("use_async_tokio and use_async_smol can not be used at the same time");
252
253// make sure that the build_async_interface and either async_embedded are not used together.
254#[cfg(all(feature = "build_async_interface", feature = "async_embedded"))]
255compile_error!("build_async_interface and either tokio/smol can not be used at the same time");
256
257//-- REEXPORT --
258#[macro_use]
259extern crate bitflags;
260pub extern crate chrono;
261extern crate socket2;
262
263
264#[cfg(target_family = "unix")]
265pub extern crate nix;
266
267/// winDAws crp...
268#[cfg(target_family = "windows")]
269pub extern crate windows;
270
271
272// -- TLS --
273
274#[cfg(all(feature = "build_sync", feature = "build_ext_tls"))]
275extern crate rustls;
276
277#[cfg(all(feature = "build_async_tokio", feature = "build_ext_tls"))]
278extern crate tokio_rustls;
279
280#[cfg(all(feature = "build_async_smol", feature = "build_ext_tls"))]
281extern crate futures_rustls;
282
283// -- ASYNC --
284
285#[cfg(feature = "build_async_tokio")]
286extern crate tokio;
287
288#[cfg(feature = "build_async_smol")]
289extern crate smol;
290
291// -- SYNC --
292#[cfg(feature = "build_with_queue")]
293extern crate crossbeam_channel;
294
295#[cfg(feature = "build_sync")]
296extern crate crossbeam_utils;
297
298/// A sync syslog realization including shared and queued syslog client.
299#[cfg(feature = "build_sync")]
300pub mod sync;
301
302/// An async interface and/or realisations. The main async syslog struct is located there.
303#[cfg(feature = "async_enabled")]
304pub mod a_sync;
305
306/// A message formatters i.e RFC5424 and other. Also a message formatter interface.
307pub mod formatters;
308
309pub mod syslog_provider;
310
311/// A syslog destnation. i.e local, remote, remote TLS, local file. Needed to initialize the
312/// (both sync and async) instances.
313pub use syslog_provider::*;
314
315/// A common things for the sockets managment.
316pub mod socket;
317
318/// A logger which can be setup from env.
319#[cfg(all(unix, feature = "build_with_logger"))]
320pub mod logger;
321
322/// A common code which holds all flags and macroses.
323#[macro_use]
324pub mod common;
325
326/// Unified error interface.
327#[macro_use]
328pub mod error;
329
330
331//-- NON EXPORT
332/// A portable code which is different for different OSes.
333mod portable;
334
335/// Public use of the type of the tap.
336pub use socket::TapType;
337
338/// A streamable sync instance (both).
339#[cfg(feature = "build_sync")]
340pub use sync::{syslog_sync::{Syslog, SyShared, DynSyslog}, SyStreamPriWarning, SyStreamPriNotice, SyStreamPriInfo,
341 SyStreamPriEmerg, SyStreamPriCrit, SyStreamPriAlert, SyStreamPriDebug, SyStreamPriErr,
342 DefaultLocalSyslogDestination};
343
344#[cfg(feature = "build_with_logger")]
345pub use logger::{logger_log};
346
347#[cfg(all(feature = "build_with_logger", not(feature = "logger_lazy")))]
348pub use logger::init_event_log;
349
350#[cfg(all(feature = "build_with_logger", not(feature = "logger_lazy")))]
351pub use logger::attach_event_log_shared;
352
353#[cfg(all(feature = "build_with_logger", feature = "build_with_queue", not(feature = "logger_lazy")))]
354pub use logger::attach_event_log_queued;
355
356#[cfg(all(feature = "build_with_logger", feature = "build_with_thread_local", not(feature = "logger_lazy")))]
357pub use logger::init_thread_local_event_log;
358
359/// A `sy_async` is a shortcut to the file `syslog_async.rs` in dir `a_sync`.
360#[cfg(feature = "async_enabled")]
361pub use a_sync::syslog_async as sy_async;
362
363/// A main async syslog instance.
364#[cfg(feature = "async_enabled")]
365pub use sy_async::AsyncSyslog;
366
367/// A shortcut for the internals of the `async_syslog` and IO for user implementation.
368#[cfg(feature = "build_async_interface")]
369pub use a_sync::syslog_async_internal::{AsyncSyslogInternal, AsyncSyslogInternalIO};
370
371/// A shortcut to the trait `AsyncMutex` which unifies the mutex realiztion of different
372/// exec environments.
373#[cfg(feature = "build_async_interface")]
374pub use a_sync::syslog_async_internal::AsyncMutex;
375
376/// Use this trait to access queue interface for async code.
377#[cfg(all(feature = "async_enabled", feature = "build_with_queue"))]
378pub use a_sync::syslog_trait::AsyncSyslogQueueApi;
379
380
381/// A shortcut to the file `syslog_sync_queue.rs` in dir `sync`.
382#[cfg(feature = "build_with_queue")]
383pub use sync::syslog_sync_queue as sy_sync_queue;
384
385/// A (previosuly SyslogQueue) queued sync type of the syslog client. Also async queue.
386#[cfg(feature = "build_with_queue")]
387pub use sync::syslog_sync::SyQueued;
388
389#[cfg(feature = "build_with_thread_local")]
390pub use sync::syslog_sync::{SyThreadLocal, MutDynSyslog};
391
392/// A shortcut use of all in the `common.rs`.
393pub use common::*;