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:
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -22,5 +22,4 @@ private:
|
||||
const std::string m_type;
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
#endif // LOGGER_H
|
||||
|
||||
Reference in New Issue
Block a user