]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: stub out logging from rust
authorJason Ish <ish@unx.ca>
Mon, 3 Apr 2017 21:30:17 +0000 (15:30 -0600)
committerJason Ish <ish@unx.ca>
Mon, 5 Jun 2017 20:57:20 +0000 (14:57 -0600)
rust/src/lib.rs
rust/src/log.rs [new file with mode: 0644]
src/Makefile.am
src/rust.h [new file with mode: 0644]
src/util-debug.c

index a93251b65da98b83862bc07ad0c89138639b58a5..4b25340d4299c15437b6b3cc1865ded0d748452a 100644 (file)
@@ -1,3 +1,2 @@
-#[test]
-fn it_works() {
-}
+#[macro_use]
+pub mod log;
diff --git a/rust/src/log.rs b/rust/src/log.rs
new file mode 100644 (file)
index 0000000..bf4ad73
--- /dev/null
@@ -0,0 +1,139 @@
+/* Copyright (C) 2017 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program 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
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+extern crate libc;
+
+use std::os::raw::c_char;
+use std::ffi::{CString};
+use std::path::Path;
+
+pub enum Level {
+    NotSet = -1,
+    None = 0,
+    Emergency,
+    Alert,
+    Critical,
+    Error,
+    Warning,
+    Notice,
+    Info,
+    Perf,
+    Config,
+    Debug,
+}
+
+pub static mut LEVEL: i32 = Level::NotSet as i32;
+
+extern {
+    fn SCLogMessage(level: libc::c_int,
+                    filename: *const c_char,
+                    line: libc::c_uint,
+                    function: *const c_char,
+                    code: libc::c_int,
+                    message: *const c_char) -> libc::c_int;
+}
+
+pub fn get_log_level() -> i32 {
+    unsafe {
+        LEVEL
+    }
+}
+
+fn basename(filename: &str) -> &str {
+    let path = Path::new(filename);
+    for os_str in path.file_name() {
+        for basename in os_str.to_str() {
+            return basename;
+        }
+    }
+    return filename;
+}
+
+pub fn sclog(level: Level, file: &str, line: u32, function: &str,
+         code: i32, message: &str)
+{
+    let filename = basename(file);
+
+    unsafe {
+        SCLogMessage(level as i32,
+                     CString::new(filename).unwrap().as_ptr(),
+                     line,
+                     CString::new(function).unwrap().as_ptr(),
+                     code,
+                     CString::new(message).unwrap().as_ptr());
+    }
+}
+
+/// Return the function name, but for now just return <rust> as Rust
+/// has no macro to return the function name, but may in the future,
+/// see: https://github.com/rust-lang/rfcs/pull/1719
+macro_rules!function {
+    () => {{ "<rust>" }}
+}
+
+#[macro_export]
+macro_rules!do_log {
+    ($level:expr, $file:expr, $line:expr, $function:expr, $code:expr,
+     $($arg:tt)*) => {
+        if get_log_level() >= $level as i32 {
+            sclog($level, $file, $line, $function, $code,
+                  &(format!($($arg)*)));
+        }
+    }
+}
+
+#[macro_export]
+macro_rules!SCLogNotice {
+    ($($arg:tt)*) => {
+        do_log!(Level::Notice, file!(), line!(), function!(), 0, $($arg)*);
+    }
+}
+
+#[macro_export]
+macro_rules!SCLogInfo {
+    ($($arg:tt)*) => {
+        do_log!(Level::Info, file!(), line!(), function!(), 0, $($arg)*);
+    }
+}
+
+#[macro_export]
+macro_rules!SCLogPerf {
+    ($($arg:tt)*) => {
+        do_log!(Level::Perf, file!(), line!(), function!(), 0, $($arg)*);
+    }
+}
+
+#[macro_export]
+macro_rules!SCLogConfig {
+    ($($arg:tt)*) => {
+        do_log!(Level::Config, file!(), line!(), function!(), 0, $($arg)*);
+    }
+}
+
+#[macro_export]
+macro_rules!SCLogDebug {
+    ($($arg:tt)*) => {
+        do_log!(Level::Debug, file!(), line!(), function!(), 0, $($arg)*);
+    }
+}
+
+#[no_mangle]
+pub extern "C" fn rs_log_init(level: i32) {
+    unsafe {
+        LEVEL = level;
+    }
+}
index 01daa039786ccfae128a85f98bd888d5ffcd13a0..09325831c009d4fe6921d0504cc996b912ae0466 100644 (file)
@@ -322,6 +322,7 @@ runmode-unittests.c runmode-unittests.h \
 runmode-unix-socket.c runmode-unix-socket.h \
 runmode-tile.c runmode-tile.h \
 runmodes.c runmodes.h \
+rust.h \
 source-af-packet.c source-af-packet.h \
 source-erf-dag.c source-erf-dag.h \
 source-erf-file.c source-erf-file.h \
diff --git a/src/rust.h b/src/rust.h
new file mode 100644 (file)
index 0000000..6f7c3b8
--- /dev/null
@@ -0,0 +1,18 @@
+/* Copyright (C) 2017 Open Information Security Foundation
+ *
+ * You can copy, redistribute or modify this Program 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
+ * version 2 along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+void rs_log_init(int32_t level);
index 7af9d89b7a742554f9ebf1fb4ebdb220172f250c..a5bf99ce83590527ef3f70a451076152cb248237 100644 (file)
 #include "util-unittest.h"
 #include "util-syslog.h"
 
+#ifdef HAVE_RUST
+#include "rust.h"
+#endif
+
 #include "conf.h"
 
 /* holds the string-enum mapping for the enums held in the table SCLogLevel */
@@ -1256,6 +1260,10 @@ void SCLogInitLogModule(SCLogInitData *sc_lid)
 
     //SCOutputPrint(sc_did->startup_message);
 
+#ifdef HAVE_RUST
+    rs_log_init(sc_log_global_log_level);
+#endif
+
     return;
 }