diff options
author | randomdan <randomdan@localhost> | 2012-04-24 10:51:44 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2012-04-24 10:51:44 +0000 |
commit | 894b12fe1fffac3a7439f2fe33a10f926f9fd603 (patch) | |
tree | 73f082cf459daf693267e31da544434cee0fb69b | |
parent | Tidy up some error handling and mutables (diff) | |
download | project2-894b12fe1fffac3a7439f2fe33a10f926f9fd603.tar.bz2 project2-894b12fe1fffac3a7439f2fe33a10f926f9fd603.tar.xz project2-894b12fe1fffac3a7439f2fe33a10f926f9fd603.zip |
Add support for using boost::format for log messages
Fix initialisation of console logger
-rw-r--r-- | project2/common/appEngine.cpp | 2 | ||||
-rw-r--r-- | project2/common/cache.cpp | 4 | ||||
-rw-r--r-- | project2/common/environment.cpp | 2 | ||||
-rw-r--r-- | project2/common/logger.cpp | 6 | ||||
-rw-r--r-- | project2/common/logger.h | 13 | ||||
-rw-r--r-- | project2/common/rowProcessor.cpp | 4 | ||||
-rw-r--r-- | project2/console/consoleAppEngine.cpp | 2 | ||||
-rw-r--r-- | project2/console/p2consoleMain.cpp | 2 | ||||
-rw-r--r-- | project2/files/fsRows.cpp | 2 | ||||
-rw-r--r-- | project2/xml/xmlCache.cpp | 2 | ||||
-rw-r--r-- | project2/xml/xmlDocumentCache.cpp | 6 |
11 files changed, 32 insertions, 13 deletions
diff --git a/project2/common/appEngine.cpp b/project2/common/appEngine.cpp index 4d0e9bc..c6d3e92 100644 --- a/project2/common/appEngine.cpp +++ b/project2/common/appEngine.cpp @@ -24,7 +24,7 @@ void ApplicationEngine::logMessage(bool writeLog, const Glib::ustring & g, const Glib::ustring & m) { if (writeLog) { - Logger()->messagef(LOG_NOTICE, "%s: %s: %s", __PRETTY_FUNCTION__, g.c_str(), m.c_str()); + Logger()->messagebf(LOG_NOTICE, "%s: %s: %s", __PRETTY_FUNCTION__, g, m); } appMessages.push_back(new Message(g, m)); } diff --git a/project2/common/cache.cpp b/project2/common/cache.cpp index ff9f41f..9be5845 100644 --- a/project2/common/cache.cpp +++ b/project2/common/cache.cpp @@ -18,12 +18,12 @@ bool Cache::checkAndExecute(const Glib::ustring & n, const Glib::ustring & f, co RowSetCPtr cached = getCachedRowSet(n, f, rp); if (cached) { try { - Logger()->messagef(LOG_DEBUG, "Executing from cache '%s'", name.c_str()); + Logger()->messagebf(LOG_DEBUG, "Executing from cache '%s'", name); cached->execute(f, rp); return true; } catch (const Glib::Exception & e) { - Logger()->messagef(LOG_WARNING, "Cache failed (%s)", e.what().c_str()); + Logger()->messagebf(LOG_WARNING, "Cache failed (%s)", e.what()); } catch (const std::exception & e) { Logger()->messagef(LOG_WARNING, "Cache failed (%s)", e.what()); diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp index 811ab22..6010860 100644 --- a/project2/common/environment.cpp +++ b/project2/common/environment.cpp @@ -86,7 +86,7 @@ Environment::init() } Logger()->clear(); if (clLevel >= 0) { - Logger()->addLogger(LogDriverLoader::createNew("console", slLevel, std::string())); + Logger()->addLogger(LogDriverLoader::createNew("console", clLevel, std::string())); } if (slLevel >= 0) { Logger()->addLogger(LogDriverLoader::createNew("syslog", slLevel, getScriptName())); diff --git a/project2/common/logger.cpp b/project2/common/logger.cpp index 2c17dbf..dc02afc 100644 --- a/project2/common/logger.cpp +++ b/project2/common/logger.cpp @@ -56,6 +56,12 @@ Log::message(int priority, const char * msg) const } void +Log::messagebf(int priority, const boost::format & f) const +{ + message(priority, f.str().c_str()); +} + +void Log::messagef(int priority, const char * msgfmt, ...) const { if (priority > lowestLevel) return; diff --git a/project2/common/logger.h b/project2/common/logger.h index 5c91936..c333123 100644 --- a/project2/common/logger.h +++ b/project2/common/logger.h @@ -5,6 +5,7 @@ #include <stdarg.h> #include <syslog.h> // Pulled in for easy client lookups of LOG_* priorties #include <boost/intrusive_ptr.hpp> +#include <boost/format.hpp> #include "intrusivePtrBase.h" #include "scriptLoader.h" @@ -32,11 +33,23 @@ class Log { void message(int priority, const char * msg) const; void messagef(int priority, const char * msgfmt, ...) const __attribute__ ((format (printf, 3, 4))); + template <typename... Args> + void messagebf(int priority, const char * msgfmt, const Args & ... args) const { + if (priority > lowestLevel) return; + boost::format fmt(msgfmt); + messagebf(priority, fmt, args...); + } void vmessagef(int priority, const char * msgfmt, va_list) const; static const char * priorityName(int priority); // Look up the priority as defined in syslog.h private: + template <typename Arg, typename... OtherArgs> + void messagebf(int priority, boost::format & f, const Arg & arg, const OtherArgs & ... otherargs) const { + f % arg; + messagebf(priority, f, otherargs...); + } + void messagebf(int priority, const boost::format & f) const; LogDrivers logs; int lowestLevel; int nextId; diff --git a/project2/common/rowProcessor.cpp b/project2/common/rowProcessor.cpp index a52efc5..541ff11 100644 --- a/project2/common/rowProcessor.cpp +++ b/project2/common/rowProcessor.cpp @@ -41,13 +41,13 @@ RowProcessor::execute() const tc.insert(TargetCachePtr(new TargetCache(p, c))); } } - Logger()->messagef(LOG_DEBUG, "Executing from source '%s'", source->name.c_str()); + Logger()->messagebf(LOG_DEBUG, "Executing from source '%s'", source->name); if (IRSE) { try { source->execute(filter, this); } catch (const std::exception & e) { - Logger()->messagef(LOG_WARNING, "Source '%s' failed with '%s'", source->name.c_str(), e.what()); + Logger()->messagebf(LOG_WARNING, "Source '%s' failed with '%s'", source->name, e.what()); } } else { diff --git a/project2/console/consoleAppEngine.cpp b/project2/console/consoleAppEngine.cpp index a546aca..9179c64 100644 --- a/project2/console/consoleAppEngine.cpp +++ b/project2/console/consoleAppEngine.cpp @@ -44,7 +44,7 @@ ConsoleApplicationEngine::process() const } catch (const Glib::Exception & e) { char * buf = __cxxabiv1::__cxa_demangle(typeid(e).name(), NULL, NULL, NULL); - Logger()->messagef(LOG_ERR, "%s: Script errored: %s: %s", __FUNCTION__, buf, e.what().c_str()); + Logger()->messagebf(LOG_ERR, "%s: Script errored: %s: %s", __FUNCTION__, buf, e.what()); free(buf); } } diff --git a/project2/console/p2consoleMain.cpp b/project2/console/p2consoleMain.cpp index 9b3ceec..f0f82d4 100644 --- a/project2/console/p2consoleMain.cpp +++ b/project2/console/p2consoleMain.cpp @@ -14,7 +14,7 @@ main(int argc, char ** argv) env.init(); BOOST_FOREACH(const ConsoleEnvironment::ToDo & todo, env.todoList()) { LoaderBase::onAllComponents(boost::bind(&ComponentLoader::onBefore, _1)); - Logger()->messagef(LOG_DEBUG, "%s: Beginning script '%s/%s'", __FUNCTION__, todo.get<0>().c_str(), todo.get<1>().c_str()); + Logger()->messagebf(LOG_DEBUG, "%s: Beginning script '%s/%s'", __FUNCTION__, todo.get<0>(), todo.get<1>()); boost::intrusive_ptr<ConsoleApplicationEngine> app(new ConsoleApplicationEngine(&env, env.resolveScript(todo.get<0>(), todo.get<1>(), false))); Logger()->messagef(LOG_DEBUG, "%s: Processing file", __FUNCTION__); diff --git a/project2/files/fsRows.cpp b/project2/files/fsRows.cpp index 72a2055..6fa9fef 100644 --- a/project2/files/fsRows.cpp +++ b/project2/files/fsRows.cpp @@ -115,7 +115,7 @@ FsRows::execute(SearchState & ss, const Path & dir, const RowProcessor * rp) con if (!ignoreErrors()) { throw; } - Logger()->messagef(LOG_WARNING, "%s when processing '%s'", e.what(), dir.string().c_str()); + Logger()->messagebf(LOG_WARNING, "%s when processing '%s'", e.what(), dir); } ss.depth -= 1; } diff --git a/project2/xml/xmlCache.cpp b/project2/xml/xmlCache.cpp index abe56ab..c3a8cc7 100644 --- a/project2/xml/xmlCache.cpp +++ b/project2/xml/xmlCache.cpp @@ -49,7 +49,7 @@ class XmlCache : public Cache { writeTo.reset(); } catch (...) { - Logger()->messagef(LOG_WARNING, "Failed to save cache (%s)", target.string().c_str()); + Logger()->messagebf(LOG_WARNING, "Failed to save cache (%s)", target); } } diff --git a/project2/xml/xmlDocumentCache.cpp b/project2/xml/xmlDocumentCache.cpp index a26b526..73d07bc 100644 --- a/project2/xml/xmlDocumentCache.cpp +++ b/project2/xml/xmlDocumentCache.cpp @@ -46,17 +46,17 @@ class XmlDocumentCachePopulator : public CurlCompleteCallback { htmlReadMemory(buf.c_str(), buf.length(), url.c_str(), encoding, flags) : xmlReadMemory(buf.c_str(), buf.length(), url.c_str(), encoding, flags); if (!doc) { - Logger()->messagef(LOG_DEBUG, "Download of '%s' succeeded, but parsing failed with error '%s'", url.c_str(), xmlGetLastError()->message); + Logger()->messagebf(LOG_DEBUG, "Download of '%s' succeeded, but parsing failed with error '%s'", url, xmlGetLastError()->message); XmlDocumentCache::documents.insert(XmlDocumentCache::Documents::value_type(url, boost::bind(helperThrow<XmlParseError>, std::string(xmlGetLastError()->message)))); } XmlDocumentCache::documents.insert(XmlDocumentCache::Documents::value_type(url, boost::bind(helperReturnDocument, XmlDocumentCache::DocumentPtr(doc, xmlFreeDoc)))); - Logger()->messagef(LOG_DEBUG, "Download of '%s' completed, stored", url.c_str()); + Logger()->messagebf(LOG_DEBUG, "Download of '%s' completed, stored", url); } void error(CurlBulkFetcher *, const char * error) { - Logger()->messagef(LOG_DEBUG, "Download of '%s' failed with error '%s'", url.c_str(), error); + Logger()->messagebf(LOG_DEBUG, "Download of '%s' failed with error '%s'", url, error); XmlDocumentCache::documents.insert(XmlDocumentCache::Documents::value_type(url, boost::bind(helperThrow<DownloadFailed>, std::string(error)))); } |