Index: pkgutil.cc =================================================================== --- pkgutil.cc (revision 1) +++ pkgutil.cc (revision 2) @@ -98,9 +98,7 @@ packages[name] = info; } -#ifndef NDEBUG - cerr << packages.size() << " packages found in database" << endl; -#endif + dbg(t2s(packages.size()) + " packages found in database"); } void pkgutil::db_commit() @@ -149,9 +147,7 @@ if (rename(dbfilename_new.c_str(), dbfilename.c_str()) == -1) throw runtime_error_with_errno("could not rename " + dbfilename_new + " to " + dbfilename); -#ifndef NDEBUG - cerr << packages.size() << " packages written to database" << endl; -#endif + dbg(t2s(packages.size()) + " packages written to database"); } void pkgutil::db_add_pkg(const string& name, const pkginfo_t& info) @@ -169,22 +165,14 @@ set files = packages[name].files; packages.erase(name); -#ifndef NDEBUG - cerr << "Removing package phase 1 (all files in package):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Removing package phase 1 (all files in package):", files); // Don't delete files that still have references for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i) for (set::const_iterator j = i->second.files.begin(); j != i->second.files.end(); ++j) files.erase(*j); -#ifndef NDEBUG - cerr << "Removing package phase 2 (files that still have references excluded):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Removing package phase 2 (files that still have references excluded):", files); // Delete the files for (set::const_reverse_iterator i = files.rbegin(); i != files.rend(); ++i) { @@ -201,32 +189,20 @@ set files = packages[name].files; packages.erase(name); -#ifndef NDEBUG - cerr << "Removing package phase 1 (all files in package):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Removing package phase 1 (all files in package):", files); // Don't delete files found in the keep list for (set::const_iterator i = keep_list.begin(); i != keep_list.end(); ++i) files.erase(*i); -#ifndef NDEBUG - cerr << "Removing package phase 2 (files that is in the keep list excluded):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Removing package phase 2 (files that is in the keep list excluded):", files); // Don't delete files that still have references for (packages_t::const_iterator i = packages.begin(); i != packages.end(); ++i) for (set::const_iterator j = i->second.files.begin(); j != i->second.files.end(); ++j) files.erase(*j); -#ifndef NDEBUG - cerr << "Removing package phase 3 (files that still have references excluded):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Removing package phase 3 (files that still have references excluded):", files); // Delete the files for (set::const_reverse_iterator i = files.rbegin(); i != files.rend(); ++i) { @@ -247,12 +223,8 @@ for (set::const_iterator j = files.begin(); j != files.end(); ++j) i->second.files.erase(*j); -#ifndef NDEBUG - cerr << "Removing files:" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif - + dbg("Removing files:", files); + // Don't delete files found in the keep list for (set::const_iterator i = keep_list.begin(); i != keep_list.end(); ++i) files.erase(*i); @@ -282,11 +254,7 @@ } } -#ifndef NDEBUG - cerr << "Conflicts phase 1 (conflicts in database):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Conflicts phase 1 (conflicts in database):", files); // Find conflicting files in filesystem for (set::iterator i = info.files.begin(); i != info.files.end(); ++i) { @@ -295,11 +263,7 @@ files.insert(files.end(), *i); } -#ifndef NDEBUG - cerr << "Conflicts phase 2 (conflicts in filesystem added):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Conflicts phase 2 (conflicts in filesystem added):", files); // Exclude directories set tmp = files; @@ -308,22 +272,14 @@ files.erase(*i); } -#ifndef NDEBUG - cerr << "Conflicts phase 3 (directories excluded):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Conflicts phase 3 (directories excluded):", files); // If this is an upgrade, remove files already owned by this package if (packages.find(name) != packages.end()) { for (set::const_iterator i = packages[name].files.begin(); i != packages[name].files.end(); ++i) files.erase(*i); -#ifndef NDEBUG - cerr << "Conflicts phase 4 (files already owned by this package excluded):" << endl; - copy(files.begin(), files.end(), ostream_iterator(cerr, "\n")); - cerr << endl; -#endif + dbg("Conflicts phase 4 (files already owned by this package excluded):", files); } return files; @@ -752,3 +708,18 @@ free(path); } } + +#ifndef NDEBUG +void dbg(const string &msg) +{ + cerr << msg << endl; +} + +void dbg(const string &msg, const set &filenames) +{ + cerr << msg << endl; + copy(filenames.begin(), filenames.end(), + ostream_iterator(cerr, "\n")); + cerr << endl; +} +#endif Index: pkgutil.h =================================================================== --- pkgutil.h (revision 1) +++ pkgutil.h (revision 2) @@ -105,4 +105,22 @@ bool permissions_equal(const string& file1, const string& file2); void file_remove(const string& basedir, const string& filename); +// Debug helpers +#ifndef NDEBUG +#include + +template +string t2s(const in_t &in) +{ + ostringstream oss; + oss << in; + return oss.str(); +} + +void dbg(const string &msg); +void dbg(const string &msg, const set &filenames); +#else +#define dbg(...) ; +#endif /* NDEBUG */ + #endif /* PKGUTIL_H */ Index: Makefile =================================================================== --- Makefile (revision 1) +++ Makefile (revision 2) @@ -30,6 +30,9 @@ CXXFLAGS += -DNDEBUG CXXFLAGS += -O2 -Wall -pedantic -D_GNU_SOURCE -DVERSION=\"$(VERSION)\" \ -Ilibtar-$(LIBTAR_VERSION)/lib -Ilibtar-$(LIBTAR_VERSION)/listhash +# Since we use #define x(...) we don't want to get this warning +# Hope variadic macros will be in the next ISO C++ standart. +CXXFLAGS += -Wno-variadic-macros LDFLAGS += -static -Llibtar-$(LIBTAR_VERSION)/lib -ltar -lz Index: pkgadd.cc =================================================================== --- pkgadd.cc (revision 1) +++ pkgadd.cc (revision 2) @@ -194,13 +194,7 @@ } } -#ifndef NDEBUG - cerr << "Keep list:" << endl; - for (set::const_iterator j = keep_list.begin(); j != keep_list.end(); j++) { - cerr << " " << (*j) << endl; - } - cerr << endl; -#endif - + dbg("Keep list:", keep_list); + return keep_list; }