syslog_ng_common/proxies/parser/
mod.rs

1// Copyright (c) 2016 Tibor Benke <ihrwein@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use LogMessage;
10use Pipe;
11
12mod option_error;
13mod proxy;
14
15pub use self::option_error::OptionError;
16pub use self::proxy::ParserProxy;
17
18pub trait ParserBuilder<P: Pipe> {
19    type Parser: Parser<P>;
20    fn new() -> Self;
21    fn option(&mut self, _name: String, _value: String) {}
22    fn build(self) -> Result<Self::Parser, OptionError>;
23}
24
25pub trait Parser<P: Pipe>: Clone {
26    fn parse(&mut self, pipe: &mut P, msg: &mut LogMessage, input: &str) -> bool;
27}
28
29#[macro_export]
30macro_rules! parser_plugin {
31    ($name:ty) => {
32
33pub mod _parser_plugin {
34    use $crate::{c_int, c_char};
35    use $crate::LogMessage;
36    use $crate::LogParser;
37    use $crate::init_logger;
38    use $crate::ParserProxy;
39
40    use std::ffi::CStr;
41
42    use super::*;
43
44    #[no_mangle]
45    pub extern fn native_parser_proxy_init(this: &mut ParserProxy<$name>) -> c_int {
46        let res = this.init();
47
48        match res {
49            true => 1,
50            false => 0
51        }
52    }
53
54    #[no_mangle]
55    pub extern fn native_parser_proxy_free(_: Box<ParserProxy<$name>>) {
56    }
57
58    #[no_mangle]
59    pub extern fn native_parser_proxy_set_option(slf: &mut ParserProxy<$name>, key: *const c_char, value: *const c_char) {
60        let k: String = unsafe { CStr::from_ptr(key).to_owned().to_string_lossy().into_owned() };
61        let v: String = unsafe { CStr::from_ptr(value).to_owned().to_string_lossy().into_owned() };
62
63        slf.set_option(k, v);
64    }
65
66    #[no_mangle]
67    pub extern fn native_parser_proxy_process(this: &mut ParserProxy<$name>, parent: *mut $crate::sys::LogParser, msg: *mut $crate::sys::LogMessage, input: *const c_char) -> c_int {
68        let input = unsafe { CStr::from_ptr(input).to_str() };
69        let mut parent = LogParser::wrap_raw(parent);
70        let mut msg = LogMessage::wrap_raw(msg);
71        let result = match input {
72            Ok(input) => this.process(&mut parent, &mut msg, input),
73            Err(err) => {
74                error!("{}", err);
75                false
76            }
77        };
78
79        match result {
80            true => 1,
81            false => 0
82        }
83    }
84
85    #[no_mangle]
86    pub extern fn native_parser_proxy_new() -> Box<ParserProxy<$name>> {
87        init_logger();
88        Box::new(ParserProxy::new())
89    }
90
91    #[no_mangle]
92    pub extern fn native_parser_proxy_clone(slf: &ParserProxy<$name>) -> Box<ParserProxy<$name>> {
93        let cloned = (*slf).clone();
94        Box::new(cloned)
95    }
96}
97    }
98}