Skip to main content

relibc/header/sys_syslog/
mod.rs

1//! `syslog.h` implementation.
2//!
3//! See <https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/syslog.h.html>.
4
5// Exported as both syslog.h and sys/syslog.h.
6
7#[cfg(target_os = "redox")]
8#[path = "redox.rs"]
9pub mod sys;
10
11#[cfg(target_os = "linux")]
12#[path = "linux.rs"]
13pub mod sys;
14
15pub mod logger;
16
17use core::ffi::VaList;
18
19use crate::{
20    c_str::CStr,
21    platform::types::{c_char, c_int},
22};
23use logger::{LOGGER, Priority};
24
25/// Record the caller's PID in log messages.
26pub const LOG_PID: c_int = 0x01;
27/// Write to /dev/console if [`syslog`] fails.
28pub const LOG_CONS: c_int = 0x02;
29/// Open the log on the first call to [`syslog`] rather than opening it early.
30/// This is the default behavior and setting or unsetting this option does nothing.
31pub const LOG_ODELAY: c_int = 0x04;
32/// Open the log file immediately.
33pub const LOG_NDELAY: c_int = 0x08;
34pub const LOG_NOWAIT: c_int = 0x10;
35/// Print log message to stderr as well as the log.
36pub const LOG_PERROR: c_int = 0x20;
37
38pub const LOG_KERN: c_int = 0 << 3;
39pub const LOG_USER: c_int = 1 << 3;
40pub const LOG_MAIL: c_int = 2 << 3;
41pub const LOG_DAEMON: c_int = 3 << 3;
42pub const LOG_AUTH: c_int = 4 << 3;
43pub const LOG_SYSLOG: c_int = 5 << 3;
44pub const LOG_LPR: c_int = 6 << 3;
45pub const LOG_NEWS: c_int = 7 << 3;
46pub const LOG_UUCP: c_int = 8 << 3;
47pub const LOG_CRON: c_int = 9 << 3;
48pub const LOG_AUTHPRIV: c_int = 10 << 3;
49pub const LOG_FTP: c_int = 11 << 3;
50
51pub const LOG_LOCAL0: c_int = 16 << 3;
52pub const LOG_LOCAL1: c_int = 17 << 3;
53pub const LOG_LOCAL2: c_int = 18 << 3;
54pub const LOG_LOCAL3: c_int = 19 << 3;
55pub const LOG_LOCAL4: c_int = 20 << 3;
56pub const LOG_LOCAL5: c_int = 21 << 3;
57pub const LOG_LOCAL6: c_int = 22 << 3;
58pub const LOG_LOCAL7: c_int = 23 << 3;
59pub const LOG_NFACILITIES: c_int = 24;
60
61// Priorities
62pub const LOG_EMERG: c_int = 0;
63pub const LOG_ALERT: c_int = 1;
64pub const LOG_CRIT: c_int = 2;
65pub const LOG_ERR: c_int = 3;
66pub const LOG_WARNING: c_int = 4;
67pub const LOG_NOTICE: c_int = 5;
68pub const LOG_INFO: c_int = 6;
69pub const LOG_DEBUG: c_int = 7;
70
71/// Create a mask that includes all levels up to a certain priority.
72#[unsafe(no_mangle)]
73pub const extern "C" fn LOG_UPTO(p: c_int) -> c_int {
74    (1 << (p + 1)) - 1
75}
76
77/// Create a mask that enables a single priority.
78#[unsafe(no_mangle)]
79pub const extern "C" fn LOG_MASK(p: c_int) -> c_int {
80    1 << p
81}
82
83/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/closelog.html>.
84#[unsafe(no_mangle)]
85pub extern "C" fn setlogmask(mask: c_int) -> c_int {
86    let mut params = LOGGER.lock();
87    let old = params.mask.bits();
88    if mask != 0
89        && let Some(mask) = params.mask.with_mask(mask)
90    {
91        params.mask = mask;
92    }
93    old
94}
95
96/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/closelog.html>.
97#[unsafe(no_mangle)]
98pub unsafe extern "C" fn openlog(ident: *const c_char, opt: c_int, facility: c_int) {
99    let ident = unsafe { CStr::from_nullable_ptr(ident) };
100    let conf = logger::Config::from_bits_truncate(opt);
101
102    let mut params = LOGGER.lock();
103    params.set_identity_cstr(ident);
104    params.opt = conf;
105    params.mask = params.mask.with_facility(facility).unwrap_or(
106        params
107            .mask
108            .with_facility(Priority::User.bits())
109            .expect("`User` is a valid syslog facility"),
110    );
111
112    // Ensure log is ready to write now instead of checking on the first message.
113    if conf.contains(logger::Config::NoDelay)
114        && let Ok(()) = params.open_logger()
115    {}; // TODO handle error
116}
117
118/// See <https://www.man7.org/linux/man-pages/man3/vsyslog.3.html>.
119///
120/// Non-POSIX, 4.3BSD-Reno.
121#[unsafe(no_mangle)]
122pub unsafe extern "C" fn vsyslog(priority: c_int, message: *const c_char, ap: VaList) {
123    let Some(message) = (unsafe { CStr::from_nullable_ptr(message) }) else {
124        return;
125    };
126    let Some(priority) = Priority::from_bits(priority) else {
127        return;
128    };
129
130    let mut logger = LOGGER.lock();
131    if logger.mask.should_log(priority) {
132        logger.write_log(priority, message, ap);
133    }
134}
135
136/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/closelog.html>.
137#[unsafe(no_mangle)]
138pub unsafe extern "C" fn syslog(priority: c_int, message: *const c_char, __valist: ...) {
139    unsafe { vsyslog(priority, message, __valist) };
140}
141
142/// See <https://pubs.opengroup.org/onlinepubs/9799919799/functions/closelog.html>.
143#[unsafe(no_mangle)]
144pub extern "C" fn closelog() {
145    LOGGER.lock().close();
146}