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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*-
 * 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
 */

#[cfg(target_os = "linux")]
pub(crate) use self::portable_linux::*;

#[cfg(any(
    target_os = "freebsd",
    target_os = "dragonfly",
    target_os = "openbsd",
    target_os = "netbsd",
    target_os = "macos"
))]
pub(crate) use self::portable_bsd::*;

#[cfg(target_family = "windows")]
pub(crate) use self::portable_windows::*;

/// A portable code for Windows.
#[cfg(target_family = "windows")]
pub mod portable_windows
{
    use std::
    {
        env, ffi::{CStr, c_void}, io::{self, ErrorKind}, mem::transmute, time::Duration
    };

    use windows::
    {
        Win32::
        {
            Foundation::HANDLE, 
            Networking::WinSock::{self, SOCKET_ERROR}, 
            System::
            {
                EventLog::{EVENTLOG_INFORMATION_TYPE, RegisterEventSourceA, ReportEventA}, 
                SystemInformation
            }
        }, 
        core::{Free, PCSTR}
    };

    use crate::{LogFacility, LogMask, NILVALUE, Priority, SyslogMsgPriFac, WindowsEvent, error::SyRes};

    pub const DEFAULT_MAXGRAM_SIZE: u64 = 2048;

    /// A dummy network socket realization for the Windows Event Log.
    #[derive(Debug)]
    pub struct EventLogLocal
    {
        /// windows::Win32::Foundation::HANDLE 
        /// (but it is a *c_void which cannot be sent between threads)
        evs: usize,
    }

    impl Drop for EventLogLocal
    {
        fn drop(&mut self) 
        {
            unsafe { HANDLE(self.evs as *mut c_void).free() };
        }
    }

    impl EventLogLocal
    {
        pub 
        fn new(winnie_ev: &WindowsEvent) -> io::Result<Self>
        {
            /*OpenEventLogA*/

            let evs = 
                unsafe 
                {  
                    RegisterEventSourceA(
                        winnie_ev
                            .get_server_unc()
                            .map_or(
                                PCSTR::null(), 
                                |f| PCSTR::from_raw( transmute(f.as_ptr()) )

                            ), 
                        PCSTR::from_raw(
                                transmute(winnie_ev.get_service_name().as_ptr())
                            ) 
                    )
                }
                .map_err(|e|
                    io::Error::new(ErrorKind::ConnectionRefused, e)

                )?;
            
            return Ok(Self{ evs: evs.0 as usize });
        }

        /// Sends message to log.
        /// 
        /// If `msg` starts with sign `|`, it contains a priority and facility data,
        /// otherwise default values will be set. The `|` will be ommited from message.
        /// It must be `NULL-terminated`!
        /// 
        /// The message may begin from '|'PRI'|', see `windows_10.rs` as example.
        /// 
        /// # Returns
        /// 
        /// * [ErrorKind::InvalidData] - if argument `msg` was not able to wrap into [CStr].
        ///     Probably the `msg` is not NULL-terminated! The error description can be 
        ///     obtained by downcasting the error to [std::ffi::FromBytesWithNulError].
        /// 
        /// * [ErrorKind::ConnectionRefused] - [ReportEventA] failed with error which can be
        ///     obtained by downcasting the error into [windows::core::Error].
        pub 
        fn send(&self, msg: &[u8]) -> io::Result<usize>
        {
            if msg.len() == 0
            {
                return Ok(0);
            }

            let mut event_log_type = EVENTLOG_INFORMATION_TYPE;
            let mut facility = LogFacility::LOG_USER.into_win_facility();
            
            if msg[0] == '|' as u8
            {
                for (idx, c) in msg.iter().enumerate().skip(1)
                {
                    if *c == '|' as u8
                    {
                        let Ok(pri_fac) = 
                            SyslogMsgPriFac::try_from(&msg[1..idx])
                                else { break };

                        event_log_type = pri_fac.get_priority().into();
                        facility = pri_fac.get_log_facility().into_win_facility();
                        break;
                    }

                    if idx == 8 
                    {
                        break;
                    }
                }
            }

            let cmsg = 
                CStr::from_bytes_with_nul(msg)
                    .map_err(|e|
                        io::Error::new(ErrorKind::InvalidData, e)

                    )?;

            let msgs =
                 &[PCSTR::from_raw( unsafe { transmute(/*copied_msg.as_c_str().as_ptr()*/ cmsg.as_ptr()) })];

            unsafe
            {
                ReportEventA(
                    HANDLE(self.evs as *mut c_void), 
                    event_log_type, //EVENTLOG_ERROR_TYPE,
                    1, 
                    0x20010000 | facility, 
                    None, 
                    0, 
                    Some(msgs), 
                    None
                )
                
            }
            .map_err(|e|
                io::Error::new(ErrorKind::ConnectionRefused, e)

            )?;

            return Ok(msg.len());
        }
    }

    /// Reutns the current process name, if available
    pub(crate) 
    fn p_getprogname() -> Option<String>
    {
        return 
            env::current_exe()
                .ok()
                .map_or(None, 
                    |path| 
                    path

                        .file_name()
                        .map(|name| name.to_string_lossy().to_string())
                );
    }

    pub(crate)
    fn get_local_dgram_maxdgram() -> u64
    {
        return DEFAULT_MAXGRAM_SIZE;
    }

    pub(crate)
    fn get_uptime() -> SyRes<Duration>
    {
        let ret: u64 = unsafe {SystemInformation::GetTickCount64() };

        return Ok(Duration::from_millis(ret));
    }

    pub(crate)
    fn portable_gethostname() -> SyRes<String>
    {
        let mut hostname = vec![0_u8; 512];
        
        let res = 
            unsafe
            {
                WinSock::gethostname(&mut hostname)
            };

        if res == SOCKET_ERROR 
        {
            let err = unsafe { WinSock::WSAGetLastError() };

            throw_error!("can bot obtain kern.boottime, err: '{}'", err.0);
        }

        return Ok(String::from_utf8(hostname).map_or(NILVALUE.into(), |f| f));
    }
}

#[cfg(target_os = "linux")]
pub mod portable_linux
{
    use std::ffi::CStr;
    use std::path::Path;
    use std::time::Duration;

    use nix::libc;

    use crate::error::SyRes;
    use crate::{map_error};

    pub const DEFAULT_MAXGRAM_SIZE: u64 = 2048;

    #[link(name = "c")]
    unsafe extern "C" {
        pub static mut program_invocation_name : *mut libc::c_char ;
    }

    /// Returns the current program's name.
    pub(crate) 
    fn p_getprogname() -> Option<String>
    {
        let pn = unsafe{ program_invocation_name };

        let temp = unsafe {CStr::from_ptr(pn)};

        return 
            temp
                .to_str()
                .ok()
                .map_or(None, |r| Path::new(r).file_name().map(|r| r.to_string_lossy().into()));
/*
        match temp.to_str()
        {
            Ok(r) => 
            {
                let path = Path::new(r);
                match path.file_name()
                {
                    Some(r) => return Some(r.to_string_lossy().into()),
                    None => return None,
                }
            },
            Err(_) => 
                return None,
        }
   */
    }

    pub(crate) 
    fn get_local_dgram_maxdgram() -> u64
    {
        return DEFAULT_MAXGRAM_SIZE;
    }

    pub(crate)
    fn get_uptime() -> SyRes<Duration>
    {
        let sysinfo = 
            nix::sys::sysinfo::sysinfo()
                .map_err(|e|
                    map_error!("get_uptime() sysinfo error: {}", e)

                )?;

        return Ok(sysinfo.uptime());
    }

    pub(crate)
    fn portable_gethostname() -> SyRes<String>
    {
        return 
            nix::unistd::gethostname()
                .map_or_else(
                    |e|
                    Err(map_error!("gethostname() error: {}", e)),
                    |hn| 
                    hn.into_string()

                        .map_or(
                            Err(map_error!("gethostname() into_string() error")),
                            |hostname| Ok(hostname)

                        )
                );
    }
}


#[cfg(any(
    target_os = "freebsd",
    target_os = "dragonfly",
    target_os = "openbsd",
    target_os = "netbsd",
    target_os = "macos"
))]
pub mod portable_bsd
{
    use std::{ffi::{CStr, CString}, time::Duration};

    use chrono::Local;
    use nix::{errno::Errno, libc::{self, timeval}};

    use crate::error::SyRes;

    pub const DEFAULT_MAXGRAM_SIZE: u64 = 2048;

    #[link(name = "c")]
    unsafe extern "C" {
        fn getprogname() -> *const libc::c_char;
    }

    /// Reutns the current process name, if available
    pub(crate) 
    fn p_getprogname() -> Option<String>
    {
        let pn = unsafe { getprogname() };

        let temp = unsafe {CStr::from_ptr(pn)};

        match temp.to_str()
        {
            Ok(r) => 
                return Some(r.to_string()),
            Err(_) => 
                return None,
        }
    
    }

    pub(crate) 
    fn get_local_dgram_maxdgram() -> u64
    {
        let name = CString::new("net.local.dgram.maxdgram").unwrap();
        let mut maxdgram: u64 = 0;
        let mut maxdgram_size: libc::size_t = std::mem::size_of_val(&maxdgram) as libc::size_t;

        let res = 
            unsafe 
            {
                libc::sysctlbyname(
                    name.as_ptr(), 
                    &mut maxdgram as *mut _ as *mut libc::c_void, 
                    &mut maxdgram_size as *mut _ as *mut libc::size_t,
                    std::ptr::null(), 
                    0)
            };

        if res == -1
        {
            if cfg!(feature = "dgram_sysctl_failure_panic")
            {
                let err = Errno::last_raw();
                panic!("can not obtain MAXDGRAM from sysctl '{}', err: '{}'", name.to_string_lossy(), err);
            }
            else 
            {
                return DEFAULT_MAXGRAM_SIZE;
            }
        }
        else if res == 0
        {
            return maxdgram;
        }
        else
        {
            panic!("can not obtain MAXDGRAM from sysctl '{}', unknwon res: '{}'", name.to_string_lossy(), res);
        }
    }

    pub(crate)
    fn get_uptime() -> SyRes<Duration>
    {
        let name = CString::new("kern.boottime").unwrap();

        let mut boottime: timeval = unsafe { std::mem::zeroed() };
        let mut size: libc::size_t = std::mem::size_of_val(&boottime) as libc::size_t;

        let res = 
            unsafe 
            {
                libc::sysctlbyname(
                    name.as_ptr(), 
                    &mut boottime as *mut _ as *mut libc::c_void, 
                    &mut size as *mut _ as *mut libc::size_t,
                    std::ptr::null(), 
                    0)
            };

        
        if res == -1
        {
            throw_error!("can bot obtain kern.boottime, err: '{}'", Errno::last_raw());
        }
        else if res == 0
        {
            let c = 
                chrono::Duration::new(boottime.tv_sec, boottime.tv_usec as u32)
                    .ok_or_else(||
                        map_error!("timeval to chrono::Duration converion error")

                    )?;
            let now = Local::now();

            let r = 
                now
                    .checked_sub_signed(c)
                    .ok_or_else(||
                        map_error!("error checked_sub_signed()")

                    )?;
            return Ok(Duration::from_millis(r.timestamp_millis() as u64));
        }
        else
        {
            throw_error!("can not obtain  kern.boottime from sysctl '{}', unknwon res: '{}'", name.to_string_lossy(), res);
        }
    }

    pub(crate)
    fn portable_gethostname() -> SyRes<String>
    {
        return 
            nix::unistd::gethostname()
                .map_or_else(
                    |e|
                    Err(map_error!("gethostname() error: {}", e)),
                    |hn| 
                    hn.into_string()

                        .map_or(
                            Err(map_error!("gethostname() into_string() error")),
                            |hostname| Ok(hostname)

                        )
                );
    }

    #[cfg(test)]
    mod tests
    {
        use crate::portable::get_uptime;

        #[test]
        fn test_get_uptime()
        {
            let res = get_uptime();
            assert_eq!(res.is_ok(), true);

            println!("{:?}", res.unwrap());
        }
    }
}


/// Returns pid of current process. Not thread id!
#[inline]
pub 
fn get_pid() -> u32
{
    return std::process::id();
}

#[cfg(test)]
mod test_portables
{
    use super::*;
    
    #[test]
    fn test_get_procname()
    {
        let procname = p_getprogname();

        println!("Processname is: {:?}", procname);

        assert_eq!(procname.is_some(), true);
        assert_eq!(procname.as_ref().unwrap().starts_with("syslog_rs"), true);
    }

    #[test]
    fn test_maxgram()
    {
        let max = get_local_dgram_maxdgram();

        println!("maxdgram: '{}'", max);

        assert_eq!(max > 0, true);
    }
}