EHS/include/Log.h

116 lines
3.7 KiB
C++

#pragma once
#include <initializer_list>
#include "Types.h"
#include "Array.h"
#include "UTF.h"
#include "Str.h"
namespace ehs
{
/// 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:
static void (*logCb)(const Log&);
static Log lastLog;
Array<Str_8> tags;
UInt_64 code;
Str_8 msg;
public:
static void SetCallback(void (*newLogCb)(const Log&));
static void Raise(const Log& log);
/// Retrieves the last log raised.
static Log GetLastLog();
/// 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.
Log(std::initializer_list<Str_8> tags, const UInt_64 code, const Str_8& msg);
/// 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.
Log(Array<Str_8>& tags, const UInt_64 code, const Str_8& msg);
/// Copies all members from the given log.
/// @param [in] log The log to copy from.
Log(const Log& log);
/// Copies all members from the given log.
/// @param [in] log The log to copy from.
/// @returns The log that has been assigned to.
Log& 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);
/// 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);
*/
/// 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.
bool HasTags(const std::initializer_list<Str_8> tags) const;
/// 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.
bool HasTags(const Array<Str_8>& tags) const;
/// 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.
bool HasTag(const Str_8& tag) const;
/// Retrieves all the tags.
/// @returns The result.
Array<Str_8> GetTags() const;
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
#define EHS_LOG_INT(type, code, msg) Log::Raise({{type, GetAcronym_8(), EHS_FILE, EHS_FUNC, Str_8::FromNum((UInt_32)EHS_LINE)}, code, msg})
#else
#define EHS_LOG_INT(type, code, msg) Log::Raise({{type, GetAcronym_8(), EHS_FUNC}, code, msg})
#endif
#endif
#ifndef EHS_LOG
#ifdef EHS_DEBUG
#define EHS_LOG(type, code, msg) lwe::Log::Raise({{type, lwe::GetAppName_8(), EHS_FILE, EHS_FUNC, lwe::Str_8::FromNum((lwe::UInt_32)EHS_LINE)}, code, msg})
#else
#define EHS_LOG(type, code, msg) lwe::Log::Raise({{type, lwe::GetAppName_8(), EHS_FUNC}, code, msg})
#endif
#endif