2022-03-16 20:50:55 +01:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
2022-08-22 18:50:38 +02:00
|
|
|
#include <wlr/util/log.h>
|
2023-09-06 12:51:36 +02:00
|
|
|
#include <format>
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <chrono>
|
|
|
|
#include "../helpers/MiscFunctions.hpp"
|
2022-03-16 20:50:55 +01:00
|
|
|
|
|
|
|
#define LOGMESSAGESIZE 1024
|
|
|
|
|
2023-09-06 12:51:36 +02:00
|
|
|
enum LogLevel
|
|
|
|
{
|
2022-03-16 20:50:55 +01:00
|
|
|
NONE = -1,
|
2022-12-16 18:17:31 +01:00
|
|
|
LOG = 0,
|
2022-03-16 20:50:55 +01:00
|
|
|
WARN,
|
|
|
|
ERR,
|
2022-06-25 20:50:29 +02:00
|
|
|
CRIT,
|
2023-08-21 19:36:09 +02:00
|
|
|
INFO,
|
|
|
|
TRACE
|
2022-03-16 20:50:55 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace Debug {
|
2022-06-03 17:41:57 +02:00
|
|
|
inline std::string logFile;
|
2023-03-24 14:00:54 +01:00
|
|
|
inline int64_t* disableLogs = nullptr;
|
|
|
|
inline int64_t* disableTime = nullptr;
|
|
|
|
inline bool disableStdout = false;
|
2023-08-21 19:36:09 +02:00
|
|
|
inline bool trace = false;
|
2023-09-06 12:51:36 +02:00
|
|
|
|
|
|
|
void init(const std::string& IS);
|
|
|
|
template <typename... Args>
|
|
|
|
void log(LogLevel level, const std::string& fmt, Args&&... args) {
|
|
|
|
if (disableLogs && *disableLogs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (level == TRACE && !trace)
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string logMsg = "";
|
|
|
|
|
|
|
|
switch (level) {
|
|
|
|
case LOG: logMsg += "[LOG] "; break;
|
|
|
|
case WARN: logMsg += "[WARN] "; break;
|
|
|
|
case ERR: logMsg += "[ERR] "; break;
|
|
|
|
case CRIT: logMsg += "[CRITICAL] "; break;
|
|
|
|
case INFO: logMsg += "[INFO] "; break;
|
|
|
|
case TRACE: logMsg += "[TRACE] "; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// log to a file
|
|
|
|
std::ofstream ofs;
|
|
|
|
ofs.open(logFile, std::ios::out | std::ios::app);
|
|
|
|
|
|
|
|
// print date and time to the ofs
|
|
|
|
if (disableTime && !*disableTime) {
|
|
|
|
#ifndef _LIBCPP_VERSION
|
|
|
|
logMsg += std::format("[{:%T}] ", std::chrono::hh_mm_ss{std::chrono::system_clock::now() - std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now())});
|
|
|
|
#else
|
|
|
|
auto c = std::chrono::hh_mm_ss{std::chrono::system_clock::now() - std::chrono::floor<std::chrono::days>(std::chrono::system_clock::now())};
|
|
|
|
logMsg += std::format("{:%H}:{:%M}:{:%S}", c.hours(), c.minutes(), c.subseconds());
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
logMsg += std::vformat(fmt, std::make_format_args(args...));
|
|
|
|
} catch (std::exception& e) {
|
|
|
|
std::string exceptionMsg = e.what();
|
|
|
|
Debug::log(ERR, "caught exception in Debug::log: {}", exceptionMsg);
|
|
|
|
|
|
|
|
const auto CALLSTACK = getBacktrace();
|
|
|
|
|
|
|
|
Debug::log(LOG, "stacktrace:");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < CALLSTACK.size(); ++i) {
|
|
|
|
Debug::log(NONE, "\t #{} | {}", i, CALLSTACK[i].desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ofs << logMsg << "\n";
|
|
|
|
|
|
|
|
ofs.close();
|
|
|
|
|
|
|
|
// log it to the stdout too.
|
|
|
|
if (!disableStdout)
|
|
|
|
std::cout << logMsg << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
void wlrLog(wlr_log_importance level, const char* fmt, va_list args);
|
2022-03-16 20:50:55 +01:00
|
|
|
};
|