AuroraRuntime/Source/Console/ConsoleMessage.cpp

105 lines
3.0 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;
Time::ToCivilTime(msg.time, utc ?
Time::ETimezoneShift::eUTC :
Time::ETimezoneShift::eLocalTime).CopyTo(localized);
if (simple)
{
return AuString(fmt::format("{:%H:%M:%S}", localized));
}
else
{
return AuString(fmt::format("{:%Y-%m-%d %H:%M:%S}", localized));
}
}
catch (...)
{
return {};
}
}
AUKN_SYM void ConsoleMessage::Read(Memory::ByteBuffer &deserialize)
{
AuUInt8 ref;
deserialize.Read(ref);
this->color = static_cast<EAnsiColor>(ref);
deserialize.Read(this->prefix);
deserialize.Read(this->line);
deserialize.Read(this->time);
deserialize.Read(this->tid);
}
AUKN_SYM void ConsoleMessage::Write(Memory::ByteBuffer &serialize) const
{
serialize.Write(static_cast<AuUInt8>(this->color));
serialize.Write(this->prefix);
serialize.Write(this->line);
serialize.Write(this->time);
serialize.Write(this->tid);
}
AuString ConsoleMessage::StringifyTime(bool simple) const
{
return StringifyTimeEx(*this,
simple,
false);
}
AuString ConsoleMessage::StringifyTimeUTC() const
{
return StringifyTimeEx(*this,
false,
true);
}
AuString ConsoleMessage::GetWrappedTag() const
{
return "[" + this->prefix + "]";
}
AuString ConsoleMessage::ToConsole() const
{
if (gRuntimeConfig.console.bStdOutUseLocalTime)
{
return this->ToPersistentString();
}
return AuString(fmt::format("[{}] {:<8} | {}",
StringifyTime(gRuntimeConfig.console.bStdOutShortTime),
GetWrappedTag(),
this->line));
}
AuString ConsoleMessage::ToPersistentString() const
{
return AuString(fmt::format("[{}] {:<8} | {}",
StringifyTimeUTC(),
GetWrappedTag(),
this->line));
}
AuString ConsoleMessage::ToSimplified() const
{
return AuString(fmt::format("{:<9} {:<8} | {}", StringifyTime(true), GetWrappedTag(), this->line));
}
}