Merge branch 'more-debug'

This commit is contained in:
Matthias Beyer 2015-11-27 22:19:29 +01:00
commit 3820ed1aeb
7 changed files with 116 additions and 11 deletions

View file

@ -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())
}
}

View file

@ -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<Cfg> {
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()
)
}
}

View file

@ -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!");
}

View file

@ -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())
}
}

View file

@ -35,16 +35,20 @@ impl StorageBackend {
fn build<M: Module>(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<Vec<FileID>> {
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<HP>) -> Option<File>
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[..]
}

View file

@ -113,6 +113,7 @@ impl<'a> Display for MatchError<'a> {
pub fn match_header_spec<'a>(spec: &'a FileHeaderSpec, data: &'a FileHeaderData)
-> Option<MatchError<'a>>
{
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() {
@ -177,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) {
@ -230,3 +248,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)
}
}

View file

@ -99,28 +99,36 @@ impl<'a, HP> Parser<HP> 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<String, ParserError>
{
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<TextTpl, ParserError> {
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<HP> 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))
}