EHS/src/Log.cpp

149 lines
2.8 KiB
C++
Raw Normal View History

2024-02-05 22:25:30 -08:00
#include "ehs/Log.h"
namespace ehs
{
void (*Log::logCb)(const Log&) = nullptr;
Log Log::lastLog;
void Log::SetCallback(void (*newLogCb)(const Log&))
{
logCb = newLogCb;
}
void Log::Raise(const Log& log)
{
if (logCb)
logCb(log);
lastLog = log;
}
Log Log::GetLastLog()
{
Log result = lastLog;
lastLog = Log();
return result;
}
Log::Log()
: code(0)
{
}
Log::Log(std::initializer_list<Str_8> tags, const UInt_64 code, const Str_8& msg)
: tags(tags.size()), code(code), msg(msg)
{
UInt_64 i = 0;
for (auto v = tags.begin(); v != tags.end(); ++v)
this->tags[i++] = *v;
}
Log::Log(Array<Str_8>& tags, const UInt_64 code, const Str_8& msg)
: tags(tags), code(code), msg(msg)
{
}
Log::Log(const Log& log)
: tags(log.tags), code(log.code), msg(log.msg)
{
}
Log& Log::operator=(const Log& log)
{
if (this == &log)
return *this;
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;
}
*/
bool Log::HasTags(const std::initializer_list<Str_8> tags) const
{
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;
}
Array<Str_8> Log::GetTags() const
{
return tags;
}
UInt_64 Log::GetCode() const
{
return code;
}
Str_8 Log::GetMsg() const
{
return msg;
}
Str_8 Log::ToStr() const
{
Str_8 result = "[";
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();
}
}