summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--project2/common/consoleLog.cpp20
-rw-r--r--project2/common/environment.cpp6
-rw-r--r--project2/common/logger.cpp89
-rw-r--r--project2/common/logger.h14
-rw-r--r--project2/common/loggers.h60
-rw-r--r--project2/common/scriptLoader.h4
-rw-r--r--project2/common/syslogLog.cpp24
-rw-r--r--project2/files/fileLog.cpp39
8 files changed, 103 insertions, 153 deletions
diff --git a/project2/common/consoleLog.cpp b/project2/common/consoleLog.cpp
new file mode 100644
index 0000000..9c5c2d8
--- /dev/null
+++ b/project2/common/consoleLog.cpp
@@ -0,0 +1,20 @@
+#include "logger.h"
+
+/// Logger that writes to the console
+class ConsoleLogDriver : public LogDriverBase {
+ public:
+ ConsoleLogDriver(int level, const std::string &) :
+ LogDriverBase(level)
+ {
+ }
+ void message(int priority, const char * msg) const
+ {
+ if (priority <= level) {
+ fprintf(stderr, "%s\n", msg);
+ fflush(stderr);
+ }
+ }
+};
+
+DECLARE_GENERIC_LOADER("console", LogDriverLoader, ConsoleLogDriver);
+
diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp
index 9d79ca0..811ab22 100644
--- a/project2/common/environment.cpp
+++ b/project2/common/environment.cpp
@@ -1,7 +1,7 @@
#include <pch.hpp>
#include "environment.h"
#include "optionsSource.h"
-#include "loggers.h"
+#include "logger.h"
#include "scriptLoader.h"
#include <stdio.h>
#include <fstream>
@@ -86,10 +86,10 @@ Environment::init()
}
Logger()->clear();
if (clLevel >= 0) {
- Logger()->addLogger(new ConsoleLogDriver(stderr, clLevel, false));
+ Logger()->addLogger(LogDriverLoader::createNew("console", slLevel, std::string()));
}
if (slLevel >= 0) {
- Logger()->addLogger(new SyslogLogDriver(getScriptName().c_str(), slLevel));
+ Logger()->addLogger(LogDriverLoader::createNew("syslog", slLevel, getScriptName()));
}
Logger()->message(LOG_DEBUG, "Loaded configuration");
}
diff --git a/project2/common/logger.cpp b/project2/common/logger.cpp
index b43efa8..2c17dbf 100644
--- a/project2/common/logger.cpp
+++ b/project2/common/logger.cpp
@@ -2,12 +2,8 @@
#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;
@@ -96,88 +92,3 @@ 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);
- }
-}
-
diff --git a/project2/common/logger.h b/project2/common/logger.h
index 5d42631..5c91936 100644
--- a/project2/common/logger.h
+++ b/project2/common/logger.h
@@ -6,8 +6,18 @@
#include <syslog.h> // Pulled in for easy client lookups of LOG_* priorties
#include <boost/intrusive_ptr.hpp>
#include "intrusivePtrBase.h"
+#include "scriptLoader.h"
+
+/// Base class for classes providing a logging facility
+class LogDriverBase : public virtual IntrusivePtrBase {
+ public:
+ LogDriverBase(int level);
+ virtual ~LogDriverBase();
+
+ virtual void message(int priority, const char * msg) const = 0;
+ const int level;
+};
-class LogDriverBase;
class Log {
public:
@@ -39,5 +49,7 @@ class Logger {
static Log log;
};
+typedef GenLoader<LogDriverBase, int, std::string> LogDriverLoader;
+
#endif
diff --git a/project2/common/loggers.h b/project2/common/loggers.h
deleted file mode 100644
index 69b352c..0000000
--- a/project2/common/loggers.h
+++ /dev/null
@@ -1,60 +0,0 @@
-#ifndef LOGGERS_H
-#define LOGGERS_H
-
-#include "logger.h"
-
-/// Base class for classes providing a logging facility
-class LogDriverBase : public virtual IntrusivePtrBase {
- public:
- LogDriverBase(int level);
- virtual ~LogDriverBase();
-
- virtual void message(int priority, const char * msg) const = 0;
- const int level;
-};
-
-/// Base class for loggers that write to some sort of file handle
-class FileBasedLogDriver : public LogDriverBase {
- public:
- FileBasedLogDriver(FILE *, int level, bool timestamp);
- virtual ~FileBasedLogDriver() = 0;
-
- virtual void message(int priority, const char * msg) const;
-
- protected:
- void writeTimestamp() const;
- void writeLevel(int level) const;
- FILE * file;
- bool timestamp;
-
- private:
- const char * timeStr() const;
- mutable char tmbuf[30];
-};
-
-/// Logger that writes to the console
-class ConsoleLogDriver : public FileBasedLogDriver {
- public:
- ConsoleLogDriver(FILE *, int level, bool timestamp);
- ~ConsoleLogDriver();
-
-};
-
-/// Logger that writes to a file
-class FileLogDriver : public FileBasedLogDriver {
- public:
- FileLogDriver(const char * path, int level);
- ~FileLogDriver();
-};
-
-/// Logger that writes to syslog
-class SyslogLogDriver : public LogDriverBase {
- public:
- SyslogLogDriver(const char * ident, int level, int option = 0, int facility = LOG_USER);
- virtual ~SyslogLogDriver();
-
- virtual void message(int priority, const char * msg) const;
-};
-
-#endif
-
diff --git a/project2/common/scriptLoader.h b/project2/common/scriptLoader.h
index ccc7afb..776d0c5 100644
--- a/project2/common/scriptLoader.h
+++ b/project2/common/scriptLoader.h
@@ -140,6 +140,10 @@ class GenLoader : public ComponentLoader {
}
};
virtual Impl * create(const Params & ...) const = 0;
+ inline static Impl * createNew(const std::string & n, const Params & ... p)
+ {
+ return LoaderBase::getLoader<GenLoader<Impl, Params...>, NotSupported>(n)->create(p...);
+ }
inline static boost::shared_ptr<GenLoader<Impl, Params...>> getFor(const std::string & n)
{
return LoaderBase::getLoader<GenLoader<Impl, Params...>, NotSupported>(n);
diff --git a/project2/common/syslogLog.cpp b/project2/common/syslogLog.cpp
new file mode 100644
index 0000000..3c8bbb0
--- /dev/null
+++ b/project2/common/syslogLog.cpp
@@ -0,0 +1,24 @@
+#include "logger.h"
+
+/// Logger that writes to syslog
+class SyslogLogDriver : public LogDriverBase {
+ public:
+ SyslogLogDriver(int level, const std::string & ident) :
+ LogDriverBase(level)
+ {
+ openlog(ident.c_str(), 0, LOG_USER);
+ }
+ ~SyslogLogDriver()
+ {
+ closelog();
+ }
+ void message(int priority, const char * msg) const
+ {
+ if (priority <= level) {
+ syslog(priority, "%s", msg);
+ }
+ }
+};
+
+DECLARE_GENERIC_LOADER("syslog", LogDriverLoader, SyslogLogDriver);
+
diff --git a/project2/files/fileLog.cpp b/project2/files/fileLog.cpp
new file mode 100644
index 0000000..9225146
--- /dev/null
+++ b/project2/files/fileLog.cpp
@@ -0,0 +1,39 @@
+#include "logger.h"
+
+/// Logger that writes to a file
+class FileLogDriver : public LogDriverBase {
+ public:
+ FileLogDriver(int level, const std::string & path) :
+ LogDriverBase(level),
+ file(fopen(path.c_str(), "a"))
+ {
+ }
+ ~FileLogDriver()
+ {
+ fclose(file);
+ }
+ void message(int priority, const char * msg) const
+ {
+ if (priority <= level) {
+ fprintf(file, "%s %-6.*s%s\n",
+ timeStr(), 5, Log::priorityName(level), msg);
+ fflush(file);
+ }
+ }
+
+ private:
+ FILE * file;
+
+ const char * timeStr() const
+ {
+ struct tm tm;
+ time_t t = time(NULL);
+ localtime_r(&t, &tm);
+ strftime(tmbuf, sizeof(tmbuf), "%F %T", &tm);
+ return tmbuf;
+ }
+ mutable char tmbuf[30];
+};
+
+DECLARE_GENERIC_LOADER("file", LogDriverLoader, FileLogDriver);
+