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="250"/> <img src="https://cdn.4neko.org/source_avail.webp" width="250"/> <img src="https://cdn.4neko.org/mit_mpl_eupl_2.webp" width="250"/>
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><br>
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><br>
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><br>
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><br>
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//! 
69//! --
70//! * feature = "udp_truncate_1024_bytes" - Forces truncation of the (whole) syslog message to 1024
71//! * feature = udp_truncate_1440_bytes - Forces truncation of the (whole) syslog message to 1440
72//! * feature = tcp_truncate_1024_bytes - Forces truncation of the (whole) syslog message to 1024
73//! * feature = tcp_truncate_2048_bytes - Forces truncation of the (whole) syslog message to 2048 
74//! * feature = tcp_truncate_4096_bytes - Forces truncation of the (whole) syslog message to 4096
75//! * feature = tcp_truncate_max_bytes - Forces truncation of the (whole) syslog message to 8192 (max)
76//! * feature = dgram_sysctl_failure_panic - BSD only! By default, if crate fails to obtain a sysctl net.local.dgram.maxdgram, 
77//!     it returns a default value 2048. If this feature is enabled, then it will panic.
78//! 
79//! `build_async_tokio` and `build_async_smol` cannot be used together.
80//! 
81//! `build_with_queue` behaves differently if enabled ine of the async providers.
82//! 
83//! **For mor info, please see README.md!**
84//! 
85//! # Usage Example
86//! 
87//! syslog-rs = {version = "7.0", default-features = false, features = ["build_sync", "truncate_default", "build_with_logger"]}
88//! 
89//! By default, the following features are enabled: `build_sync`, `truncate_default`, `build_with_logger`
90//! 
91//! ## Examples
92//! 
93//! See the directory `./examples`
94//! 
95//! ## build_with_logger
96//! 
97//! `logger.rs`
98//!  
99//! A logger can be controlled via environmental variables:
100//! 
101//! * `SYSLOG_LOGGER_DST_URI` - enables the logger and configures it.
102//! 
103//! * `SYSLOG_LOGGER_LOG_FACILITY` - optional, overrides the default which is LOG_DAEMON
104//! 
105//! * `SYSLOG_LOGGER_LOG_FORMATTER` - optional, overrides the default formatter dep on the OS.
106//! 
107//! * `SYSLOG_LOGGER_LOG_IDENTITY` - optional, if not set, will be determined automatically.
108//! 
109//! * `SYSLOG_LOGGER_LOG_UPTO` - optional, sets the LOGUPTO priority.
110//! 
111//! The vars can have suffix, for example `XIO`, so SYSLOG_LOGGER_DST_URI_XIO, SYSLOG_LOGGER_LOG_FACILITY_XIO, ...
112//! 
113//! In the crate or executable, a logger should be initialized. It can be done using:
114//! 
115//! ```ignore
116//!     init_event_log(None).unwrap();
117//! ```
118//! 
119//! if `suffix` was used then it should be specified:
120//! 
121//! ```ignore
122//!     init_event_log(Some("XIO")).unwrap();
123//! ```
124//! 
125//! Below are the macroses and functions which can be used to send messages
126//! 
127//! ```ignore
128//!     logger_dbg!("debug: test_logger_simple");
129//!     logger_info!("info: test1test_logger_simple23");
130//!     logger_error!("error: test_logger_simple");
131//!     logger_warn!("warn: test_logger_simple");
132//!     logger_emerg!("emerg: test_logger_simple");
133//!     logger_custom!(Priority::LOG_EMERG, "custom");
134//!     logger_log(Priority::LOG_EMERG, format!("abc"));
135//! ```
136//! 
137//! Also the logger can be attached to the existing syslog instance i.e in the larger work:
138//! 
139//! Example:
140//! ```ignore
141//!     attach_event_log_shared(&syslog).unwrap()
142//! ```
143//! 
144//! attaches the crate's syslog to the created one in a larger program. But the crate which
145//! uses logger must provide a function which would call this routine internally!
146//! 
147//! There are two attach methods: 
148//! 
149//! * `attach_event_log_shared` - for a [SyShared] instance 
150//! 
151//! * `attach_event_log_queued` - for a [SyQueue] instance
152//! 
153//! ### SYSLOG_LOGGER_DST_URI
154//! 
155//! #### Local
156//! * `local#` - for default
157//! 
158//! * `local#/var/run/syslogcustom` - for custom path 
159//! 
160//! Example: 
161//! 
162//! ```text
163//! SYSLOG_LOGGER_DST_URI="local#"
164//! SYSLOG_LOGGER_DST_URI="local#/var/run/log"
165//! ```
166//! 
167//! #### UDP/TCP
168//! 
169//! * `udp#127.0.0.1:80` - for IPv4 + port
170//! 
171//! * `udp#[fe00:::0]:80` - for IPv6 + port
172//! 
173//! * `udp#www.mydomain.tdl:80` - for hostname + port
174//! 
175//! * `tcp#127.0.0.1:80` - for IPv4 + port
176//! 
177//! Example:
178//! 
179//! ```text
180//! SYSLOG_LOGGER_DST_URI="udp#127.0.0.1:80"
181//! ```
182//! 
183//! #### TLS
184//! 
185//! * `tls#127.0.0.1:80#servername#root_cert_path` - for IPv4 + port
186//! 
187//! * `tls#[fe00:::0]:80#servername#root_cert_path` - for IPv6 + port
188//! 
189//! * `tls#www.mydomain.tdl:80#servername#root_cert_path` - for hostname + port
190//! 
191//! Example:
192//! 
193//! ```text
194//! SYSLOG_LOGGER_DST_URI="tls#127.0.0.1:80#prod0.syslogse.tld#/etc/ssl/rootcerts.crt"
195//! ```
196//! 
197//! #### FILE
198//! 
199//! * `file#/tmp/file1.txt` - for file append mode
200//! 
201//! Example:
202//! 
203//! ```text
204//! SYSLOG_LOGGER_DST_URI="file#/tmp/file1.txt"
205//! ```
206//! 
207//! ### SYSLOG_LOGGER_LOG_FACILITY
208//! 
209//! Default: LOG_DAEMON
210//! 
211//! ```text
212//! LOG_KERN, LOG_USER, LOG_MAIL, LOG_DAEMON, LOG_AUTH, LOG_SYSLOG, LOG_LPR, LOG_NEWS,
213//! LOG_UUCP, LOG_LOCAL0, LOG_LOCAL1, LOG_LOCAL2, LOG_LOCAL3, LOG_LOCAL4, LOG_LOCAL5,
214//! LOG_LOCAL6, LOG_LOCAL7
215//! ```
216//! 
217//! ### SYSLOG_LOGGER_LOG_FORMATTER
218//! 
219//! Default: autoselected
220//! 
221//! ```text
222//! RFC3146, RFC5424, FILE
223//! ```
224//! 
225//! ! Don't use `FILE` with syslog server, this is for local file only.
226//! 
227//! ### SYSLOG_LOGGER_LOG_UPTO
228//! 
229//! ```text
230//! LOG_ALERT, LOG_CRIT, LOG_DEBUG, LOG_EMERG, LOG_ERR, LOG_INFO, LOG_NOTICE, LOG_WARNING
231//! ```
232//! 
233
234// make sure that the smol and tokio are not used together.
235#[cfg(all(feature = "build_async_tokio", feature = "build_async_smol"))]
236compile_error!("use_async_tokio and use_async_smol can not be used at the same time");
237
238// make sure that the build_async_interface and either async_embedded are not used together.
239#[cfg(all(feature = "build_async_interface", feature = "async_embedded"))]
240compile_error!("build_async_interface and either tokio/smol can not be used at the same time");
241
242//-- REEXPORT --
243#[macro_use] 
244extern crate bitflags;
245pub extern crate chrono;
246extern crate socket2;
247
248
249#[cfg(target_family = "unix")]
250pub extern crate nix;
251
252/// winDAws crp...
253#[cfg(target_family = "windows")]
254pub extern crate windows;
255
256
257// -- TLS --
258
259#[cfg(all(feature = "build_sync", feature = "build_ext_tls"))]
260extern crate rustls;
261
262#[cfg(all(feature = "build_async_tokio", feature = "build_ext_tls"))]
263extern crate tokio_rustls;
264
265#[cfg(all(feature = "build_async_smol", feature = "build_ext_tls"))]
266extern crate futures_rustls;
267
268// -- ASYNC --
269
270#[cfg(feature = "build_async_tokio")]
271extern crate tokio;
272
273#[cfg(feature = "build_async_smol")]
274extern crate smol;
275
276// -- SYNC --
277#[cfg(feature = "build_with_queue")]
278extern crate crossbeam_channel;
279
280#[cfg(feature = "build_sync")]
281extern crate crossbeam_utils;
282
283/// A sync syslog realization including shared and queued syslog client.
284#[cfg(feature = "build_sync")]
285pub mod sync;
286
287/// An async interface and/or realisations. The main async syslog struct is located there.
288#[cfg(feature = "async_enabled")]
289pub mod a_sync;
290
291/// A message formatters i.e RFC5424 and other. Also a message formatter interface.
292pub mod formatters;
293
294pub mod syslog_provider;
295
296/// A syslog destnation. i.e local, remote, remote TLS, local file. Needed to initialize the 
297/// (both sync and async) instances.
298pub use syslog_provider::*;
299
300/// A common things for the sockets managment.
301pub mod socket;
302
303/// A logger which can be setup from env.
304#[cfg(all(unix, feature = "build_with_logger"))]
305pub mod logger;
306
307/// A common code which holds all flags and macroses.
308#[macro_use] 
309pub mod common;
310
311/// Unified error interface.
312#[macro_use] 
313pub mod error;
314
315
316//-- NON EXPORT
317/// A portable code which is different for different OSes.
318mod portable;
319
320/// Public use of the type of the tap.
321pub use socket::TapType;
322
323/// A streamable sync instance (both).
324#[cfg(feature = "build_sync")]
325pub use sync::{syslog_sync::{Syslog, SyShared, DynSyslog}, SyStreamPriWarning, SyStreamPriNotice, SyStreamPriInfo,
326    SyStreamPriEmerg, SyStreamPriCrit, SyStreamPriAlert, SyStreamPriDebug, SyStreamPriErr,
327    DefaultLocalSyslogDestination};
328
329#[cfg(feature = "build_with_logger")]
330pub use logger::{init_event_log, logger_log, attach_event_log_shared};
331
332#[cfg(all(feature = "build_with_logger", feature = "build_with_queue"))]
333pub use logger::attach_event_log_queued;
334
335/// A `sy_async` is a shortcut to the file `syslog_async.rs` in dir `a_sync`.
336#[cfg(feature = "async_enabled")]
337pub use a_sync::syslog_async as sy_async;
338
339/// A main async syslog instance.
340#[cfg(feature = "async_enabled")]
341pub use sy_async::AsyncSyslog;
342
343/// A shortcut for the internals of the `async_syslog` and IO for user implementation.
344#[cfg(feature = "build_async_interface")]
345pub use a_sync::syslog_async_internal::{AsyncSyslogInternal, AsyncSyslogInternalIO};
346
347/// A shortcut to the trait `AsyncMutex` which unifies the mutex realiztion of different 
348/// exec environments.
349#[cfg(feature = "build_async_interface")]
350pub use a_sync::syslog_async_internal::AsyncMutex;
351
352/// Use this trait to access queue interface for async code.
353#[cfg(all(feature = "async_enabled", feature = "build_with_queue"))]
354pub use a_sync::syslog_trait::AsyncSyslogQueueApi;
355
356
357/// A shortcut to the file `syslog_sync_queue.rs` in dir `sync`.
358#[cfg(feature = "build_with_queue")]
359pub use sync::syslog_sync_queue as sy_sync_queue;
360
361/// A (previosuly SyslogQueue) queued sync type of the syslog client. Also async queue.
362#[cfg(feature = "build_with_queue")]
363pub use sync::syslog_sync::SyQueued;
364
365#[cfg(feature = "build_with_thread_local")]
366pub use sync::syslog_sync::SyThreadLocal;
367
368/// A shortcut use of all in the `common.rs`.
369pub use common::*;