more safety around logs

This commit is contained in:
vaxerski 2023-09-06 12:32:14 +02:00
parent 7efbbad1bd
commit 000b6d44dc
3 changed files with 39 additions and 2 deletions

View File

@ -5,6 +5,7 @@
#include <iostream>
#include <fstream>
#include <chrono>
#include "../helpers/MiscFunctions.hpp"
#define LOGMESSAGESIZE 1024
@ -62,7 +63,20 @@ namespace Debug {
#endif
}
logMsg += std::vformat(fmt, std::make_format_args(args...));
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";

View File

@ -705,3 +705,7 @@ void throwError(const std::string& err) {
Debug::log(CRIT, "Critical error thrown: {}", err.c_str());
throw std::runtime_error(err);
}
std::string sendToLog(uint8_t level, const std::string& s) {
Debug::log((LogLevel)level, s);
}

View File

@ -32,7 +32,26 @@ std::string replaceInString(std::string subject, const std:
std::vector<SCallstackFrameInfo> getBacktrace();
void throwError(const std::string& err);
// why, C++.
std::string sendToLog(uint8_t, const std::string&);
template <typename... Args>
std::string getFormat(const std::string& fmt, Args&&... args) {
return std::vformat(fmt, std::make_format_args(args...));
std::string fmtdMsg;
try {
fmtdMsg += std::vformat(fmt, std::make_format_args(args...));
} catch (std::exception& e) {
std::string exceptionMsg = e.what();
sendToLog(2, std::format("caught exception in getFormat: {}", exceptionMsg));
const auto CALLSTACK = getBacktrace();
sendToLog(0, "stacktrace:");
for (size_t i = 0; i < CALLSTACK.size(); ++i) {
sendToLog(1, std::format("\t #{} | {}", i, CALLSTACK[i].desc));
}
}
return fmtdMsg;
}