Fix logging performance and crashes on non-Windows platforms

#662
This commit is contained in:
Wojtek Figat
2023-10-13 14:41:45 +02:00
parent 03c120ba69
commit cf94cd937a

View File

@@ -199,35 +199,35 @@ void Log::Logger::WriteFloor()
void Log::Logger::ProcessLogMessage(LogType type, const StringView& msg, fmt_flax::memory_buffer& w)
{
const TimeSpan time = DateTime::Now() - LogStartTime;
const int32 msgLength = msg.Length();
fmt_flax::format(w, TEXT("[ {0} ]: [{1}] "), *time.ToString('a'), ToString(type));
// On Windows convert all '\n' into '\r\n'
#if PLATFORM_WINDOWS
const int32 msgLength = msg.Length();
if (msgLength > 1)
bool hasWindowsNewLine = false;
for (int32 i = 1; i < msgLength && !hasWindowsNewLine; i++)
hasWindowsNewLine |= msg.Get()[i - 1] != '\r' && msg.Get()[i] == '\n';
if (hasWindowsNewLine)
{
MemoryWriteStream msgStream(msgLength * sizeof(Char));
msgStream.WriteChar(msg[0]);
msgStream.WriteChar(msg.Get()[0]);
for (int32 i = 1; i < msgLength; i++)
{
if (msg[i - 1] != '\r' && msg[i] == '\n')
if (msg.Get()[i - 1] != '\r' && msg.Get()[i] == '\n')
msgStream.WriteChar(TEXT('\r'));
msgStream.WriteChar(msg[i]);
msgStream.WriteChar(msg.Get()[i]);
}
msgStream.WriteChar(msg[msgLength]);
msgStream.WriteChar(TEXT('\0'));
fmt_flax::format(w, TEXT("{}"), (const Char*)msgStream.GetHandle());
//w.append(msgStream.GetHandle(), msgStream.GetHandle() + msgStream.GetPosition());
w.append((const Char*)msgStream.GetHandle(), (const Char*)msgStream.GetHandle() + msgStream.GetPosition());
//fmt_flax::format(w, TEXT("{}"), (const Char*)msgStream.GetHandle());
return;
}
else
{
//w.append(msg.Get(), msg.Get() + msg.Length());
fmt_flax::format(w, TEXT("{}"), msg);
}
#else
fmt_flax::format(w, TEXT("{}"), msg);
#endif
// Output raw message to the log
w.append(msg.Get(), msg.Get() + msg.Length());
//fmt_flax::format(w, TEXT("{}"), msg);
}
void Log::Logger::Write(LogType type, const StringView& msg)