utility: Added ConsoleLogger

Adds the class ConsoleLogger, which logs LogMessages to std::cout. The
Application class creates and registers a ConsoleLogger on creation.

Output format:
<time> | <filename>:<line> <function>() | <type>: <message>

Example:
12:58:18 | Application.cpp:21 create() | INFO: hello world!
12:58:18 | Application.cpp:22 create() | WARNING: attention world!
12:58:18 | Application.cpp:23 create() | ERROR: world failed

review=4
reviewer=stalli, malte
This commit is contained in:
Eberhard Graether
2014-04-09 14:43:48 +02:00
parent 6b02f6ff87
commit 9902288cab
6 changed files with 87 additions and 10 deletions
+8 -1
View File
@@ -1,4 +1,7 @@
#include "Application.h"
#include "Application.h"
#include "utility/logging/ConsoleLogger.h"
#include "utility/logging/LogManager.h"
std::shared_ptr<Application> Application::create(std::shared_ptr<GuiElementFactory> guiElementFactory)
{
@@ -8,6 +11,10 @@ std::shared_ptr<Application> Application::create(std::shared_ptr<GuiElementFacto
ptr->m_graphAccess = std::make_shared<GraphAccess>();
ptr->m_componentManager =
ComponentManager::create(ptr->m_viewManager, guiElementFactory, ptr->m_codeAccess, ptr->m_graphAccess);
std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>();
LogManager::getInstance()->addLogger(consoleLogger);
return ptr;
}
+5 -3
View File
@@ -54,17 +54,19 @@ add_files(
gui/GuiElement.h
gui/GuiElementFactory.cpp
gui/GuiElementFactory.h
utility/logging/ConsoleLogger.cpp
utility/logging/ConsoleLogger.h
utility/logging/Logger.cpp
utility/logging/Logger.h
utility/logging/logging.h
utility/logging/LogManager.cpp
utility/logging/LogManager.h
utility/logging/LogMessage.h
utility/ConfigManager.cpp
utility/ConfigManager.h
Application.cpp
Application.h
ApplicationSettings.cpp
+36
View File
@@ -0,0 +1,36 @@
#include "utility/logging/ConsoleLogger.h"
#include <iostream>
ConsoleLogger::ConsoleLogger()
: Logger("ConsoleLogger")
{
}
ConsoleLogger::~ConsoleLogger()
{
}
void ConsoleLogger::logInfo(const LogMessage& message)
{
logMessage("INFO", message);
}
void ConsoleLogger::logWarning(const LogMessage& message)
{
logMessage("WARNING", message);
}
void ConsoleLogger::logError(const LogMessage& message)
{
logMessage("ERROR", message);
}
void ConsoleLogger::logMessage(const std::string& type, const LogMessage& message)
{
std::cout
<< message.getTimeString("%H:%M:%S") << " | "
<< message.getFileName() << ':' << message.line << ' ' << message.functionName << "() | "
<< type << ": " << message.message
<< std::endl;
}
+21
View File
@@ -0,0 +1,21 @@
#ifndef CONSOLE_LOGGER_H
#define CONSOLE_LOGGER_H
#include "utility/logging/Logger.h"
#include "utility/logging/LogMessage.h"
class ConsoleLogger: public Logger
{
public:
ConsoleLogger();
~ConsoleLogger();
virtual void logInfo(const LogMessage& message);
virtual void logWarning(const LogMessage& message);
virtual void logError(const LogMessage& message);
private:
void logMessage(const std::string& type, const LogMessage& message);
};
#endif // CONSOLE_LOGGER_H
+16 -4
View File
@@ -9,23 +9,35 @@ struct LogMessage
public:
LogMessage(
const std::string& message,
const std::string& fileName,
const std::string& filePath,
const std::string& functionName,
const unsigned int line,
const std::tm& time
)
: message(message)
, fileName(fileName)
, filePath(filePath)
, functionName(functionName)
, line(line)
, time(time)
{}
std::string getTimeString(const std::string& format) const
{
char timeString[50];
strftime(timeString, 50, format.c_str(), &time);
return std::string(timeString);
}
std::string getFileName() const
{
return filePath.substr(filePath.find_last_of("/\\") + 1);
}
const std::string message;
const std::string fileName;
const std::string filePath;
const std::string functionName;
const unsigned int line;
const std::tm time;
};
#endif // LOG_MESSAGE_H
#endif // LOG_MESSAGE_H
+1 -2
View File
@@ -22,5 +22,4 @@ private:
const std::string m_type;
};
#endif
#endif // LOGGER_H