syslog-rs 6.5.0

A native Rust implementation of the glibc/libc/windows syslog client and windows native log for logging.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*-
 * syslog-rs - a syslog client translated from libc to rust
 * 
 * Copyright 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *
 *   2. The MIT License (MIT)
 *                     
 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */


use std::fmt;
use std::marker::PhantomData;

use crate::a_sync::syslog_trait::AsyncSyslogApi;
use crate::formatters::SyslogFormatter;
use crate::map_error_os;
use crate::portable;
use crate::common::*;
use crate::error::SyRes;
use crate::socket::TapType;
use crate::AsyncSyslogDestination;

use super::async_socket::*;

/// A trait which generalize some operations which are different from the
/// various async providers i.e `smol` or `tokio` or external.
#[allow(async_fn_in_trait)]
pub trait AsyncSyslogInternalIO: fmt::Debug + Send + 'static
{
    /// Sends a message to syscons device. Usually this is a `/dev/console`
    /// defined by crate::common::PATH_CONSOLE.
    /// 
    /// # Arguemnts
    /// 
    /// * `logstat` - a instance setup [LogStat].
    /// 
    /// * `msg_payload` - a payload of the syslog message (without headers).
    async fn send_to_syscons(logstat: LogStat, msg_payload: &str);

    /// Sends a message to stderr device.
    /// 
    /// # Arguemnts
    /// 
    /// * `logstat` - a instance setup [LogStat].
    /// 
    /// * `msg` - a payload of the syslog message (without headers).
    async fn send_to_stderr(logstat: LogStat, msg: &str);

    /// Sleep the current task for `us` microseconds.
    /// 
    /// # Arguments
    /// 
    /// * `us` - microseconds.
    async fn sleep_micro(us: u64);
}

/// A trait which generalize the mutex from the std lib's of multiple async executors.
/// The trait should be implemented on the mutex direclty.
#[allow(async_fn_in_trait)]
pub trait AsyncMutex<F: SyslogFormatter, D: AsyncSyslogDestination, DS: AsyncSyslogApi<F, D>>
{
    /// A mutex guard type.
    type MutxGuard<'mux>: AsyncMutexGuard<'mux, F, D, DS> where Self: 'mux;

    /// Creates new mutex instance for type which implements the [AsyncSyslogApi].
    fn a_new(v: DS) -> Self;

    /// Locks the mutex emmiting the `mutex guard`.
    async fn a_lock<'mux>(&'mux self) -> Self::MutxGuard<'mux>;
}

/// A trait which generalize the mutex guarding emited by the mutex from various async executors.
pub trait AsyncMutexGuard<'mux, F: SyslogFormatter, D: AsyncSyslogDestination, DS: AsyncSyslogApi<F, D>>
{
    /// Returns the reference to the inner type of the mutex guard.
    fn guard(&self) -> &DS;

    /// Returns the mutable reference to the inner type of the mutex guard.
    fn guard_mut(&mut self) -> &mut DS;
}

/// Internal structure of the syslog async client.
#[derive(Debug)]
pub struct AsyncSyslogInternal<F: SyslogFormatter + Send, D: AsyncSyslogDestination, IO: AsyncSyslogInternalIO>
{
    /// An identification i.e program name, thread name
    logtag: String, 

    /// A pid of the program.
    logpid: String,

    /// Defines how syslog operates
    logstat: LogStat,

    /// Holds the facility 
    facility: LogFacility,

    /// A logmask
    logmask: i32,

    /// A stream
    stream: D::SocketTap,

    /// Phantom for [SyslogFormatter]
    _p: PhantomData<F>,

    /// Phantom for the [AsyncSyslogInternalIO] which provides writing to console and other IO.
    _p2: PhantomData<IO>,
}


impl<F: SyslogFormatter + Send, D: AsyncSyslogDestination, IO: AsyncSyslogInternalIO> AsyncSyslogInternal<F, D, IO>
{
    /// Creates new instance of [SyslogInternal] which contains all 
    /// client syslog logic.
    ///
    /// # Arguments
    ///
    /// * `ident` - An optional argument which takes ref to str. If none, the
    ///     ident will be set later. Yje ident will be trucated to 48 UTF8
    ///     chars.
    /// 
    /// * `logstat` - A [LogStat] flags separated by '|'
    /// 
    /// * `facility` - A [LogFacility] flag
    /// 
    /// * `req_tap` - A type of the syslog instance reuired. See [SyslogDestination].
    /// 
    /// # Returns
    ///
    /// A [SyRes] is returned with following:
    /// 
    /// * [Result::Ok] - with the instance.
    /// 
    /// * [Result::Err] - with error description.
    pub(crate) 
    fn new(
        ident: Option<&str>, 
        logstat: LogStat, 
        facility: LogFacility, 
        req_tap: D
    ) -> SyRes<Self>
    {
        // check if log_facility is invalid
        let log_facility =
            if facility.is_empty() == false && 
                (facility & !LogMask::LOG_FACMASK).is_empty() == true
            {
                facility
            }
            else
            {
                // default facility code
                LogFacility::LOG_USER
            };

        let logtag = 
            match ident
            {
                Some(r) => 
                    truncate_n(r, RFC_MAX_APP_NAME).to_string(),
                None => 
                    truncate_n(
                        portable::p_getprogname()
                            .unwrap_or("".to_string())
                            .as_str(),
                        RFC_MAX_APP_NAME
                    )
                    .to_string()
            };

        let sock = D::SocketTap::new(req_tap)?;

        return Ok(
            Self
            {
                logtag: logtag,
                logpid: portable::get_pid().to_string(),
                logstat: logstat, 
                facility: log_facility,
                logmask: 0xff,
                stream: sock,
                _p: PhantomData,
                _p2: PhantomData,
            }
        );
    }

    #[inline]
    pub(crate) 
    fn is_logmasked(&self, pri: Priority) -> bool
    {
        return ((1 << (pri & LogMask::LOG_PRIMASK)) & self.logmask) == 0;
    }

        /// Returns the type of the socket.
    #[inline]
    pub(crate)
    fn get_taptype(&self) -> TapType
    {
        return self.stream.get_type();
    }

    /* 
    /// Returns the maximum msg size in bytes. (Full msg with headers.)
    #[inline]
    pub(crate) 
    fn get_max_msg_size(&self) -> usize
    {
        return self.stream.get_max_msg_size();
    }
    */

    #[inline]
    pub(crate) 
    fn set_logtag<L: AsRef<str>>(&mut self, logtag: L, update_pid: bool)
    {
        self.logtag = 
            truncate_n(logtag.as_ref(), RFC_MAX_APP_NAME).to_string();

        if update_pid == true
        {
            self.logpid = portable::get_pid().to_string();
        }

        return;
    }

    /// Disconnects the unix stream from syslog.
    #[inline]
    pub(crate) async 
    fn disconnectlog(&mut self)  -> SyRes<()>
    {
        return 
            self
                .stream
                .disconnectlog()
                .await
                .map_err(|e| map_error_os!(e, "can not disconnect log properly"));
    }
}

impl<F: SyslogFormatter + Send, D: AsyncSyslogDestination, IO: AsyncSyslogInternalIO> AsyncSyslogApi<F, D>
for AsyncSyslogInternal<F, D, IO>
{
    async 
    fn update_tap_data(&mut self, tap_data: D) -> SyRes<()>
    {
        let is_con = self.stream.is_connected();

        if is_con == true
        {
            self
                .stream
                .disconnectlog()
                .await
                .map_err(|e|
                    map_error_os!(e, "update_tap_data() can not disconnect log properly")

                )?;
        }

        self.stream.update_tap_data(tap_data);

        if is_con == true
        {
            // replace with new instance
            self.stream.connectlog().await?;
        }

        return Ok(());
    }
 
    #[inline]
    fn change_identity(&mut self, ident: &str)
    {
        self.set_logtag(ident, true);
    }

    async 
    fn reconnect(&mut self) -> SyRes<()>
    {
        self.disconnectlog().await?;

        self.connectlog().await?;

        return Ok(());
    }

    async 
    fn closelog(&mut self) -> SyRes<()>
    {
        return self.disconnectlog().await;
    }
    
    fn set_logmask(&mut self, logmask: i32) -> i32
    {
        let oldmask = self.logmask;

        if logmask != 0
        {
            self.logmask = logmask;
        }

        return oldmask;
    }


    /// Connects unix stream to the syslog and sets up the properties of
    /// the unix stream.
    #[inline]
    async 
    fn connectlog(&mut self) -> SyRes<()>
    {
        return self.stream.connectlog().await;
    }
    
    /// An internal function which is called by the syslog or vsyslog.
    async 
    fn vsyslog1(&mut self, pri: Priority, fmt: F)
    {
        /*
        // check for invalid bits
        if let Err(e) = pri.check_invalid_bits()
        {
            IO::send_to_stderr(self.logstat, &e.to_string()).await;
        }
        */

        /*match check_invalid_bits(&mut pri)
        {
            Ok(_) => {},
            Err(_e) => self.vsyslog1(get_internal_log(), fmt).await
        }*/

        // check priority against setlogmask
        if self.is_logmasked(pri) == true
        {
            return;
        }

        // set default facility if not specified in pri
        let pri_fac = SyslogMsgPriFac::set_facility(pri, self.facility);

        // set PID if needed
        let msg_pid = 
            if self.logstat.intersects(LogStat::LOG_PID) == true
            {
                Some(self.logpid.as_str())
            }
            else
            {
                None
            };

        let mut msg_formatted = 
            fmt.vsyslog1_format(D::SocketTap::get_max_msg_size(), pri_fac, &self.logtag, msg_pid);

        // output to stderr if required
        IO::send_to_stderr(self.logstat, msg_formatted.get_stderr_output()).await;

        if self.stream.is_connected() == false
        {
            // open connection
            match self.connectlog().await
            {
                Ok(_) => {},
                Err(e) =>
                {
                    IO::send_to_stderr(self.logstat, &e.into_inner() ).await;
                    return;
                }
            }
        }
        
        let fullmsg = msg_formatted.get_full_msg();


        // There are two possible scenarios when send may fail:
        // 1. syslog temporary unavailable
        // 2. syslog out of buffer space
        // If we are connected to priv socket then in case of 1 we reopen connection
        //      and retry once.
        // If we are connected to unpriv then in case of 2 repeatedly retrying to send
        //      until syslog socket buffer space will be cleared

        loop
        {
            match self.stream.send(fullmsg.as_bytes()).await
            {
                Ok(_) => 
                    return,
                Err(err) =>
                {   
                    if self.get_taptype().is_network() == false
                    {
                        #[cfg(target_family = "unix")]
                        {
                            if let Some(nix::libc::ENOBUFS) = err.raw_os_error()
                            {
                                // scenario 2
                                if self.get_taptype().is_priv() == true
                                {
                                    break;
                                }

                                IO::sleep_micro(1).await;
                                //sleep(Duration::from_micros(1)).await;
                            }
                            else
                            {
                                // scenario 1
                                let _ = self.disconnectlog().await;
                                match self.connectlog().await
                                {
                                    Ok(_) => {},
                                    Err(_e) => break,
                                }

                               
                            } 
                        }

                         #[cfg(target_family = "windows")]
                        {
                            let Ok(werr) = err.downcast::<windows::core::Error>()
                                else
                                {
                                    IO::send_to_stderr(self.logstat, "error downcast failed").await;
                                    break;
                                };

                            IO::send_to_stderr(self.logstat, &werr.message()).await;   
              
                            break;
                        }

                        // if resend will fail then probably the scn 2 will take place
                    }
                    else
                    {
                        let _ = self.disconnectlog().await;
                        match self.connectlog().await
                        {
                            Ok(_) => {},
                            Err(_e) => break,
                        }
                    }  
                }
            }
        } // loop


        // If program reached this point then transmission over socket failed.
        // Try to output message to console

        IO::send_to_syscons(self.logstat, msg_formatted.get_stderr_output()).await;
    }
}