diff options
| author | Vaxry <43317083+vaxerski@users.noreply.github.com> | 2023-09-06 12:51:36 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-06 12:51:36 +0200 |
| commit | 61a71c65ace5124cb26a074715280537e6e34d00 (patch) | |
| tree | b54cf4d7a0f8477b2958619384982d31663f128f /src/debug/Log.hpp | |
| parent | c3a83daa1e61285a418c39a1d9f29427c2c9a2c3 (diff) | |
internal: Formatter rework (#3186)
Diffstat (limited to 'src/debug/Log.hpp')
| -rw-r--r-- | src/debug/Log.hpp | 74 |
1 files changed, 69 insertions, 5 deletions
diff --git a/src/debug/Log.hpp b/src/debug/Log.hpp index b2d76187..a65bccb1 100644 --- a/src/debug/Log.hpp +++ b/src/debug/Log.hpp @@ -1,10 +1,16 @@ #pragma once #include <string> #include <wlr/util/log.h> +#include <format> +#include <iostream> +#include <fstream> +#include <chrono> +#include "../helpers/MiscFunctions.hpp" #define LOGMESSAGESIZE 1024 -enum LogLevel { +enum LogLevel +{ NONE = -1, LOG = 0, WARN, @@ -15,13 +21,71 @@ enum LogLevel { }; namespace Debug { - void init(const std::string& IS); - void log(LogLevel level, const char* fmt, ...); - void wlrLog(wlr_log_importance level, const char* fmt, va_list args); - inline std::string logFile; inline int64_t* disableLogs = nullptr; inline int64_t* disableTime = nullptr; inline bool disableStdout = false; inline bool trace = false; + + 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); };
\ No newline at end of file |
