Files
FEX-Emu--FEX/External/FEXCore/Source/Utils/LogManager.cpp
T
Ryan Houdek aa3b77e4c8 Uninstalls LogManager handlers on shutdown
In case something manages to have stayed alive and tried throwing a
message, this will no longer crash.

Noticed this with the previous commit
2021-01-11 09:03:56 -08:00

53 lines
1.4 KiB
C++

#include <FEXCore/Utils/LogManager.h>
#include <sstream>
#include <vector>
namespace LogMan {
namespace Throw {
std::vector<ThrowHandler> Handlers;
void InstallHandler(ThrowHandler Handler) { Handlers.emplace_back(Handler); }
void UnInstallHandlers() { Handlers.clear(); }
[[noreturn]] void M(const char *fmt, va_list args) {
size_t MsgSize = 1024;
char *Buffer = reinterpret_cast<char*>(alloca(MsgSize));
size_t Return = vsnprintf(Buffer, MsgSize, fmt, args);
if (Return >= MsgSize) {
// Allocate a bigger size on failure
MsgSize = Return;
Buffer = reinterpret_cast<char*>(alloca(MsgSize));
vsnprintf(Buffer, MsgSize, fmt, args);
}
for (auto &Handler : Handlers) {
Handler(Buffer);
}
__builtin_trap();
}
} // namespace Throw
namespace Msg {
std::vector<MsgHandler> Handlers;
void InstallHandler(MsgHandler Handler) { Handlers.emplace_back(Handler); }
void UnInstallHandlers() { Handlers.clear(); }
void M(DebugLevels Level, const char *fmt, va_list args) {
size_t MsgSize = 1024;
char *Buffer = reinterpret_cast<char*>(alloca(MsgSize));
size_t Return = vsnprintf(Buffer, MsgSize, fmt, args);
if (Return >= MsgSize) {
// Allocate a bigger size on failure
MsgSize = Return;
Buffer = reinterpret_cast<char*>(alloca(MsgSize));
vsnprintf(Buffer, MsgSize, fmt, args);
}
for (auto &Handler : Handlers) {
Handler(Level, Buffer);
}
}
} // namespace Msg
} // namespace LogMan