summaryrefslogtreecommitdiff
path: root/src/utils/logger.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/logger.cpp')
-rw-r--r--src/utils/logger.cpp277
1 files changed, 59 insertions, 218 deletions
diff --git a/src/utils/logger.cpp b/src/utils/logger.cpp
index 36d43f80..f7d26059 100644
--- a/src/utils/logger.cpp
+++ b/src/utils/logger.cpp
@@ -23,6 +23,7 @@
#include "logger.h"
#include <ctime>
+#include <fstream>
#include <iomanip>
#include <iostream>
@@ -33,257 +34,97 @@
namespace utils
{
+static std::ofstream mLogFile; /**< Log file. */
+bool Logger::mHasTimestamp = true; /**< Timestamp flag. */
+bool Logger::mTeeMode = false; /**< Tee mode flag. */
+Logger::Level Logger::mVerbosity = Logger::INFO; /**< Verbosity level. */
/**
- * Default constructor.
- */
-Logger::Logger(void)
- throw()
- : mHasTimestamp(true),
- mTeeMode(false),
- mVerbosity(0)
+ * Gets the current time.
+ *
+ * @return the current time as string.
+ */
+static std::string getCurrentTime()
{
- // NOOP
-}
-
-
-/**
- * Destructor.
- */
-Logger::~Logger(void)
- throw()
-{
- // the destructor of std::ofstream takes care of closing the file
- // if it is still open :)
-}
-
-
-/**
- * Set the log file.
- */
-void
-Logger::setLogFile(const std::string& logFile)
-{
- // close the current log file.
- if (mLogFile.is_open()) {
- mLogFile.close();
- }
-
- // open the file for output and remove the former file contents.
- mLogFile.open(logFile.c_str(), std::ios::trunc);
-
- if (!mLogFile.is_open()) {
- std::string msg("unable to open ");
- msg += logFile;
- msg += " for writing.";
-
- throw std::ios::failure(msg);
- }
- else {
- // by default the streams do not throw any exception
- // let std::ios::failbit and std::ios::badbit throw exceptions.
- mLogFile.exceptions(std::ios::failbit | std::ios::badbit);
- }
-}
+ time_t now;
+ tm local;
+ // get current time_t value
+ time(&now);
-/**
- * Add/remove the timestamp.
- */
-void
-Logger::setTimestamp(bool flag)
- throw()
-{
- mHasTimestamp = flag;
-}
+ // convert time_t to tm struct to break the time into individual
+ // constituents
+ local = *(localtime(&now));
+ // stringify the time, the format is: [hh:mm:ss]
+ using namespace std;
+ ostringstream os;
+ os << "[" << setw(2) << setfill('0') << local.tm_hour
+ << ":" << setw(2) << setfill('0') << local.tm_min
+ << ":" << setw(2) << setfill('0') << local.tm_sec
+ << "]";
-/**
- * Set tee mode.
- */
-void
-Logger::setTeeMode(bool flag)
- throw()
-{
- mTeeMode = flag;
+ return os.str();
}
-
-/**
- * Log a generic message.
- */
-void
-Logger::log(const std::string& msg, unsigned short atVerbosity)
+void Logger::output(std::ostream &os, std::string const &msg, char const *prefix)
{
- if ( mVerbosity >= atVerbosity )
+ if (mHasTimestamp)
{
- if (mTeeMode) {
- log(std::cout, msg);
-
- if (mLogFile.is_open()) {
- log(mLogFile, msg);
- }
- }
- else {
- log((mLogFile.is_open() ? mLogFile : std::cout), msg);
- }
+ os << getCurrentTime() << ' ';
}
-}
-
-/**
- * Log a debug message.
- */
-void
-Logger::debug(const std::string& msg, unsigned short atVerbosity)
-{
- if ( mVerbosity >= atVerbosity )
+ if (prefix)
{
- if (mTeeMode) {
- log(std::cout, msg, "[DBG]");
-
- if (mLogFile.is_open()) {
- log(mLogFile, msg, "[DBG]");
- }
- }
- else {
- log((mLogFile.is_open() ? mLogFile : std::cout), msg, "[DBG]");
- }
+ os << prefix << ' ';
}
-}
+ os << msg << std::endl;
+}
-/**
- * Log an info message.
- */
-void
-Logger::info(const std::string& msg, unsigned short atVerbosity)
+void Logger::setLogFile(std::string const &logFile)
{
- if ( mVerbosity >= atVerbosity )
+ // Close the current log file.
+ if (mLogFile.is_open())
{
- if (mTeeMode) {
- log(std::cout, msg, "[INF]");
-
- if (mLogFile.is_open()) {
- log(mLogFile, msg, "[INF]");
- }
- }
- else {
- log((mLogFile.is_open() ? mLogFile : std::cout), msg, "[INF]");
- }
+ mLogFile.close();
}
-}
+ // Open the file for output and remove the former file contents.
+ mLogFile.open(logFile.c_str(), std::ios::trunc);
-/**
- * Log a warn message.
- */
-void
-Logger::warn(const std::string& msg, unsigned short atVerbosity)
-{
- if ( mVerbosity >= atVerbosity )
+ if (!mLogFile.is_open())
{
- if (mTeeMode) {
- log(std::cerr, msg, "[WRN]");
-
- if (mLogFile.is_open()) {
- log(mLogFile, msg, "[WRN]");
- }
- }
- else {
- log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[WRN]");
- }
+ throw std::ios::failure("unable to open " + logFile + "for writing");
}
-}
-
-
-/**
- * Log an error message.
- */
-void
-Logger::error(const std::string& msg, unsigned short atVerbosity)
-{
- if ( mVerbosity >= atVerbosity )
+ else
{
- if (mTeeMode) {
- log(std::cerr, msg, "[ERR]");
-
- if (mLogFile.is_open()) {
- log(mLogFile, msg, "[ERR]");
- }
- }
- else {
- log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[ERR]");
- }
+ // by default the streams do not throw any exception
+ // let std::ios::failbit and std::ios::badbit throw exceptions.
+ mLogFile.exceptions(std::ios::failbit | std::ios::badbit);
}
}
-
-/**
- * Log a fatal error message.
- */
-void
-Logger::fatal(const std::string& msg, unsigned short atVerbosity)
+void Logger::output(std::string const& msg, Level atVerbosity)
{
- if (mTeeMode) {
- log(std::cerr, msg, "[FTL]");
-
- if (mLogFile.is_open()) {
- log(mLogFile, msg, "[FTL]");
- }
- }
- else {
- log((mLogFile.is_open() ? mLogFile : std::cerr), msg, "[FTL]");
- }
-}
+ static char const *prefixes[] =
+ { "[FTL]", "[ERR]", "[WRN]", "[INF]", "[DBG]" };
+ if (mVerbosity >= atVerbosity)
+ {
+ bool open = mLogFile.is_open();
-/**
- * Log a generic message.
- */
-void
-Logger::log(std::ostream& os,
- const std::string& msg,
- const std::string& prefix)
-{
- if (mHasTimestamp) {
- os << getCurrentTime() << " ";
- }
+ if (open)
+ {
+ output(mLogFile, msg, prefixes[atVerbosity]);
+ }
- if (prefix != "") {
- os << prefix << " ";
+ if (!open || mTeeMode)
+ {
+ output(atVerbosity <= WARN ? std::cerr : std::cout,
+ msg, prefixes[atVerbosity]);
+ }
}
-
- os << msg << std::endl;
}
-
-/**
- * Get the current time.
- */
-std::string
-Logger::getCurrentTime(void)
-{
- time_t now;
- tm local;
-
- // get current time_t value
- time(&now);
-
- // convert time_t to tm struct to break the time into individual
- // constituents
- local = *(localtime(&now));
-
- // stringify the time, the format is: [hh:mm:ss]
- using namespace std;
- ostringstream os;
- os << "[" << setw(2) << setfill('0') << local.tm_hour
- << ":" << setw(2) << setfill('0') << local.tm_min
- << ":" << setw(2) << setfill('0') << local.tm_sec
- << "]";
-
- return os.str();
-}
-
-
} // namespace utils