[dss-commits] r8801 - dss/trunk/core
dss-commits at forum.digitalstrom.org
dss-commits at forum.digitalstrom.org
Mon Sep 28 13:16:35 CEST 2009
Author: jwinkelmann
Date: 2009-09-28 13:16:35 +0200 (Mon, 28 Sep 2009)
New Revision: 8801
Modified:
dss/trunk/core/dss.cpp
dss/trunk/core/dss.h
dss/trunk/core/logger.cpp
dss/trunk/core/logger.h
Log:
logging: introduce possiblity to log to file; move versionString to dss
Modified: dss/trunk/core/dss.cpp
===================================================================
--- dss/trunk/core/dss.cpp 2009-09-28 11:14:27 UTC (rev 8800)
+++ dss/trunk/core/dss.cpp 2009-09-28 11:16:35 UTC (rev 8801)
@@ -23,6 +23,11 @@
#include "config.h"
#endif
+#ifdef HAVE_BUILD_INFO_H
+ #include "build_info.h"
+#endif
+
+
#include "dss.h"
#include "logger.h"
#include "xmlwrapper.h"
@@ -49,6 +54,7 @@
#include <cassert>
#include <string>
+#include <sstream>
using namespace std;
@@ -218,13 +224,33 @@
}
void DSS::run() {
- Logger::getInstance()->log("DSS stating up....", lsInfo);
+ Logger::getInstance()->log("DSS starting up....", lsInfo);
if(!loadConfig()) {
Logger::getInstance()->log("Could not parse config file", lsFatal);
return;
}
+
+ // see whether we have a log file set in config.xml, and set the
+ // log target accordingly
+ PropertyNodePtr pNode = getPropertySystem().getProperty("/config/logfile");
+ if (pNode) {
+ std::string logFileName = pNode->getStringValue();
+ Logger::getInstance()->log("Logging to file: " + logFileName, lsInfo);
+
+ boost::shared_ptr<dss::LogTarget>
+ logTarget(new dss::FileLogTarget(logFileName));
+ if (!dss::Logger::getInstance()->setLogTarget(logTarget)) {
+ Logger::getInstance()->log("Failed to open logfile '" + logFileName +
+ "'; exiting", lsFatal);
+ return;
+ }
+ } else {
+ Logger::getInstance()->log("No logfile configured, logging to stdout",
+ lsInfo);
+ }
+
+ dss::Logger::getInstance()->log(versionString(), lsInfo);
-
SensornestServer server;
server.run();
@@ -260,4 +286,19 @@
return getPropertySystem().loadFromXML(getDataDirectory() + "config.xml", getPropertySystem().getProperty("/config"));
} // loadConfig
+
+ std::string DSS::versionString() {
+ std::ostringstream ostr;
+ ostr << "DSS";
+#ifdef HAVE_CONFIG_H
+ ostr << " v" << DSS_VERSION;
+#endif
+#ifdef HAVE_BUILD_INFO_H
+ ostr << " (r" << DSS_RCS_REVISION << ")"
+ << " (" << DSS_BUILD_USER << "@" << DSS_BUILD_HOST << ")"
+ << " " << DSS_BUILD_DATE;
+#endif
+ return ostr.str();
+ }
+
}
Modified: dss/trunk/core/dss.h
===================================================================
--- dss/trunk/core/dss.h 2009-09-28 11:14:27 UTC (rev 8800)
+++ dss/trunk/core/dss.h 2009-09-28 11:16:35 UTC (rev 8801)
@@ -102,6 +102,7 @@
#ifdef WITH_TESTS
static void teardown();
#endif
+ static std::string versionString();
aDSSState getState() const { return m_State; }
Modified: dss/trunk/core/logger.cpp
===================================================================
--- dss/trunk/core/logger.cpp 2009-09-28 11:14:27 UTC (rev 8800)
+++ dss/trunk/core/logger.cpp 2009-09-28 11:16:35 UTC (rev 8801)
@@ -80,6 +80,9 @@
return wstring(SeverityToString<const wchar_t*>(_severity));
} // severityToString<const wstring>
+ Logger::Logger()
+ : m_logTarget(boost::shared_ptr<LogTarget>(new CoutLogTarget()))
+ {}
Logger* Logger::getInstance() {
if(m_Instance == NULL) {
@@ -105,7 +108,9 @@
#else
localtime_r( &now, &t );
#endif
- cout << "[" << dateToISOString<string>(&t) << "]" << SeverityToString<const string>(_severity) << " " << _message << endl;
+ m_logTarget->outputStream() << "[" << dateToISOString<string>(&t) << "]"
+ << SeverityToString<const string>(_severity) << " "
+ << _message << endl;
} // log
void Logger::log(const char* _message, const aLogSeverity _severity) {
@@ -118,4 +123,15 @@
}
} // log
+
+ bool Logger::setLogTarget(boost::shared_ptr<LogTarget>& _logTarget) {
+ if (_logTarget->open()) {
+ m_logTarget->close();
+ m_logTarget = _logTarget;
+ return true;
+ }
+ return false;
+ } // setLogTarget
+
+
}
Modified: dss/trunk/core/logger.h
===================================================================
--- dss/trunk/core/logger.h 2009-09-28 11:14:27 UTC (rev 8800)
+++ dss/trunk/core/logger.h 2009-09-28 11:16:35 UTC (rev 8801)
@@ -23,6 +23,9 @@
#define LOGGER_H_INLUDED
#include <string>
+#include <ostream>
+#include <fstream>
+#include <iostream>
#include <boost/shared_ptr.hpp>
typedef enum {
@@ -35,13 +38,10 @@
namespace dss {
- class LogChannel;
+class LogChannel;
+class LogTarget;
class Logger {
- private:
- static Logger* m_Instance;
-
- Logger() {}
public:
static Logger* getInstance();
static void shutdown();
@@ -50,6 +50,16 @@
void log(const char* _message, const aLogSeverity _severity = lsDebug);
void log(const LogChannel& _channel, const std::string& _message, const aLogSeverity _severity = lsDebug);
+
+ bool setLogTarget(boost::shared_ptr<LogTarget>& _logTarget);
+
+ private:
+ Logger();
+
+ static Logger* m_Instance;
+
+ boost::shared_ptr<LogTarget> m_logTarget;
+
}; // Logger
class LogChannel {
@@ -74,6 +84,44 @@
}; // LogChannel
+class LogTarget {
+public:
+ virtual bool open() = 0;
+ virtual void close() = 0;
+ virtual std::ostream& outputStream() = 0;
+}; // LogTarget
+
+class CoutLogTarget : public LogTarget {
+public:
+ virtual bool open() { return true; }
+ virtual void close() {}
+ std::ostream& outputStream() { return std::cout; }
+}; // CoutLogTarget
+
+class FileLogTarget : public LogTarget {
+public:
+ FileLogTarget(const std::string& _fileName) : m_fileName(_fileName) {}
+ virtual bool open() {
+ m_fileOutputStream.open(m_fileName.c_str(), std::ios::out|std::ios::app);
+ m_fileOpen = true;
+ m_fileOutputStream << "-- Log opened" << std::endl;
+ return m_fileOutputStream.good();
+ }
+ virtual void close() {
+ if (m_fileOpen) {
+ m_fileOutputStream << "-- Log closed" << std::endl;
+ m_fileOutputStream. close();
+ m_fileOpen = false;
+ }
+ }
+ std::ostream& outputStream() { return m_fileOutputStream; }
+
+private:
+ std::string m_fileName;
+ bool m_fileOpen;
+ std::ofstream m_fileOutputStream;
+}; // FileLogTarget
+
}
#endif
More information about the dss-commits
mailing list