changeset 236:9c3396bab02e refactoring

VERB_LOG macro into audioDB.h ... and use it everywhere.
author mas01cr
date Wed, 12 Dec 2007 16:43:04 +0000
parents 692e026947a8
children b2b39d7ecdf8
files audioDB.cpp audioDB.h create.cpp insert.cpp query.cpp
diffstat 5 files changed, 32 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/audioDB.cpp	Wed Dec 12 11:10:48 2007 +0000
+++ b/audioDB.cpp	Wed Dec 12 16:43:04 2007 +0000
@@ -128,10 +128,10 @@
   }
 
   if(args_info.verbosity_given){
-    verbosity=args_info.verbosity_arg;
-    if(verbosity<0 || verbosity>10){
+    verbosity = args_info.verbosity_arg;
+    if(verbosity < 0 || verbosity > 10){
       std::cerr << "Warning: verbosity out of range, setting to 1" << std::endl;
-      verbosity=1;
+      verbosity = 1;
     }
   }
 
@@ -142,15 +142,13 @@
     size = (off_t) args_info.size_arg * 1000000;
   }
 
-  if(args_info.radius_given){
-    radius=args_info.radius_arg;
-    if(radius<=0 || radius>1000000000){
+  if(args_info.radius_given) {
+    radius = args_info.radius_arg;
+    if(radius <= 0 || radius > 1000000000) {
       error("radius out of range");
+    } else {
+      VERB_LOG(3, "Setting radius to %f\n", radius);
     }
-    else 
-      if(verbosity>3) {
-	std::cerr << "Setting radius to " << radius << std::endl;
-      }
   }
   
   if(args_info.SERVER_given){
@@ -429,9 +427,7 @@
   if( !append && (dbH->flags & O2_FLAG_L2NORM) )
     error("Database is already L2 normed", "automatic norm on insert is enabled");
 
-  if(verbosity>2) {
-    std::cerr << "norming " << n << " vectors...";std::cerr.flush();
-  }
+  VERB_LOG(2, "norming %u vectors...", n);
 
   double* l2buf = new double[n];
   double* l2ptr = l2buf;
@@ -462,9 +458,7 @@
   memcpy(l2normTable+offset, l2buf, n*sizeof(double));
   if(l2buf)
     delete[] l2buf;
-  if(verbosity>2) {
-    std::cerr << "done..." << std::endl;
-  }
+  VERB_LOG(2, " done.");
 }
 
 int main(const unsigned argc, char* const argv[]){
--- a/audioDB.h	Wed Dec 12 11:10:48 2007 +0000
+++ b/audioDB.h	Wed Dec 12 16:43:04 2007 +0000
@@ -98,6 +98,12 @@
     var = (type) tmp; \
   }
 
+#define VERB_LOG(vv, ...) \
+  if(verbosity > vv) { \
+    fprintf(stderr, __VA_ARGS__); \
+    fflush(stderr); \
+  }
+
 typedef struct dbTableHeader {
   uint32_t magic;
   uint32_t version;
--- a/create.cpp	Wed Dec 12 11:10:48 2007 +0000
+++ b/create.cpp	Wed Dec 12 16:43:04 2007 +0000
@@ -19,9 +19,7 @@
     error("Can't create database file", dbName, "open");
   get_lock(dbfid, 1);
 
-  if(verbosity) {
-    std::cerr << "header size:" << O2_HEADERSIZE << std::endl;
-  }
+  VERB_LOG(0, "header size: %u\n", O2_HEADERSIZE);
   
   dbH = new dbTableHeaderT();
   assert(dbH);
@@ -53,10 +51,8 @@
   // write a dummy byte at the last location
   if (write (dbfid, "", 1) != 1)
     error("write error", "", "write");
-  
-  if(verbosity) {
-    std::cerr << COM_CREATE << " " << dbName << std::endl;
-  }
+
+  VERB_LOG(0, "%s %s\n", COM_CREATE, dbName);
 }
 
 void audioDB::drop(){
--- a/insert.cpp	Wed Dec 12 11:10:48 2007 +0000
+++ b/insert.cpp	Wed Dec 12 16:43:04 2007 +0000
@@ -33,19 +33,16 @@
       break;
     }
 
-  if(alreadyInserted){
-    if(verbosity) {
-      std::cerr << "Warning: key already exists in database, ignoring: " <<inFile << std::endl;
-    }
+  if(alreadyInserted) {
+    VERB_LOG(0, "key already exists in database; ignoring: %s\n", inFile);
     return;
   }
   
   // Make a track index table of features to file indexes
   unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim);
-  if(!numVectors){
-    if(verbosity) {
-      std::cerr << "Warning: ignoring zero-length feature vector file:" << key << std::endl;
-    }
+  if(!numVectors) {
+    VERB_LOG(0, "ignoring zero-length feature vector file: %s\n", key);
+
     // CLEAN UP
     munmap(indata,statbuf.st_size);
     munmap(db,dbH->dbSize);
@@ -89,10 +86,7 @@
 
   // Report status
   status(dbName);
-  if(verbosity) {
-    std::cerr << COM_INSERT << " " << dbName << " " << numVectors << " vectors " 
-	 << (statbuf.st_size-sizeof(int)) << " bytes." << std::endl;
-  }
+  VERB_LOG(0, "%s %s %u vectors %jd bytes.\n", COM_INSERT, dbName, numVectors, (intmax_t) (statbuf.st_size - sizeof(int)));
 
   // Copy the header back to the database
   memcpy (db, dbH, sizeof(dbTableHeaderT));  
@@ -229,19 +223,13 @@
 	break;
       }
   
-    if(alreadyInserted){
-      if(verbosity) {
-	std::cerr << "Warning: key already exists in database:" << thisKey << std::endl;
-      }
-    }
-    else{
-  
+    if(alreadyInserted) {
+      VERB_LOG(0, "key already exists in database: %s\n", thisKey);
+    } else {
       // Make a track index table of features to file indexes
       unsigned numVectors = (statbuf.st_size-sizeof(int))/(sizeof(double)*dbH->dim);
-      if(!numVectors){
-	if(verbosity) {
-	  std::cerr << "Warning: ignoring zero-length feature vector file:" << thisKey << std::endl;
-        }
+      if(!numVectors) {
+        VERB_LOG(0, "ignoring zero-length feature vector file: %s\n", thisKey);
       }
       else{
 	if(usingTimes){
@@ -307,12 +295,9 @@
     // CLEAN UP
     munmap(indata,statbuf.st_size);
     close(infid);
-  }while(!filesIn->eof());
+  } while(!filesIn->eof());
 
-  if(verbosity) {
-    std::cerr << COM_BATCHINSERT << " " << dbName << " " << totalVectors << " vectors " 
-	 << totalVectors*dbH->dim*sizeof(double) << " bytes." << std::endl;
-  }
+  VERB_LOG(0, "%s %s %u vectors %u bytes.\n", COM_BATCHINSERT, dbName, totalVectors, totalVectors * dbH->dim * sizeof(double));
   
   // Report status
   status(dbName);
--- a/query.cpp	Wed Dec 12 11:10:48 2007 +0000
+++ b/query.cpp	Wed Dec 12 16:43:04 2007 +0000
@@ -2,12 +2,6 @@
 
 #include "reporter.h"
 
-#define VERB_LOG(vv, ...) \
-  if(verbosity > vv) { \
-    fprintf(stderr, __VA_ARGS__); \
-    fflush(stderr); \
-  }
-
 bool audioDB::powers_acceptable(double p1, double p2) {
   if (use_absolute_threshold) {
     if ((p1 < absolute_threshold) || (p2 < absolute_threshold)) {