ES-DE/es-core/src/Log.h

71 lines
1.9 KiB
C
Raw Normal View History

// SPDX-License-Identifier: MIT
//
// EmulationStation Desktop Edition
// Log.h
//
2021-11-15 19:47:00 +00:00
// Log output.
//
#ifndef ES_CORE_LOG_H
#define ES_CORE_LOG_H
#include "utils/FileSystemUtil.h"
2021-11-15 19:47:00 +00:00
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
2022-06-22 05:06:20 +00:00
#include <mutex>
#include <sstream>
2017-11-01 22:21:10 +00:00
#define LOG(level) \
if (level > Log::getReportingLevel()) \
; \
else \
Log().get(level)
enum LogLevel {
LogError,
LogWarning,
LogInfo,
LogDebug
};
class Log
{
public:
2022-06-22 05:06:20 +00:00
// Constructor/deconstructor handle a lock, making get() thread-safe.
Log();
~Log();
std::ostringstream& get(LogLevel level = LogInfo);
2022-06-22 05:06:20 +00:00
static LogLevel getReportingLevel();
static void setReportingLevel(LogLevel level);
2022-06-22 05:06:20 +00:00
// getLogPath() is not thread-safe.
static std::string getLogPath();
// init() is not thread-safe.
static void init();
2022-06-22 05:06:20 +00:00
// The following static functions are thread-safe.
static void flush();
static void open();
static void close();
protected:
2021-11-15 19:47:00 +00:00
std::ostringstream mOutStringStream;
private:
std::map<LogLevel, std::string> mLogLevelMap {// Log level indicators.
{LogError, "Error"},
{LogWarning, "Warn"},
{LogInfo, "Info"},
{LogDebug, "Debug"}};
2022-01-29 17:16:30 +00:00
static inline std::ofstream sFile;
static inline LogLevel sReportingLevel = LogInfo;
2022-06-22 05:06:20 +00:00
static inline std::recursive_mutex sLogMutex;
2021-11-15 19:47:00 +00:00
LogLevel mMessageLevel;
};
#endif // ES_CORE_LOG_H