1
0
mirror of https://github.com/binary-kitchen/doorlockd synced 2024-11-16 20:19:13 +01:00
doorlockd-mirror/doorlockd/lib/logger.h

76 lines
1.8 KiB
C
Raw Normal View History

2015-05-11 00:18:22 +02:00
#ifndef LOGGER_H
#define LOGGER_H
#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
void operator() (const std::string &message, const LogLevel level = LogLevel::debug);
2015-05-11 00:18:22 +02:00
/// Log a ostringstream
void operator() (const std::ostringstream &message, const LogLevel level = LogLevel::debug);
2015-05-11 00:18:22 +02:00
/// Log a format string
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;
/// 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();
bool _consoleActive = { true };
bool _logFileActive = { false };
std::ofstream _logFile = {};
2015-05-11 00:18:22 +02:00
LogLevel _level;
mutable std::mutex _ostreamMutex;
};
#endif