2024-02-05 22:25:30 -08:00
|
|
|
#include "ehs/Log.h"
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
#include "ehs/io/Console.h"
|
|
|
|
#include "ehs/io/File.h"
|
|
|
|
|
2024-02-05 22:25:30 -08:00
|
|
|
namespace ehs
|
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
void Log::DefaultRaisedCb(const Log &log)
|
|
|
|
{
|
|
|
|
Console::Write_8(log.ToStr());
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::DefaultOutputCb(const Array<Log> &logs)
|
|
|
|
{
|
|
|
|
File output("Logs.txt", Mode::WRITE, Disposition::CREATE_PERSISTENT);
|
|
|
|
output.SeekBeginning();
|
|
|
|
|
|
|
|
for (UInt_64 i = 0; i < logs.Size(); ++i)
|
|
|
|
output.WriteStr_8(logs[i].ToStr() + "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
LogRaisedCb Log::raisedCb = DefaultRaisedCb;
|
|
|
|
LogOutputCb Log::outputCb = DefaultOutputCb;
|
|
|
|
Array<Log> Log::logs;
|
2024-02-05 22:25:30 -08:00
|
|
|
Log Log::lastLog;
|
2024-04-23 22:29:49 -07:00
|
|
|
bool Log::immediate = false;
|
|
|
|
|
|
|
|
void Log::SetRaisedCallback(const LogRaisedCb newCb)
|
|
|
|
{
|
|
|
|
raisedCb = newCb;
|
|
|
|
}
|
2024-02-05 22:25:30 -08:00
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
void Log::SetOutputCallback(const LogOutputCb newCb)
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
outputCb = newCb;
|
2024-02-05 22:25:30 -08:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
void Log::OnExit()
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
if (lastLog.GetType() != LogType::SUCCESS)
|
|
|
|
logs.Push(lastLog);
|
|
|
|
|
|
|
|
lastLog = {};
|
2024-02-05 22:25:30 -08:00
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
if (outputCb)
|
|
|
|
outputCb(logs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::Raise(Log log)
|
|
|
|
{
|
|
|
|
if (log.GetType() == LogType::INFO || (log.GetType() != LogType::SUCCESS && immediate))
|
|
|
|
if (raisedCb)
|
|
|
|
raisedCb(log);
|
|
|
|
|
|
|
|
if (lastLog.GetType() != LogType::SUCCESS)
|
|
|
|
logs.Push((Log&&)lastLog);
|
|
|
|
|
|
|
|
lastLog = (Log&&)log;
|
2024-02-05 22:25:30 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
Log Log::GetLastLog()
|
|
|
|
{
|
|
|
|
Log result = lastLog;
|
2024-04-23 22:29:49 -07:00
|
|
|
lastLog = {};
|
2024-02-05 22:25:30 -08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
void Log::EnableImmediateMode(const bool enable)
|
|
|
|
{
|
|
|
|
immediate = enable;
|
|
|
|
}
|
|
|
|
|
2024-02-05 22:25:30 -08:00
|
|
|
Log::Log()
|
2024-04-23 22:29:49 -07:00
|
|
|
: type(LogType::SUCCESS), code(0)
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
Log::Log(LogType type, const std::initializer_list<Str_8> &tags, const UInt_64 code, Str_8 msg)
|
|
|
|
: type(type), tags(tags.size()), code(code), msg((Str_8&&)msg)
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
|
|
|
UInt_64 i = 0;
|
|
|
|
for (auto v = tags.begin(); v != tags.end(); ++v)
|
|
|
|
this->tags[i++] = *v;
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
Log::Log(LogType type, Array<Str_8> tags, const UInt_64 code, Str_8 msg)
|
|
|
|
: type(type), tags((Array<Str_8>&&)tags), code(code), msg((Str_8&&)msg)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::Log(Log &&log) noexcept
|
|
|
|
: type(log.type), tags((Array<Str_8>&&)log.tags), code(log.code), msg((Str_8&&)log.msg)
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
log.type = LogType::INFO;
|
|
|
|
log.code = 0;
|
2024-02-05 22:25:30 -08:00
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
Log::Log(const Log &log)
|
|
|
|
: type(log.type), tags(log.tags), code(log.code), msg(log.msg)
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
Log & Log::operator=(Log &&log) noexcept
|
|
|
|
{
|
|
|
|
if (this == &log)
|
|
|
|
return *this;
|
|
|
|
|
|
|
|
type = log.type;
|
|
|
|
tags = (Array<Str_8>&&)log.tags;
|
|
|
|
code = log.code;
|
|
|
|
msg = (Str_8&&)log.msg;
|
|
|
|
|
|
|
|
log.type = LogType::INFO;
|
|
|
|
log.code = 0;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Log& Log::operator=(const Log &log)
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
|
|
|
if (this == &log)
|
|
|
|
return *this;
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
type = log.type;
|
2024-02-05 22:25:30 -08:00
|
|
|
tags = log.tags;
|
|
|
|
code = log.code;
|
|
|
|
msg = log.msg;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
bool Log::operator==(const Log log)
|
|
|
|
{
|
|
|
|
return src == log.src && type == log.type && code == log.code && msg == log.msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Log::operator!=(const Log log)
|
|
|
|
{
|
|
|
|
return src != log.src || type != log.type || code != log.code || msg != log.msg;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
LogType Log::GetType() const
|
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Log::HasTags(const std::initializer_list<Str_8> &tags) const
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
|
|
|
UInt_64 i = 0;
|
|
|
|
UInt_64 c = 0;
|
|
|
|
|
|
|
|
for (auto v = tags.begin(); v != tags.end(); ++v)
|
|
|
|
if (this->tags[i++].GetLower() == v->GetLower())
|
|
|
|
++c;
|
|
|
|
|
|
|
|
if (c == tags.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Log::HasTags(const Array<Str_8> &tags) const
|
|
|
|
{
|
|
|
|
UInt_64 c = 0;
|
|
|
|
|
|
|
|
for (UInt_64 i = 0; i < tags.Size(); ++i)
|
|
|
|
if (this->tags[i].GetLower() == tags[c].GetLower())
|
|
|
|
++c;
|
|
|
|
|
|
|
|
if (c == tags.Size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Log::HasTag(const Str_8& tag) const
|
|
|
|
{
|
|
|
|
for (UInt_64 i = 0; i < tags.Size(); ++i)
|
|
|
|
if (tags[i].GetLower() == tag.GetLower())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
const Array<Str_8> &Log::GetTags() const
|
2024-02-05 22:25:30 -08:00
|
|
|
{
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
UInt_64 Log::GetCode() const
|
|
|
|
{
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
Str_8 Log::GetMsg() const
|
|
|
|
{
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Str_8 Log::ToStr() const
|
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
Str_8 result = "<";
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case LogType::SUCCESS:
|
|
|
|
{
|
|
|
|
result += "Success";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LogType::ERR:
|
|
|
|
{
|
|
|
|
result += "Error";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LogType::WARN:
|
|
|
|
{
|
|
|
|
result += "Warning";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case LogType::INFO:
|
|
|
|
{
|
|
|
|
result += "Information";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result += ">[";
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
for (UInt_64 i = 0; i < tags.Size(); ++i)
|
|
|
|
{
|
|
|
|
result += tags[i];
|
|
|
|
|
|
|
|
if (i != tags.Size() - 1)
|
|
|
|
result += ", ";
|
|
|
|
}
|
|
|
|
|
|
|
|
result += "](" + Str_8::FromNum(code) + "): " + msg;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Log::IsValid() const
|
|
|
|
{
|
|
|
|
return tags.Size() && msg.Size();
|
|
|
|
}
|
2024-04-23 22:29:49 -07:00
|
|
|
}
|