From 4bc2caadbfbb831dbaa7faf291f5e00fe1182583 Mon Sep 17 00:00:00 2001 From: Mario Krehl Date: Sat, 9 Dec 2017 21:11:26 +0100 Subject: Tests with Regexin main; refactoring of config handling code --- Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- example_config.toml | 7 +++- src/config.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++------- src/error.rs | 4 +++ src/main.rs | 45 ++++++++++++++++++++++---- 6 files changed, 206 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43e2f0b..6e7724c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,7 @@ dependencies = [ "clap 2.29.0 (registry+https://github.com/rust-lang/crates.io-index)", "error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -12,6 +13,14 @@ dependencies = [ "toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "aho-corasick" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "ansi_term" version = "0.10.2" @@ -116,6 +125,11 @@ dependencies = [ "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "lazy_static" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "libc" version = "0.2.34" @@ -126,6 +140,14 @@ name = "log" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "memchr" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num-traits" version = "0.1.41" @@ -149,6 +171,23 @@ dependencies = [ "redox_syscall 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "rustc-demangle" version = "0.1.5" @@ -249,6 +288,15 @@ dependencies = [ "unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread_local" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "time" version = "0.1.38" @@ -278,11 +326,29 @@ name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unreachable" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "utf8-ranges" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "vec_map" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.2.8" @@ -294,6 +360,7 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] +"checksum aho-corasick 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d6531d44de723825aa81398a6415283229725a00fa30713812ab9323faa82fc4" "checksum ansi_term 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6b3568b48b7cefa6b8ce125f9bb4989e52fbcc29ebea88df04cc7c5f12f70455" "checksum atty 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "21e50800ec991574876040fff8ee46b136a53e985286fbe6a3bdfe6421b78860" "checksum backtrace 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8709cc7ec06f6f0ae6c2c7e12f6ed41540781f72b488d83734978295ceae182e" @@ -307,12 +374,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum error-chain 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ff511d5dc435d703f4971bc399647c9bc38e20cb41452e3b9feb4765419ed3f3" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d" "checksum libc 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "36fbc8a8929c632868295d0178dd8f63fc423fd7537ad0738372bd010b3ac9b0" "checksum log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "880f77541efa6e5cc74e76910c9884d9859683118839d6a1dc3b11e63512565b" +"checksum memchr 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "796fba70e76612589ed2ce7f45282f5af869e0fdd7cc6199fa1aa1f1d591ba9d" "checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" "checksum redox_syscall 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "ab105df655884ede59d45b7070c8a65002d921461ee813a024558ca16030eea0" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum regex 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ac6ab4e9218ade5b423358bbd2567d1617418403c7a512603630181813316322" +"checksum regex-syntax 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ad890a5eef7953f55427c50575c680c42841653abd2b028b68cd223d157f62db" "checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e" "checksum serde 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)" = "6a7c37d7f192f00041e8a613e936717923a71bc0c9051fc4425a49b104140f05" "checksum serde_derive 1.0.23 (registry+https://github.com/rust-lang/crates.io-index)" = "0672de7300b02bac3f3689f8faea813c4a1ea9fe0cb49e80f714231d267518a2" @@ -325,10 +396,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c0b59b6b4b44d867f1370ef1bd91bfb262bf07bf0ae65c202ea2fbc16153b693" +"checksum thread_local 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "279ef31c19ededf577bfd12dfae728040a21f635b06a24cd670ff510edd38963" "checksum time 0.1.38 (registry+https://github.com/rust-lang/crates.io-index)" = "d5d788d3aa77bc0ef3e9621256885555368b47bd495c13dd2e7413c89f845520" "checksum toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a7540f4ffc193e0d3c94121edb19b055670d369f77d5804db11ae053a45b6e7e" "checksum unicode-width 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" +"checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum utf8-ranges 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "662fab6525a98beff2921d7f61a39e7d59e0b425ebc7d0d9e66d316e55124122" "checksum vec_map 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" "checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" "checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" diff --git a/Cargo.toml b/Cargo.toml index d5f0c80..cf30ef4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,9 +7,9 @@ version = "0.1.0" clap = "2" error-chain = "0.11" log = "^0.3.8" +regex = "0.2" serde = "1" serde_derive = "1" serde_json = "1" simplelog = "^0.4.3" toml = "^0.4" - diff --git a/example_config.toml b/example_config.toml index 946a3d7..95cdfa9 100644 --- a/example_config.toml +++ b/example_config.toml @@ -1,6 +1,11 @@ +[[item]] +file = "/var/log/antikoerper/temp.zone1" +regex = "(?P\\d+)\\s(?P\\d+)" +alias = "tempmain" + [[item]] file = "/var/log/antikoerper/os.load" -regex = "^(\\d{10,}) (\\d+\\.\\d\\d) (\\d+\\.\\d\\d) (\\d+\\.\\d\\d).*$" +regex = "(\\d+)\\s(\\d+\\.\\d\\d)\\s(\\d+\\.\\d\\d)\\s(\\d+\\.\\d\\d)\\s" alias = "load" diff --git a/src/config.rs b/src/config.rs index 5d1885c..635be32 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,24 +5,43 @@ extern crate toml; use std::fs::File; use std::io::Read; +use regex::Regex; use error::*; -/// Main Config struct -#[derive(Debug, Deserialize)] -pub struct Config { - item : Vec, -} +//------------------------------------// +// structs for deserialization // +//------------------------------------// -/// Holds data for one antikoerper Log-File -#[derive(Debug, Deserialize)] -struct Item { +/// Holds data for one antikoerper Log-File. +/// Used for deserialization only +#[derive(Clone, Debug, Deserialize)] +pub struct LogItemDeser { file : String, regex : String, alias : String, } -impl Config { - pub fn load(filename : String) -> Result { +impl LogItemDeser { + pub fn file(&self) -> String { + self.file.clone() + } + pub fn regex(&self) -> Result { + debug!("trying to parse regex `{}`", self.regex); + Regex::new(self.regex.as_str()).chain_err(|| format!("failed to parse regex `{}`", self.regex)) + } + pub fn alias(&self) -> String { + self.alias.clone() + } +} + +/// Used for deserialization only +#[derive(Debug, Deserialize)] +pub struct ConfigDeser { + item : Vec, +} + +impl ConfigDeser { + pub fn load(filename : String) -> Result { debug!("configuration file name: '{}'", filename); let mut file = File::open(filename.clone()) .chain_err(|| "configuration file could not be opened")?; @@ -33,10 +52,61 @@ impl Config { debug!("configuration file successfully read"); match toml::from_str(content.as_str()) { Ok(config) => { - debug!("configuration file successfully parsed"); + info!("successfully parsed configuration file"); Ok(config) }, _ => Err(ErrorKind::ConfigParseError(filename).into()), } } + + pub fn get_items(&self) -> Vec { + self.item.clone() + } +} + +//------------------------------------// +// struct to access data later on // +//------------------------------------// + +pub struct LogItem { + file : String, + regex : Regex, + aliases : Vec, +} + +impl LogItem { + fn from_log_item_deser(lid : LogItemDeser) -> Result { + debug!("trying to parse regex `{}`", lid.regex); + let l_regex = Regex::new(lid.regex.as_str()) + .chain_err(|| format!("regex not parseable: '{}'", lid.regex))?; + let cnames : Vec = l_regex + .capture_names() + .skip(2) + .filter_map(|n| n) + .map(|n| String::from(n)) + .collect(); + debug!("capture names: {:?}", cnames); + let mut als : Vec = Vec::new(); + for name in cnames { + let mut temp = String::from(lid.alias.as_str()); + temp.push('.'); + temp.push_str(name.as_str()); + als.push(temp); + } + debug!("aliases: {:?}", als); + + Ok(LogItem { file : lid.file, regex : l_regex, aliases : als }) + } } + +pub struct Config { + items : Vec, + all_aliases : Vec, +} + +impl Config { + pub fn load(filename : String) -> Result { + Err(ErrorKind::ConfigParseError(String::from("meh")).into()) + } +} + diff --git a/src/error.rs b/src/error.rs index 6258c10..af5af98 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,6 +5,10 @@ error_chain! { description("configuration file could not be parsed"), display("configuration file could not be parsed: '{}'", filename), } + RegexParseError(regex: String) { + description("regex could not be parsed"), + display("regex not parsable: '{}'", regex), + } } } diff --git a/src/main.rs b/src/main.rs index 4a4e53d..1d4abe2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,18 +5,20 @@ extern crate clap; extern crate error_chain; #[macro_use] extern crate log; +extern crate regex; #[macro_use] extern crate serde_derive; extern crate simplelog; +use std::process::exit; +use std::fs::File; + use clap::{App, Arg}; use simplelog::{SimpleLogger, LogLevelFilter, Config as LogConfig}; -use std::process::exit; - mod error; mod config; -use config::Config; +use config::ConfigDeser; fn main() { let matches = App::new("aklog-server") @@ -47,12 +49,43 @@ fn main() { debug!("Initialized logger"); let config_file = matches.value_of("config").unwrap(); - let config = match Config::load(String::from(config_file)) { + let config = match ConfigDeser::load(String::from(config_file)) { Ok(c) => c, - Err(_) => { - error!("Failed to open/parse configuration file: '{}'", config_file); + Err(e) => { + error!("{}", e); exit(1); }, }; + let items = config.get_items(); + let aliases : Vec<_> = items.clone().into_iter() + .map(|it| it.alias()) + .collect(); + let first_item = items.first().unwrap(); + + let mut file = File::open(first_item.file()).unwrap(); + use std::io::BufReader; + use std::io::BufRead; + let mut bufreader = BufReader::new(file); + let mut line = String::new(); + let cregex = match first_item.regex() { + Ok(r) => { + info!("regex parsed successfully"); + r + }, + Err(_) => exit(2), + }; + let mut capturename_iter = cregex.capture_names().skip(1); + while let Some(Some(name)) = capturename_iter.next() { + println!("Named Capture: {}", name); + } + let mut line_iter = bufreader.lines(); + while let Some(Ok(line)) = line_iter.next() { + if cregex.is_match(line.as_str()) { + println!("{}", line); + } + else { + println!("did not match"); + } + } } -- cgit v1.2.3