Fixed the Logging system to actually be able to handle errors. Database is also fixed to use directories.
This commit is contained in:
143
src/Log.cpp
143
src/Log.cpp
@@ -1,58 +1,127 @@
|
||||
#include "ehs/Log.h"
|
||||
|
||||
#include "ehs/io/Console.h"
|
||||
#include "ehs/io/File.h"
|
||||
|
||||
namespace ehs
|
||||
{
|
||||
void (*Log::logCb)(const Log&) = nullptr;
|
||||
Log Log::lastLog;
|
||||
|
||||
void Log::SetCallback(void (*newLogCb)(const Log&))
|
||||
void Log::DefaultRaisedCb(const Log &log)
|
||||
{
|
||||
logCb = newLogCb;
|
||||
Console::Write_8(log.ToStr());
|
||||
}
|
||||
|
||||
void Log::Raise(const Log& log)
|
||||
void Log::DefaultOutputCb(const Array<Log> &logs)
|
||||
{
|
||||
if (logCb)
|
||||
logCb(log);
|
||||
File output("Logs.txt", Mode::WRITE, Disposition::CREATE_PERSISTENT);
|
||||
output.SeekBeginning();
|
||||
|
||||
lastLog = log;
|
||||
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;
|
||||
Log Log::lastLog;
|
||||
bool Log::immediate = false;
|
||||
|
||||
void Log::SetRaisedCallback(const LogRaisedCb newCb)
|
||||
{
|
||||
raisedCb = newCb;
|
||||
}
|
||||
|
||||
void Log::SetOutputCallback(const LogOutputCb newCb)
|
||||
{
|
||||
outputCb = newCb;
|
||||
}
|
||||
|
||||
void Log::OnExit()
|
||||
{
|
||||
if (lastLog.GetType() != LogType::SUCCESS)
|
||||
logs.Push(lastLog);
|
||||
|
||||
lastLog = {};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
Log Log::GetLastLog()
|
||||
{
|
||||
Log result = lastLog;
|
||||
lastLog = Log();
|
||||
lastLog = {};
|
||||
return result;
|
||||
}
|
||||
|
||||
void Log::EnableImmediateMode(const bool enable)
|
||||
{
|
||||
immediate = enable;
|
||||
}
|
||||
|
||||
Log::Log()
|
||||
: code(0)
|
||||
: type(LogType::SUCCESS), 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)
|
||||
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)
|
||||
{
|
||||
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(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(const Log& log)
|
||||
: tags(log.tags), code(log.code), msg(log.msg)
|
||||
Log::Log(Log &&log) noexcept
|
||||
: type(log.type), tags((Array<Str_8>&&)log.tags), code(log.code), msg((Str_8&&)log.msg)
|
||||
{
|
||||
log.type = LogType::INFO;
|
||||
log.code = 0;
|
||||
}
|
||||
|
||||
Log::Log(const Log &log)
|
||||
: type(log.type), tags(log.tags), code(log.code), msg(log.msg)
|
||||
{
|
||||
}
|
||||
|
||||
Log& Log::operator=(const Log& log)
|
||||
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)
|
||||
{
|
||||
if (this == &log)
|
||||
return *this;
|
||||
|
||||
type = log.type;
|
||||
tags = log.tags;
|
||||
code = log.code;
|
||||
msg = log.msg;
|
||||
@@ -72,7 +141,12 @@ namespace ehs
|
||||
}
|
||||
*/
|
||||
|
||||
bool Log::HasTags(const std::initializer_list<Str_8> tags) const
|
||||
LogType Log::GetType() const
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
bool Log::HasTags(const std::initializer_list<Str_8> &tags) const
|
||||
{
|
||||
UInt_64 i = 0;
|
||||
UInt_64 c = 0;
|
||||
@@ -110,7 +184,7 @@ namespace ehs
|
||||
return false;
|
||||
}
|
||||
|
||||
Array<Str_8> Log::GetTags() const
|
||||
const Array<Str_8> &Log::GetTags() const
|
||||
{
|
||||
return tags;
|
||||
}
|
||||
@@ -127,7 +201,32 @@ namespace ehs
|
||||
|
||||
Str_8 Log::ToStr() const
|
||||
{
|
||||
Str_8 result = "[";
|
||||
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 += ">[";
|
||||
|
||||
for (UInt_64 i = 0; i < tags.Size(); ++i)
|
||||
{
|
||||
@@ -146,4 +245,4 @@ namespace ehs
|
||||
{
|
||||
return tags.Size() && msg.Size();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user