[niepce: 20/29] rust+fwk: implement err_out, dbg_out and dbg_assert macros



commit 854af6853087d32d384839072f9da2c6b27873c0
Author: Hubert Figuière <hub figuiere net>
Date:   Sun Aug 27 10:19:25 2017 -0400

    rust+fwk: implement err_out, dbg_out and dbg_assert macros

 src/engine/db/filebundle.rs |    7 ++-----
 src/engine/db/library.rs    |   10 ++++++----
 src/fwk/base/debug.rs       |   37 +++++++++++++++++++++++++++++++++++++
 src/fwk/base/mod.rs         |    2 ++
 src/fwk/mod.rs              |    1 +
 src/fwk/toolkit/mimetype.rs |    1 +
 src/lib.rs                  |    1 +
 7 files changed, 50 insertions(+), 9 deletions(-)
---
diff --git a/src/engine/db/filebundle.rs b/src/engine/db/filebundle.rs
index 9dc4dcd..f63808c 100644
--- a/src/engine/db/filebundle.rs
+++ b/src/engine/db/filebundle.rs
@@ -49,7 +49,7 @@ impl FileBundle {
     }
 
     pub fn add(&mut self, path: &str) -> bool {
-        println!("path {}", path);
+        dbg_out!("path {}", path);
         let mime_type = MimeType::new(path);
         let mut added = true;
 
@@ -83,10 +83,7 @@ impl FileBundle {
             self.main = String::from(path);
             self.file_type = FileType::VIDEO;
         } else {
-//            let cstr = &*unsafe { CStr::from_ptr(mime_type.c_str()) };
-            println!("Unknown file {} of type", path);//, cstr.to_string_lossy());
-            //DBG_OUT("Unkown file %s of type %s\n", path.c_str(),
-            //        mime_type.string().c_str());
+            dbg_out!("Unknown file {} of type {:?}", path, mime_type);
             added = false;
         }
         added
diff --git a/src/engine/db/library.rs b/src/engine/db/library.rs
index 60ce887..5aa955b 100644
--- a/src/engine/db/library.rs
+++ b/src/engine/db/library.rs
@@ -93,8 +93,10 @@ impl Library {
         let version = self.check_database_version();
         if version == -1 {
             // error
+            dbg_out!("version check -1");
         } else if version == 0 {
-            // version == 0
+            // let's create our DB
+            dbg_out!("version == 0");
             return self.init_db();
         } else if version != DB_SCHEMA_VERSION {
             // WAT?
@@ -240,7 +242,7 @@ impl Library {
                         return None;
                     }
                     let id = conn.last_insert_rowid();
-                    // DBG_OUT("last row inserted %Ld", (long long)id);
+                    dbg_out!("last row inserted {}", id);
                     return Some(LibFolder::new(id, &foldername));
                 }
             }
@@ -385,7 +387,7 @@ impl Library {
         let filename = Self::leaf_name_for_pathname(file).unwrap_or(String::from(""));
         let fs_file_id = self.add_fs_file(file);
         if fs_file_id <= 0 {
-            // ERR_OUT("add fsfile failed");
+            err_out!("add fsfile failed");
             return 0;
         }
 
@@ -550,7 +552,7 @@ impl Library {
                         }
                         let p = p.unwrap();
                         if p.exists() {
-                            // DBG_OUT("%s already exist", p.c_str());
+                            dbg_out!("{:?} already exist", p);
                         }
                         let mut xmppacket = fwk::XmpMeta::new();
                         xmppacket.unserialize(&xmp_buffer);
diff --git a/src/fwk/base/debug.rs b/src/fwk/base/debug.rs
new file mode 100644
index 0000000..3730d79
--- /dev/null
+++ b/src/fwk/base/debug.rs
@@ -0,0 +1,37 @@
+
+#[macro_export]
+macro_rules! dbg_out {
+    ( $( $x:expr ),* ) => {
+        print!("DEBUG: ");
+        println!( $($x),* );
+    };
+}
+
+#[macro_export]
+macro_rules! err_out {
+    ( $( $x:expr ),* ) => {
+        print!("ERROR: ");
+        println!( $($x),* );
+    };
+}
+
+#[macro_export]
+macro_rules! dbg_assert {
+    ( $cond:expr,  $msg:expr ) => {
+        {
+            if !$cond {
+                print!("ASSERT: {}", stringify!($cond));
+                println!( $msg );
+            }
+        }
+    };
+}
+
+#[cfg(test)]
+mod tests {
+    #[test]
+    fn it_works() {
+        dbg_out!("debug {}", 42);
+        err_out!("error {}", 69);
+    }
+}
diff --git a/src/fwk/base/mod.rs b/src/fwk/base/mod.rs
index 83354d3..1cf2abe 100644
--- a/src/fwk/base/mod.rs
+++ b/src/fwk/base/mod.rs
@@ -19,5 +19,7 @@
 
 pub mod fractions;
 pub mod date;
+#[macro_use]
+pub mod debug;
 
 pub type PropertyIndex = u32;
diff --git a/src/fwk/mod.rs b/src/fwk/mod.rs
index d3175fc..ef0f3e1 100644
--- a/src/fwk/mod.rs
+++ b/src/fwk/mod.rs
@@ -18,6 +18,7 @@
  */
 
 pub mod utils;
+#[macro_use]
 pub mod base;
 pub mod toolkit;
 
diff --git a/src/fwk/toolkit/mimetype.rs b/src/fwk/toolkit/mimetype.rs
index 0399cda..6c1c6a9 100644
--- a/src/fwk/toolkit/mimetype.rs
+++ b/src/fwk/toolkit/mimetype.rs
@@ -25,6 +25,7 @@ pub enum MType {
     Xmp
 }
 
+#[derive(Debug)]
 pub struct MimeType {
 //    name: String,
     mtype: MType,
diff --git a/src/lib.rs b/src/lib.rs
index e1b6839..d495a93 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -24,6 +24,7 @@ extern crate gio_sys;
 extern crate gio;
 extern crate rusqlite;
 
+#[macro_use]
 pub mod fwk;
 pub mod engine;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]