2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-26 15:17:35 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-26 15:17:35 +00:00
|
|
|
// Log.h
|
|
|
|
//
|
2021-11-15 19:47:00 +00:00
|
|
|
// Log output.
|
2020-06-26 15:17:35 +00:00
|
|
|
//
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_LOG_H
|
|
|
|
#define ES_CORE_LOG_H
|
2013-01-04 23:31:51 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#include "utils/FileSystemUtil.h"
|
|
|
|
|
2021-11-15 19:47:00 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
2020-07-26 21:30:45 +00:00
|
|
|
#include <map>
|
2022-06-22 05:06:20 +00:00
|
|
|
#include <mutex>
|
2020-09-21 17:17:34 +00:00
|
|
|
#include <sstream>
|
2017-11-01 22:21:10 +00:00
|
|
|
|
2021-07-07 18:31:46 +00:00
|
|
|
#define LOG(level) \
|
|
|
|
if (level > Log::getReportingLevel()) \
|
|
|
|
; \
|
|
|
|
else \
|
|
|
|
Log().get(level)
|
2013-01-04 23:31:51 +00:00
|
|
|
|
2020-06-26 15:17:35 +00:00
|
|
|
enum LogLevel {
|
2022-06-05 10:36:55 +00:00
|
|
|
LogError,
|
2020-06-26 15:17:35 +00:00
|
|
|
LogWarning,
|
|
|
|
LogInfo,
|
|
|
|
LogDebug
|
|
|
|
};
|
2013-01-04 23:31:51 +00:00
|
|
|
|
|
|
|
class Log
|
|
|
|
{
|
|
|
|
public:
|
2022-06-22 05:06:20 +00:00
|
|
|
// Constructor/deconstructor handle a lock, making get() thread-safe.
|
|
|
|
Log();
|
2020-06-26 15:17:35 +00:00
|
|
|
~Log();
|
|
|
|
std::ostringstream& get(LogLevel level = LogInfo);
|
2013-01-04 23:31:51 +00:00
|
|
|
|
2022-06-22 05:06:20 +00:00
|
|
|
static LogLevel getReportingLevel();
|
|
|
|
static void setReportingLevel(LogLevel level);
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2022-06-22 05:06:20 +00:00
|
|
|
// getLogPath() is not thread-safe.
|
|
|
|
static std::string getLogPath();
|
|
|
|
// init() is not thread-safe.
|
2020-06-26 15:17:35 +00:00
|
|
|
static void init();
|
2022-06-22 05:06:20 +00:00
|
|
|
|
|
|
|
// The following static functions are thread-safe.
|
|
|
|
static void flush();
|
2020-06-26 15:17:35 +00:00
|
|
|
static void open();
|
|
|
|
static void close();
|
2013-01-04 23:31:51 +00:00
|
|
|
|
|
|
|
protected:
|
2021-11-15 19:47:00 +00:00
|
|
|
std::ostringstream mOutStringStream;
|
2020-06-26 15:17:35 +00:00
|
|
|
|
2013-01-04 23:31:51 +00:00
|
|
|
private:
|
2022-01-16 11:09:55 +00:00
|
|
|
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;
|
2013-01-04 23:31:51 +00:00
|
|
|
};
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#endif // ES_CORE_LOG_H
|