diff --git a/rust/src/core.rs b/rust/src/core.rs index 8c6ecc7ab0..4f6ce602a8 100644 --- a/rust/src/core.rs +++ b/rust/src/core.rs @@ -19,7 +19,7 @@ use std; use std::os::raw::{c_int, c_void}; -use suricata_sys::sys::{AppProto, AppProtoEnum}; +use suricata_sys::sys::{AppProto, AppProtoEnum, SCLogLevel}; use crate::filecontainer::*; use crate::flow::Flow; @@ -89,7 +89,7 @@ extern "C" { #[allow(non_snake_case)] pub type SCLogMessageFunc = - extern "C" fn(level: std::os::raw::c_int, + extern "C" fn(level: SCLogLevel, filename: *const std::os::raw::c_char, line: std::os::raw::c_uint, function: *const std::os::raw::c_char, diff --git a/rust/src/debug.rs b/rust/src/debug.rs index 72f7b737c3..d31508bde3 100644 --- a/rust/src/debug.rs +++ b/rust/src/debug.rs @@ -20,42 +20,17 @@ use std::{ffi::CString, path::Path}; use crate::core::SC; -use std::os::raw::c_char; -/// cbindgen:ignore -extern "C" { - pub fn SCLogGetLogLevel() -> i32; -} - -// Defined in util-debug.c -/// cbindgen:ignore -extern "C" { - pub fn SCFatalErrorOnInitStatic(arg: *const c_char); -} - -#[derive(Debug)] -#[repr(C)] -pub enum Level { - NotSet = -1, - _None = 0, - Error, - Warning, - Notice, - Info, - _Perf, - Config, - #[cfg(feature = "debug")] - Debug, -} +use suricata_sys::sys::{SCFatalErrorOnInitStatic, SCLogLevel}; -pub static mut LEVEL: i32 = Level::NotSet as i32; +pub static mut LEVEL: SCLogLevel = SCLogLevel::SC_LOG_NOTSET; /// Set the Rust context's idea of the log level. /// /// This will be called during Suricata initialization with the /// runtime log level. #[no_mangle] -pub unsafe extern "C" fn SCSetRustLogLevel(level: i32) { +pub unsafe extern "C" fn SCSetRustLogLevel(level: SCLogLevel) { LEVEL = level; } @@ -75,7 +50,7 @@ pub fn fatalerror(message: &str) { } } -pub fn sclog(level: Level, file: &str, line: u32, function: &str, message: &str) { +pub fn sclog(level: SCLogLevel, file: &str, line: u32, function: &str, message: &str) { let filename = basename(file); let noext = &filename[0..filename.len() - 3]; sc_log_message(level, filename, line, function, noext, message); @@ -85,13 +60,13 @@ pub fn sclog(level: Level, file: &str, line: u32, function: &str, message: &str) /// a more basic log format will be used (for example, when running /// Rust unit tests). pub fn sc_log_message( - level: Level, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str, + level: SCLogLevel, filename: &str, line: std::os::raw::c_uint, function: &str, module: &str, message: &str, ) -> std::os::raw::c_int { unsafe { if let Some(c) = SC { return (c.SCLogMessage)( - level as i32, + level, to_safe_cstring(filename).as_ptr(), line, to_safe_cstring(function).as_ptr(), @@ -148,7 +123,7 @@ macro_rules! function { macro_rules!do_log { ($level:expr, $($arg:tt)*) => { #[allow(unused_unsafe)] - if unsafe { $crate::debug::LEVEL } >= $level as i32 { + if unsafe { $crate::debug::LEVEL as i32 } >= $level as i32 { $crate::debug::sclog($level, file!(), line!(), $crate::function!(), &(format!($($arg)*))); } @@ -158,42 +133,42 @@ macro_rules!do_log { #[macro_export] macro_rules!SCLogError { ($($arg:tt)*) => { - $crate::do_log!($crate::debug::Level::Error, $($arg)*); + $crate::do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_ERROR, $($arg)*); }; } #[macro_export] macro_rules!SCLogWarning { ($($arg:tt)*) => { - $crate::do_log!($crate::debug::Level::Warning, $($arg)*); + $crate::do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_WARNING, $($arg)*); }; } #[macro_export] macro_rules!SCLogNotice { ($($arg:tt)*) => { - $crate::do_log!($crate::debug::Level::Notice, $($arg)*); + $crate::do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_NOTICE, $($arg)*); } } #[macro_export] macro_rules!SCLogInfo { ($($arg:tt)*) => { - $crate::do_log!($crate::debug::Level::Info, $($arg)*); + $crate::do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_INFO, $($arg)*); } } #[macro_export] macro_rules!SCLogPerf { ($($arg:tt)*) => { - $crate::do_log!($crate::debug::Level::Perf, $($arg)*); + $crate::do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_PERF, $($arg)*); } } #[macro_export] macro_rules!SCLogConfig { ($($arg:tt)*) => { - $crate::do_log!($crate::debug::Level::Config, $($arg)*); + $crate::do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_CONFIG, $($arg)*); } } @@ -202,7 +177,7 @@ macro_rules!SCLogConfig { #[macro_export] macro_rules!SCLogDebug { ($($arg:tt)*) => { - do_log!($crate::debug::Level::Debug, $($arg)*); + do_log!(suricata_sys::sys::SCLogLevel::SC_LOG_DEBUG, $($arg)*); } } diff --git a/rust/src/plugin.rs b/rust/src/plugin.rs index db47131848..925f30c95d 100644 --- a/rust/src/plugin.rs +++ b/rust/src/plugin.rs @@ -17,11 +17,13 @@ //! Plugin utility module. +use suricata_sys::sys::SCLogGetLogLevel; + pub fn init() { unsafe { let context = crate::core::SCGetContext(); crate::core::init_ffi(context); - crate::debug::LEVEL = crate::debug::SCLogGetLogLevel(); + crate::debug::LEVEL = SCLogGetLogLevel(); } } diff --git a/rust/sys/src/sys.rs b/rust/sys/src/sys.rs index 74a29339e8..75e75f8081 100644 --- a/rust/sys/src/sys.rs +++ b/rust/sys/src/sys.rs @@ -396,6 +396,27 @@ extern "C" { s: *mut Signature, alproto: AppProto, ) -> ::std::os::raw::c_int; } +#[repr(i32)] +#[doc = " \\brief The various log levels\n NOTE: when adding new level, don't forget to update SCLogMapLogLevelToSyslogLevel()\n or it may result in logging to syslog with LOG_EMERG priority."] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum SCLogLevel { + SC_LOG_NOTSET = -1, + SC_LOG_NONE = 0, + SC_LOG_ERROR = 1, + SC_LOG_WARNING = 2, + SC_LOG_NOTICE = 3, + SC_LOG_INFO = 4, + SC_LOG_PERF = 5, + SC_LOG_CONFIG = 6, + SC_LOG_DEBUG = 7, + SC_LOG_LEVEL_MAX = 8, +} +extern "C" { + pub fn SCFatalErrorOnInitStatic(arg1: *const ::std::os::raw::c_char); +} +extern "C" { + pub fn SCLogGetLogLevel() -> SCLogLevel; +} #[doc = " Structure of a configuration parameter."] #[repr(C)] #[derive(Debug, Copy, Clone)] diff --git a/src/bindgen.h b/src/bindgen.h index e2016639ed..9ba5501b2d 100644 --- a/src/bindgen.h +++ b/src/bindgen.h @@ -42,6 +42,8 @@ #include "detect-engine-helper.h" #include "detect-parse.h" +#include "util-debug.h" + #include "conf.h" #endif diff --git a/src/util-debug.h b/src/util-debug.h index 78586de6cd..9a5f077954 100644 --- a/src/util-debug.h +++ b/src/util-debug.h @@ -24,22 +24,6 @@ #ifndef SURICATA_UTIL_DEBUG_H #define SURICATA_UTIL_DEBUG_H -#include "suricata-common.h" - -#include "threads.h" -#include "util-error.h" -#include "util-debug-filters.h" - -/** - * \brief ENV vars that can be used to set the properties for the logging module - */ -#define SC_LOG_ENV_LOG_LEVEL "SC_LOG_LEVEL" -#define SC_LOG_ENV_LOG_OP_IFACE "SC_LOG_OP_IFACE" -#define SC_LOG_ENV_LOG_FILE "SC_LOG_FILE" -#define SC_LOG_ENV_LOG_FACILITY "SC_LOG_FACILITY" -#define SC_LOG_ENV_LOG_FORMAT "SC_LOG_FORMAT" -#define SC_LOG_ENV_LOG_OP_FILTER "SC_LOG_OP_FILTER" - /** * \brief The various log levels * NOTE: when adding new level, don't forget to update SCLogMapLogLevelToSyslogLevel() @@ -58,6 +42,23 @@ typedef enum { SC_LOG_LEVEL_MAX, } SCLogLevel; +#ifndef SURICATA_BINDGEN_H +#include "suricata-common.h" + +#include "threads.h" +#include "util-error.h" +#include "util-debug-filters.h" + +/** + * \brief ENV vars that can be used to set the properties for the logging module + */ +#define SC_LOG_ENV_LOG_LEVEL "SC_LOG_LEVEL" +#define SC_LOG_ENV_LOG_OP_IFACE "SC_LOG_OP_IFACE" +#define SC_LOG_ENV_LOG_FILE "SC_LOG_FILE" +#define SC_LOG_ENV_LOG_FACILITY "SC_LOG_FACILITY" +#define SC_LOG_ENV_LOG_FORMAT "SC_LOG_FORMAT" +#define SC_LOG_ENV_LOG_OP_FILTER "SC_LOG_OP_FILTER" + /** * \brief The various output interfaces supported */ @@ -530,8 +531,6 @@ SCLogInitData *SCLogAllocLogInitData(void); void SCLogAppendOPIfaceCtx(SCLogOPIfaceCtx *, SCLogInitData *); -void SCFatalErrorOnInitStatic(const char *); - void SCLogInitLogModule(SCLogInitData *); void SCLogDeInitLogModule(void); @@ -546,6 +545,9 @@ int SCLogDebugEnabled(void); void SCLogRegisterTests(void); void SCLogLoadConfig(int daemon, int verbose, uint32_t userid, uint32_t groupid); +#endif // #ifndef SURICATA_BINDGEN_H + +void SCFatalErrorOnInitStatic(const char *); SCLogLevel SCLogGetLogLevel(void);