forked from Cisco-Talos/clamav
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogging.rs
More file actions
103 lines (90 loc) · 2.8 KB
/
logging.rs
File metadata and controls
103 lines (90 loc) · 2.8 KB
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
/*
* Rust logging module
*
* Copyright (C) 2021-2023 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
*
* Authors: Mickey Sola
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
use std::{
ffi::{CStr, CString},
os::raw::c_char,
};
use log::{set_max_level, Level, LevelFilter, Metadata, Record};
use crate::sys;
pub struct ClamLogger;
impl log::Log for ClamLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= Level::Debug
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
let msg = CString::new(format!("{}\n", record.args())).unwrap();
let ptr = msg.as_ptr();
match record.level() {
Level::Debug => unsafe {
sys::cli_dbgmsg_no_inline(ptr);
},
Level::Error => unsafe {
sys::cli_errmsg(ptr);
},
Level::Info => unsafe {
sys::cli_infomsg_simple(ptr);
},
Level::Warn => unsafe {
sys::cli_warnmsg(ptr);
},
_ => {}
}
}
}
fn flush(&self) {}
}
#[no_mangle]
pub extern "C" fn clrs_log_init() -> bool {
log::set_boxed_logger(Box::new(ClamLogger))
.map(|()| set_max_level(LevelFilter::Debug))
.is_ok()
}
#[cfg(test)]
mod tests {
use super::clrs_log_init;
use log::{debug, error, info, warn};
#[test]
fn parse_move_works() {
let init_status = clrs_log_init();
assert!(init_status);
debug!("Hello");
info!("darkness");
warn!("my old");
error!("friend.");
}
}
/// API exported for C code to log to standard error using Rust.
/// This would be be an alternative to fputs, and reliably prints
/// non-ASCII UTF8 characters on Windows, where fputs does not.
///
/// # Safety
///
/// This function dereferences the c_buff raw pointer. Pointer must be valid.
#[no_mangle]
pub unsafe extern "C" fn clrs_eprint(c_buf: *const c_char) {
if c_buf.is_null() {
return;
}
let msg = unsafe { CStr::from_ptr(c_buf) }.to_string_lossy();
eprint!("{}", msg);
}