mirror of
https://github.com/FEX-Emu/FEX.git
synced 2026-10-08 09:00:48 +02:00
va_list types have an edge where after operating on it, then it is undefined behaviour to do anything other than va_end on the object. So in order to call vsnprintf on it multiple times we must create copies with va_copy. Easy enough to work around by doing a few more copies of the va_list
65 lines
1.7 KiB
C++
65 lines
1.7 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));
|
|
va_list argsCopy;
|
|
va_copy(argsCopy, args);
|
|
size_t Return = vsnprintf(Buffer, MsgSize, fmt, argsCopy);
|
|
va_end(argsCopy);
|
|
if (Return >= MsgSize) {
|
|
// Allocate a bigger size on failure
|
|
MsgSize = Return;
|
|
Buffer = reinterpret_cast<char*>(alloca(MsgSize));
|
|
va_end(argsCopy);
|
|
va_copy(argsCopy, args);
|
|
vsnprintf(Buffer, MsgSize, fmt, argsCopy);
|
|
va_end(argsCopy);
|
|
}
|
|
|
|
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));
|
|
va_list argsCopy;
|
|
va_copy(argsCopy, args);
|
|
size_t Return = vsnprintf(Buffer, MsgSize, fmt, argsCopy);
|
|
va_end(argsCopy);
|
|
if (Return >= MsgSize) {
|
|
// Allocate a bigger size on failure
|
|
MsgSize = Return;
|
|
Buffer = reinterpret_cast<char*>(alloca(MsgSize));
|
|
va_end(argsCopy);
|
|
va_copy(argsCopy, args);
|
|
vsnprintf(Buffer, MsgSize, fmt, argsCopy);
|
|
va_end(argsCopy);
|
|
}
|
|
for (auto &Handler : Handlers) {
|
|
Handler(Level, Buffer);
|
|
}
|
|
}
|
|
|
|
} // namespace Msg
|
|
} // namespace LogMan
|