diff options
author | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-11-23 01:39:42 +0100 |
---|---|---|
committer | Andreas Stöckel <astoecke@techfak.uni-bielefeld.de> | 2014-11-23 01:39:42 +0100 |
commit | aaaf493e3cddcc2cb0797ca3fe7eca4f12a04453 (patch) | |
tree | a260341b3c9d0839358876a2d053fb20d0f3f05e /src/core/Logger.cpp | |
parent | ec0039e4656d579d423dac3f5d626cb2deed05af (diff) |
imlemented Logger, TerminalLogger, OusiaException, LoggableException
Diffstat (limited to 'src/core/Logger.cpp')
-rw-r--r-- | src/core/Logger.cpp | 160 |
1 files changed, 126 insertions, 34 deletions
diff --git a/src/core/Logger.cpp b/src/core/Logger.cpp index 2317e70..1a3b6c6 100644 --- a/src/core/Logger.cpp +++ b/src/core/Logger.cpp @@ -16,54 +16,146 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <iostream> +#include <sstream> + +#include "Logger.hpp" + namespace ousia { -static const int BLACK = 30; -static const int RED = 31; -static const int GREEN = 32; -static const int YELLOW = 33; -static const int BLUE = 34; -static const int MAGENTA = 35; -static const int CYAN = 36; -static const int WHITE = 37; - -void StreamLogger::logMessage(const LogMessage &msg) { - os << '['; +/* Class Logger */ + +void Logger::log(Severity severity, const std::string &msg, + const std::string &file, int line, int column) +{ + // Copy the current severity level + if (static_cast<int>(severity) > static_cast<int>(maxEncounteredSeverity)) { + maxEncounteredSeverity = severity; + } + + // Call the actual log message function if the severity is larger or equal + // to the minimum severity + if (static_cast<int>(severity) >= static_cast<int>(minSeverity)) { + process(Message{severity, msg, file, line, column}); + } +} + +unsigned int Logger::pushFilename(const std::string &name) +{ + filenameStack.push(name); + return filenameStack.size(); +} + +unsigned int Logger::popFilename() +{ + filenameStack.pop(); + return filenameStack.size(); +} + +void Logger::unwindFilenameStack(unsigned int pos) +{ + while (filenameStack.size() > pos && !filenameStack.empty()) { + filenameStack.pop(); + } +} + +/* Class TerminalLogger */ + +/** + * Small class used internally for formated terminal output using ANSI/VT100 + * escape codes on supported terminals. + * + * TODO: Deactivate if using windows or use the corresponding API function. + */ +class Terminal { +private: + /** + * If set to false, no control codes are generated. + */ + bool active; + +public: + static const int BLACK = 30; + static const int RED = 31; + static const int GREEN = 32; + static const int YELLOW = 33; + static const int BLUE = 34; + static const int MAGENTA = 35; + static const int CYAN = 36; + static const int WHITE = 37; + + Terminal(bool active) : active(active) {} + + std::string color(int color, bool bright = true) const + { + if (!active) { + return std::string{}; + } + std::stringstream ss; + ss << "\x1b["; + if (bright) { + ss << "1;"; + } + ss << color << "m"; + return ss.str(); + } + + std::string reset() const + { + if (!active) { + return std::string{}; + } + return "\x1b[0m"; + } +}; + +void TerminalLogger::process(const Message &msg) +{ + Terminal t(useColor); + + // Print the file name + if (msg.hasFile()) { + os << t.color(Terminal::WHITE, true) << msg.file << t.reset(); + } + + // Print line and column number + if (msg.hasLine()) { + if (msg.hasFile()) { + os << ':'; + } + os << t.color(Terminal::WHITE, true) << msg.line + << t.reset(); + if (msg.hasColumn()) { + os << ':' << msg.column; + } + } + + // Print the optional seperator + if (msg.hasFile() || msg.hasLine()) { + os << ": "; + } + + // Print the severity switch (msg.severity) { case Severity::DEBUG: - os << "debug" << os; break; - case Severity::INFO: - os << "info" << os; + case Severity::NOTE: + os << t.color(Terminal::CYAN, true) << "note: "; break; case Severity::WARNING: - os << "warning" << os; + os << t.color(Terminal::MAGENTA, true) << "warning: "; break; case Severity::ERROR: - os << "error" << os; + os << t.color(Terminal::RED, true) << "error: "; break; case Severity::FATAL_ERROR: - is << "fatal error" << os; + os << t.color(Terminal::RED, true) << "error: "; break; } - os << ']'; - - // Print the file name - if (!msg.file.empty()) { - os << msg.file; - - // Print the line and column - if (msg.line >= 0) { - os << ':' << msg.line; - if (msg.column >= 0) { - os << ':' << msg.column; - } - } - } + os << t.reset(); // Print the actual message - os << ' ' << msg.msg; + os << msg.msg << std::endl; +} } - -}; |