AuroraRuntime/Source/Console/ConsoleMessage.cpp

134 lines
3.2 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: ConsoleMessage.cpp
Date: 2021-6-12
Author: Reece
***/
#include <Source/RuntimeInternal.hpp>
#include "ConsoleMessage.hpp"
#include "ColorConvert.hpp"
namespace Aurora::Console
{
static AuString StringifyTimeEx(const ConsoleMessage &msg, bool simple, bool utc)
{
try
{
std::tm localized;
Aurora::Time::ToCivilTime(msg.time, utc).CopyTo(localized);
if (simple)
{
return fmt::format("{:%H:%M:%S}", localized);
}
else
{
return fmt::format("{:%Y-%m-%d %H:%M:%S}", localized);
}
}
catch (...)
{
return {};
}
}
AUKN_SYM void ConsoleMessage::Read(Memory::ByteBuffer &deserialize)
{
AuUInt8 ref;
deserialize.Read(ref);
color = static_cast<EAnsiColor>(ref);
deserialize.Read(prefix);
deserialize.Read(line);
deserialize.Read(time);
deserialize.Read(tid);
}
AUKN_SYM void ConsoleMessage::Write(Memory::ByteBuffer &serialize) const
{
serialize.Write(static_cast<EAnsiColor>(color));
serialize.Write(prefix);
serialize.Write(line);
serialize.Write(time);
serialize.Write(tid);
}
AuString ConsoleMessage::StringifyTime(bool simple) const
{
try
{
return StringifyTimeEx(*this, simple, false);
}
catch (...)
{
return {};
}
}
AuString ConsoleMessage::StringifyTimeUTC() const
{
try
{
return StringifyTimeEx(*this, false, true);
}
catch (...)
{
return {};
}
}
AuString ConsoleMessage::GetWrappedTag() const
{
return "[" + prefix + "]";
}
AuString ConsoleMessage::ToConsole() const
{
try
{
return fmt::format("{}[{}] {:<8} | {}{}",
static_cast<EAnsiColor>(color) <= EAnsiColor::eEnumCount ?
kAnsiColorForegroundToVirtualEscape[static_cast<AuUInt>(color)] :
"",
StringifyTime(gRuntimeConfig.console.stdOutShortTime),
GetWrappedTag(),
line,
kAnsiColorForegroundToVirtualEscape[static_cast<AuUInt>(EAnsiColor::eReset)]);
}
catch (...)
{
return {};
}
}
AuString ConsoleMessage::ToPersistentString() const
{
try
{
return fmt::format("[{}] {:<7} | {}",
StringifyTimeUTC(),
GetWrappedTag(),
line);
}
catch (...)
{
return {};
}
}
AuString ConsoleMessage::ToSimplified() const
{
try
{
return fmt::format("{:<9} {:<8} | {}", StringifyTime(true), GetWrappedTag(), line);
}
catch (...)
{
return {};
}
}
}