mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-09 19:00:20 +02:00
In case something manages to have stayed alive and tried throwing a message, this will no longer crash. Noticed this with the previous commit
53 lines
1.4 KiB
C++
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
|