summaryrefslogtreecommitdiff
path: root/project2/common/logger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'project2/common/logger.cpp')
-rw-r--r--project2/common/logger.cpp182
1 files changed, 182 insertions, 0 deletions
diff --git a/project2/common/logger.cpp b/project2/common/logger.cpp
new file mode 100644
index 0000000..6133ce9
--- /dev/null
+++ b/project2/common/logger.cpp
@@ -0,0 +1,182 @@
+#define SYSLOG_NAMES 1 // Enables the definition of names in syslog.h
+
+#include "logger.h"
+#include "loggers.h"
+#include <stdio.h>
+#include <boost/foreach.hpp>
+#include <boost/algorithm/string/case_conv.hpp>
+
+const char * const version = "$Id: logger.cpp 8818 2011-01-10 10:09:59Z danielg $";
+
+Log Logger::log;
+
+Log::Log() :
+ lowestLevel(-1)
+{
+}
+
+Log::~Log()
+{
+ logs.clear();
+}
+
+int
+Log::addLogger(LogDriverBasePtr log)
+{
+ logs[++nextId] = log;
+ if (log->level > lowestLevel) {
+ lowestLevel = log->level;
+ }
+ return nextId;
+}
+
+void
+Log::clear()
+{
+ logs.clear();
+}
+
+const char *
+Log::priorityName(int priority)
+{
+ const char * name = NULL;
+ for (CODE * c = prioritynames; c->c_name; c++) {
+ if (c->c_val == priority) {
+ name = c->c_name;
+ }
+ }
+ return name;
+}
+
+
+void
+Log::message(int priority, const char * msg) const
+{
+ if (priority > lowestLevel) return;
+ BOOST_FOREACH(const LogDrivers::value_type & l, logs) {
+ l.second->message(priority, msg);
+ }
+}
+
+void
+Log::messagef(int priority, const char * msgfmt, ...) const
+{
+ if (priority > lowestLevel) return;
+ va_list v;
+ va_start(v, msgfmt);
+ vmessagef(priority, msgfmt, v);
+ va_end(v);
+}
+
+void
+Log::vmessagef(int priority, const char * msgfmt, va_list va) const
+{
+ if (priority > lowestLevel) return;
+ char * msg;
+ int len = vasprintf(&msg, msgfmt, va);
+ if (len > 0) {
+ message(priority, msg);
+ }
+ free(msg);
+}
+
+Log *
+Logger::operator->() const
+{
+ return &log;
+}
+
+LogDriverBase::LogDriverBase(int l) :
+ level(l)
+{
+}
+
+LogDriverBase::~LogDriverBase()
+{
+}
+
+// File based log driver
+//----------------------
+FileBasedLogDriver::FileBasedLogDriver(FILE * f, int l, bool ts) :
+ LogDriverBase(l),
+ file(f),
+ timestamp(ts)
+{
+}
+FileBasedLogDriver::~FileBasedLogDriver()
+{
+}
+void
+FileBasedLogDriver::message(int priority, const char * msg) const
+{
+ if (priority <= level) {
+ writeTimestamp();
+ writeLevel(priority);
+ fprintf(file, "%s\n", msg);
+ fflush(file);
+ }
+}
+const char *
+FileBasedLogDriver::timeStr() const
+{
+ struct tm tm;
+ time_t t = time(NULL);
+ localtime_r(&t, &tm);
+ strftime(tmbuf, sizeof(tmbuf), "%F %T", &tm);
+ return tmbuf;
+}
+void
+FileBasedLogDriver::writeTimestamp() const
+{
+ if (timestamp) {
+ fprintf(file, "%s ", timeStr());
+ }
+}
+void
+FileBasedLogDriver::writeLevel(int level) const
+{
+ if (timestamp) {
+ fprintf(file, "%-6.*s", 5, boost::algorithm::to_upper_copy(std::string(Log::priorityName(level))).c_str());
+ }
+}
+
+// Consol driver
+//-------------------
+ConsoleLogDriver::ConsoleLogDriver(FILE * f, int l, bool ts) :
+ FileBasedLogDriver(f, l, ts)
+{
+}
+ConsoleLogDriver::~ConsoleLogDriver()
+{
+}
+
+// File log driver
+//-------------------
+FileLogDriver::FileLogDriver(const char * path, int l) :
+ FileBasedLogDriver(fopen(path, "a"), l, true)
+{
+}
+FileLogDriver::~FileLogDriver()
+{
+ fclose(file);
+}
+
+// Syslog Log Driver
+//-------------------
+SyslogLogDriver::SyslogLogDriver(const char * ident, int level, int option, int facility) :
+ LogDriverBase(level)
+{
+ openlog(ident, option, facility);
+}
+SyslogLogDriver::~SyslogLogDriver()
+{
+ closelog();
+}
+void
+SyslogLogDriver::message(int priority, const char * msg) const
+{
+ if (priority <= level) {
+ syslog(priority, "%s", msg);
+ }
+}
+