2015-05-11 00:18:22 +02:00
|
|
|
#ifndef LOGGER_H
|
|
|
|
#define LOGGER_H
|
|
|
|
|
2015-10-02 17:25:16 +02:00
|
|
|
#include <fstream>
|
2015-05-11 00:18:22 +02:00
|
|
|
#include <ostream>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The LogLevel enum
|
|
|
|
*/
|
|
|
|
enum class LogLevel : unsigned char
|
|
|
|
{
|
|
|
|
off, /// disable logging
|
|
|
|
error, /// error conditions
|
|
|
|
warning, /// warning conditions
|
|
|
|
notice, /// normal but significant conditions
|
|
|
|
info, /// informational messages
|
|
|
|
debug, /// debug-level messages
|
|
|
|
debug2 /// more debug-level messages
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief The Logger class
|
|
|
|
*
|
|
|
|
* The logger class is a thread-safe class which is used for formatting and forwarding
|
|
|
|
* log messages.
|
|
|
|
*/
|
2015-10-02 16:40:12 +02:00
|
|
|
class Logger final
|
|
|
|
{
|
2015-05-11 00:18:22 +02:00
|
|
|
public:
|
|
|
|
static Logger &get();
|
|
|
|
|
|
|
|
/// Log a string
|
2015-10-02 17:25:16 +02:00
|
|
|
void operator() (const std::string &message, const LogLevel level = LogLevel::debug);
|
2015-05-11 00:18:22 +02:00
|
|
|
|
|
|
|
/// Log a ostringstream
|
2015-10-02 17:25:16 +02:00
|
|
|
void operator() (const std::ostringstream &message, const LogLevel level = LogLevel::debug);
|
2015-05-11 00:18:22 +02:00
|
|
|
|
|
|
|
/// Log a format string
|
2015-10-02 17:25:16 +02:00
|
|
|
void operator() (const LogLevel level, const char* format, ...);
|
2015-05-11 00:18:22 +02:00
|
|
|
|
|
|
|
/// Set minimum required log level to generate output
|
|
|
|
void level(const LogLevel level);
|
|
|
|
/// Get minimum required log level to generate output
|
|
|
|
LogLevel level() const;
|
|
|
|
|
2015-10-02 17:25:16 +02:00
|
|
|
/// Getter/Setter for console output
|
|
|
|
bool console() const;
|
|
|
|
void console(const bool active);
|
|
|
|
|
|
|
|
/// Getter/Setter for logfile output
|
|
|
|
bool logFile() const;
|
|
|
|
void logFile(const bool active);
|
|
|
|
void setLogFile(const std::string &logFile);
|
|
|
|
|
2015-05-11 00:18:22 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Constructor
|
|
|
|
* @param level Minimum required log level to generate output
|
|
|
|
*/
|
|
|
|
Logger(const LogLevel level);
|
|
|
|
~Logger();
|
|
|
|
|
2015-10-02 17:25:16 +02:00
|
|
|
bool _consoleActive = { true };
|
|
|
|
bool _logFileActive = { false };
|
|
|
|
|
|
|
|
std::ofstream _logFile = {};
|
|
|
|
|
2015-05-11 00:18:22 +02:00
|
|
|
LogLevel _level;
|
|
|
|
mutable std::mutex _ostreamMutex;
|
|
|
|
};
|
|
|
|
#endif
|