2024-02-05 22:25:30 -08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <initializer_list>
|
|
|
|
|
|
|
|
#include "Types.h"
|
|
|
|
#include "Array.h"
|
|
|
|
#include "UTF.h"
|
|
|
|
#include "Str.h"
|
|
|
|
|
|
|
|
namespace ehs
|
|
|
|
{
|
2024-04-23 22:29:49 -07:00
|
|
|
class Log;
|
|
|
|
|
|
|
|
typedef void (*LogRaisedCb)(const Log &);
|
|
|
|
typedef void (*LogOutputCb)(const Array<Log> &);
|
|
|
|
|
|
|
|
enum class LogType : UInt_8
|
|
|
|
{
|
|
|
|
SUCCESS,
|
|
|
|
ERR,
|
|
|
|
WARN,
|
|
|
|
INFO
|
|
|
|
};
|
|
|
|
|
2024-02-05 22:25:30 -08:00
|
|
|
/// A helper class for holding error information and handling them.
|
|
|
|
/// @tparam T The character data type to use.
|
|
|
|
/// @tparam N The number data type to use.
|
|
|
|
class Log
|
|
|
|
{
|
|
|
|
private:
|
2024-04-23 22:29:49 -07:00
|
|
|
static void DefaultRaisedCb(const Log &log);
|
|
|
|
|
|
|
|
static void DefaultOutputCb(const Array<Log> &logs);
|
|
|
|
|
|
|
|
static LogRaisedCb raisedCb;
|
|
|
|
static LogOutputCb outputCb;
|
|
|
|
static Array<Log> logs;
|
2024-02-05 22:25:30 -08:00
|
|
|
static Log lastLog;
|
2024-04-23 22:29:49 -07:00
|
|
|
static bool immediate;
|
|
|
|
LogType type;
|
2024-02-05 22:25:30 -08:00
|
|
|
Array<Str_8> tags;
|
|
|
|
UInt_64 code;
|
|
|
|
Str_8 msg;
|
|
|
|
|
|
|
|
public:
|
2024-04-23 22:29:49 -07:00
|
|
|
static void SetRaisedCallback(LogRaisedCb newCb);
|
|
|
|
|
|
|
|
static void SetOutputCallback(LogOutputCb newCb);
|
2024-02-05 22:25:30 -08:00
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
static void OnExit();
|
|
|
|
|
|
|
|
static void Raise(Log log);
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Retrieves the last log raised.
|
|
|
|
static Log GetLastLog();
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
static void EnableImmediateMode(bool enable);
|
|
|
|
|
2024-02-05 22:25:30 -08:00
|
|
|
/// Default members initialization.
|
|
|
|
Log();
|
|
|
|
|
|
|
|
/// Initializes members with the given information.
|
|
|
|
/// @param [in] tags The tags to associate this log with.
|
|
|
|
/// @param [in] code The unique code to use.
|
|
|
|
/// @param [in] msg Detailed information about what happened.
|
2024-04-23 22:29:49 -07:00
|
|
|
Log(LogType type, const std::initializer_list<Str_8> &tags, UInt_64 code, Str_8 msg);
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Initializes members with the given information.
|
|
|
|
/// @param [in] tags The tags to associate this log with.
|
|
|
|
/// @param [in] code The unique code to use.
|
|
|
|
/// @param [in] msg Detailed information about what happened.
|
2024-04-23 22:29:49 -07:00
|
|
|
Log(LogType type, Array<Str_8> tags, UInt_64 code, Str_8 msg);
|
|
|
|
|
|
|
|
Log(Log &&log) noexcept;
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Copies all members from the given log.
|
|
|
|
/// @param [in] log The log to copy from.
|
2024-04-23 22:29:49 -07:00
|
|
|
Log(const Log &log);
|
|
|
|
|
|
|
|
Log &operator=(Log &&log) noexcept;
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Copies all members from the given log.
|
|
|
|
/// @param [in] log The log to copy from.
|
|
|
|
/// @returns The log that has been assigned to.
|
2024-04-23 22:29:49 -07:00
|
|
|
Log &operator=(const Log &log);
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
/// Compares with another given log.
|
|
|
|
/// @param [in] log The log to compare with.
|
|
|
|
/// @returns Whether or not they are equal.
|
|
|
|
bool operator==(const Log log);
|
|
|
|
|
|
|
|
/// Compares with another given log.
|
|
|
|
/// @param [in] log The log to compare with.
|
|
|
|
/// @returns Whether or not they are equal.
|
|
|
|
bool operator!=(const Log log);
|
|
|
|
*/
|
|
|
|
|
2024-04-23 22:29:49 -07:00
|
|
|
LogType GetType() const;
|
|
|
|
|
2024-02-05 22:25:30 -08:00
|
|
|
/// Checks whether or not this log has the given tags.
|
|
|
|
/// @param [in] tags The tags to look for.
|
|
|
|
/// @returns True if all tags were found, otherwise false.
|
2024-04-23 22:29:49 -07:00
|
|
|
bool HasTags(const std::initializer_list<Str_8> &tags) const;
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Checks whether or not this log has the given tags.
|
|
|
|
/// @param [in] tags The tags to look for.
|
|
|
|
/// @returns True if all tags were found, otherwise false.
|
2024-04-23 22:29:49 -07:00
|
|
|
bool HasTags(const Array<Str_8> &tags) const;
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Checks whether or not this log has the given tag.
|
|
|
|
/// @param [in] tag The tag to look for.
|
|
|
|
/// @returns True if tag was found, otherwise false.
|
2024-04-23 22:29:49 -07:00
|
|
|
bool HasTag(const Str_8 &tag) const;
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
/// Retrieves all the tags.
|
|
|
|
/// @returns The result.
|
2024-04-23 22:29:49 -07:00
|
|
|
const Array<Str_8> &GetTags() const;
|
2024-02-05 22:25:30 -08:00
|
|
|
|
|
|
|
UInt_64 GetCode() const;
|
|
|
|
|
|
|
|
/// Retrieves the detailed error message string.
|
|
|
|
/// @returns The error message.
|
|
|
|
Str_8 GetMsg() const;
|
|
|
|
|
|
|
|
Str_8 ToStr() const;
|
|
|
|
|
|
|
|
/// Retrieves whether or not this is a valid object.
|
|
|
|
/// @returns The result.
|
|
|
|
/// @note To be a valid object it must have one or more tags and a message size greater than zero.
|
|
|
|
bool IsValid() const;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef EHS_LOG_INT
|
|
|
|
#ifdef EHS_DEBUG
|
2024-04-23 22:29:49 -07:00
|
|
|
#define EHS_LOG_INT(type, code, msg) ehs::Log::Raise({type, {ehs::GetAcronym_8(), EHS_FILE, EHS_FUNC, ehs::Str_8::FromNum((ehs::UInt_32)EHS_LINE)}, code, msg})
|
2024-02-05 22:25:30 -08:00
|
|
|
#else
|
2024-04-23 22:29:49 -07:00
|
|
|
#define EHS_LOG_INT(type, code, msg) ehs::Log::Raise({type, {ehs::GetAcronym_8(), EHS_FUNC}, code, msg})
|
2024-02-05 22:25:30 -08:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EHS_LOG
|
|
|
|
#ifdef EHS_DEBUG
|
2024-04-23 22:29:49 -07:00
|
|
|
#define EHS_LOG(type, code, msg) ehs::Log::Raise({type, {ehs::GetAppName_8(), EHS_FILE, EHS_FUNC, ehs::Str_8::FromNum((ehs::UInt_32)EHS_LINE)}, code, msg})
|
2024-02-05 22:25:30 -08:00
|
|
|
#else
|
2024-04-23 22:29:49 -07:00
|
|
|
#define EHS_LOG(type, code, msg) ehs::Log::Raise({type, {ehs::GetAppName_8(), EHS_FUNC}, code, msg})
|
2024-02-05 22:25:30 -08:00
|
|
|
#endif
|
2024-04-23 22:29:49 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef EHS_LOG_SUCCESS
|
|
|
|
#define EHS_LOG_SUCCESS() ehs::Log::Raise({})
|
2024-02-05 22:25:30 -08:00
|
|
|
#endif
|