From 990252e4e470b239966df546bede832a9032cb9e Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:21:40 +0100 Subject: [PATCH 1/9] Implement Debug for Runtime --- src/runtime.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/runtime.rs b/src/runtime.rs index dc64b181..5901b439 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -3,6 +3,10 @@ extern crate log; pub use cli::CliConfig; pub use configuration::Configuration as Cfg; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::fmt::Error; + use log::{LogRecord, LogLevel, LogLevelFilter, LogMetadata, SetLoggerError}; pub struct ImagLogger { @@ -79,3 +83,15 @@ impl<'a> Runtime<'a> { } } + +impl<'a> Debug for Runtime<'a> { + + fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { + write!(f, "Runtime (verbose: {}, debugging: {}, rtp: {})", + self.is_verbose(), + self.is_debugging(), + self.get_rtp()) + } + +} + From a487fbcaf4a50664a2efeccf1df9304b0554e450 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:18:38 +0100 Subject: [PATCH 2/9] Implement Debug for CliConfig --- src/cli.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/cli.rs b/src/cli.rs index 27ac1f3b..2a4f25bc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,10 @@ extern crate clap; use clap::{App, ArgMatches}; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::fmt::Error; + pub struct ModuleConfig { pub load : bool, } @@ -39,3 +43,13 @@ impl<'a> CliConfig<'a> { } } +impl<'a> Debug for CliConfig<'a> { + + fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { + write!(f, "CliConfig (verbose: {}, debugging: {}, rtp: {})", + self.is_verbose(), + self.is_debugging(), + self.get_rtp().or(Some(String::from("NONE"))).unwrap()) + } + +} From beccb7ab3d32cbab9c647ad8d3d67c261dbb9de3 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:22:21 +0100 Subject: [PATCH 3/9] Implement Debug for Configuration --- src/configuration.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/configuration.rs b/src/configuration.rs index 253cb3e4..a4ff2971 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -6,6 +6,10 @@ use std::path::Path; use config::reader::from_file; use config::types::Config as Cfg; +use std::fmt::Debug; +use std::fmt::Formatter; +use std::fmt::Error; + pub struct Configuration { pub rtp : String, pub store_sub : String, @@ -68,3 +72,16 @@ fn fetch_config(rtp: &String) -> Option { from_file(Path::new(&(rtp.clone() + "/config"))).ok() } +impl Debug for Configuration { + + fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { + write!(f, "Configuration (verbose: {}, debugging: {}, rtp: {}, store path: {})", + self.is_verbose(), + self.is_debugging(), + self.get_rtp(), + self.store_path_str() + ) + } + +} + From beee7b20b4f3dd4941e75983789216b78c2309e4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:24:58 +0100 Subject: [PATCH 4/9] Add debug output in main --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 3f6723af..11a18c10 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,8 +27,12 @@ fn main() { let logger = ImagLogger::init(&configuration, &config); debug!("Logger created!"); + debug!("CliConfig : {:?}", &config); + debug!("Configuration: {:?}", &configuration); + let rt = Runtime::new(configuration, config); - debug!("Runtime created!"); + + debug!("Runtime : {:?}", &rt); info!("Hello, world!"); } From 55d6de627acd995229a4a56e7e07ed2f9b051a09 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:31:46 +0100 Subject: [PATCH 5/9] Add debug output in match_header_spec() --- src/storage/file.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/storage/file.rs b/src/storage/file.rs index 74f49a23..a5bc4024 100644 --- a/src/storage/file.rs +++ b/src/storage/file.rs @@ -113,6 +113,7 @@ impl<'a> Display for MatchError<'a> { pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData) -> Option> { + debug!("Start matching:\n'{:?}'\non\n{:?}", spec, data); match (spec, data) { (&FileHeaderSpec::Null, &FileHeaderData::Null) => { } (&FileHeaderSpec::Bool, &FileHeaderData::Bool(_)) => { } @@ -125,7 +126,11 @@ pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData) &FileHeaderSpec::Key{name: ref kname, value_type: ref vtype}, &FileHeaderData::Key{name: ref n, value: ref val} ) => { + debug!("Matching Key: '{:?}' == '{:?}', Value: '{:?}' == '{:?}'", + kname, n, + vtype, val); if kname != n { + debug!("Keys not matching"); unimplemented!(); } return match_header_spec(&*vtype, &*val); @@ -135,6 +140,8 @@ pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData) &FileHeaderSpec::Map{keys: ref sks}, &FileHeaderData::Map{keys: ref dks} ) => { + debug!("Matching Map: '{:?}' == '{:?}'", sks, dks); + for (s, d) in sks.iter().zip(dks.iter()) { let res = match_header_spec(s, d); if res.is_some() { @@ -147,6 +154,7 @@ pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData) &FileHeaderSpec::Array{allowed_types: ref vtypes}, &FileHeaderData::Array{values: ref vs} ) => { + debug!("Matching Array: '{:?}' == '{:?}'", vtypes, vs); for (t, v) in vtypes.iter().zip(vs.iter()) { let res = match_header_spec(t, v); if res.is_some() { From c82f9ea6ae246bd678c0b024fb9e98d503109022 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:37:08 +0100 Subject: [PATCH 6/9] Implement Debug for File --- src/storage/file.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/storage/file.rs b/src/storage/file.rs index a5bc4024..9125a9c1 100644 --- a/src/storage/file.rs +++ b/src/storage/file.rs @@ -238,3 +238,11 @@ impl File { } } +impl Debug for File { + + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + write!(f, "File[{:?}] header: '{:?}', data: '{:?}')", + self.id, self.header, self.data) + } +} + From 20dc562dee538e8d55d4ceec298b0d5255e748ab Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 19:37:20 +0100 Subject: [PATCH 7/9] Do debug output when creating a file object --- src/storage/file.rs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/storage/file.rs b/src/storage/file.rs index 9125a9c1..800cccbc 100644 --- a/src/storage/file.rs +++ b/src/storage/file.rs @@ -185,43 +185,53 @@ pub struct File { impl File { pub fn new() -> File { - File { + let f = File { header: FileHeaderData::Null, data: String::from(""), id: File::get_new_file_id(), - } + }; + debug!("Create new File object: {:?}", f); + f } pub fn from_parser_result(id: FileID, header: FileHeaderData, data: String) -> File { - File { + let f = File { header: header, data: data, id: id, - } + }; + debug!("Create new File object from parser result: {:?}", f); + f } pub fn new_with_header(h: FileHeaderData) -> File { - File { + let f = File { header: h, data: String::from(""), id: File::get_new_file_id(), - } + }; + debug!("Create new File object with header: {:?}", f); + f } pub fn new_with_data(d: String) -> File { - File { + let f = File { header: FileHeaderData::Null, data: d, id: File::get_new_file_id(), - } + }; + debug!("Create new File object with data: {:?}", f); + f } pub fn new_with_content(h: FileHeaderData, d: String) -> File { - File { + let f = File { header: h, data: d, id: File::get_new_file_id(), - } + }; + debug!("Create new File object with content: {:?}", f); + f } pub fn contents(&self) -> (FileHeaderData, String) { From 53a14ea1dc87b43531dd09cb45d2c2f957db78b4 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 22:14:02 +0100 Subject: [PATCH 8/9] Add debug output in storage backend code --- src/storage/backend.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/storage/backend.rs b/src/storage/backend.rs index 90489ec5..2b8ab577 100644 --- a/src/storage/backend.rs +++ b/src/storage/backend.rs @@ -35,16 +35,20 @@ impl StorageBackend { fn build(rt: &Runtime, m: &M) -> StorageBackend { let path = rt.get_rtp() + m.name() + "/store"; + // TODO: Don't use "/store" but value from configuration + debug!("Building StorageBackend for {}", path); StorageBackend::new(path) } fn get_file_ids(&self) -> Option> { + debug!("Getting files from {}", self.basepath); let list = glob(&self.basepath[..]); if let Ok(globlist) = list { let mut v = vec![]; for entry in globlist { if let Ok(path) = entry { + debug!(" - File: {:?}", path); v.push(from_pathbuf(&path)); } else { // Entry is not a path @@ -70,8 +74,10 @@ impl StorageBackend { if let Ok(string) = written { let path = self.build_filepath(&f); debug!("Writing file: {}", path); + debug!(" contents: {}", string); Ok(Ok(())) } else { + debug!("Error parsing : {:?}", f.contents()); Err(written.err().unwrap()) } } @@ -87,13 +93,17 @@ impl StorageBackend { let contents = p.write(f.contents()); if contents.is_err() { + debug!("Error parsing contents: {:?}", f.contents()); return Err(contents.err().unwrap()); } let content = contents.unwrap(); + debug!("Success parsing content : {}", content); let path = self.build_filepath(&f); + debug!("Trying to write to file at {}", path); if let Err(_) = FSFile::open(&path) { + debug!("Error opening {}", path); return Ok(Err(StorageBackendError::new( String::from("File::open()"), format!("Tried to open '{}'", path), @@ -103,6 +113,7 @@ impl StorageBackend { if let Ok(mut file) = FSFile::create(&path) { if let Err(writeerr) = file.write_all(&content.clone().into_bytes()) { + debug!("Error writing to {}", path); return Ok(Err(StorageBackendError::new( String::from("File::write()"), format!("Tried to write '{}'", path), @@ -111,6 +122,7 @@ impl StorageBackend { } } + debug!("Successfully written to file."); Ok(Ok(())) } @@ -124,11 +136,15 @@ impl StorageBackend { pub fn get_file_by_id<'a, HP>(&self, id: FileID, p: &Parser) -> Option where HP: FileHeaderParser<'a> { + debug!("Searching for file with id '{}'", id); if let Ok(mut fs) = FSFile::open(self.build_filepath_with_id(id.clone())) { let mut s = String::new(); fs.read_to_string(&mut s); + debug!("Success reading file with id '{}'", id); + debug!("Parsing to internal structure now"); p.read(s).and_then(|(h, d)| Ok(File::from_parser_result(id, h, d))).ok() } else { + debug!("No file with id '{}'", id); None } } @@ -138,6 +154,7 @@ impl StorageBackend { } fn build_filepath_with_id(&self, id: FileID) -> String { + debug!("Building filepath for id '{}'", id); self.basepath.clone() + &id[..] } From 9edad2800fd5ba58a1c1a92de49d5028dbff910d Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 27 Nov 2015 22:18:26 +0100 Subject: [PATCH 9/9] Add debugging output in storage parser code --- src/storage/parser.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/storage/parser.rs b/src/storage/parser.rs index f36eb4f1..dc0d28c9 100644 --- a/src/storage/parser.rs +++ b/src/storage/parser.rs @@ -99,28 +99,36 @@ impl<'a, HP> Parser where pub fn read(&self, s: String) -> Result<(FileHeaderData, String), ParserError> { + debug!("Reading into internal datastructure: '{}'", s); let divided = self.divide_text(&s); if divided.is_err() { + debug!("Error reading into internal datastructure"); return Err(divided.err().unwrap()); } let (header, data) = divided.ok().unwrap(); + debug!("Header = '{:?}'", header); + debug!("Data = '{:?}'", data); let h_parseres = try!(self.headerp.read(header)); + debug!("Success parsing header"); Ok((h_parseres, data.unwrap_or(String::new()))) } pub fn write(&self, tpl : (FileHeaderData, String)) -> Result { + debug!("Parsing internal datastructure to String"); let (header, data) = tpl; let h_text = try!(self.headerp.write(&header)); + debug!("Success translating header"); Ok(h_text + &data[..]) } fn divide_text(&self, text: &String) -> Result { + debug!("Splitting: '{}'", text); let re = Regex::new(r"(?m)^\-\-\-$\n(.*)^\-\-\-$\n(.*)").unwrap(); let captures = re.captures(&text[..]).unwrap_or( @@ -137,6 +145,9 @@ impl<'a, HP> Parser where let header = captures.at(0).map(|s| String::from(s)); let content = captures.at(1).map(|s| String::from(s)); + + debug!("Splitted, Header = '{:?}'", header); + debug!("Splitted, Data = '{:?}'", content); Ok((header, content)) }