201 lines
5.6 KiB
C++
201 lines
5.6 KiB
C++
/**
|
|
* @file dl_log.h
|
|
* @brief 日志系统
|
|
*
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 23-06-22
|
|
*
|
|
* @note
|
|
*/
|
|
#pragma once
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
#include <stack>
|
|
#include <array>
|
|
|
|
#include "system/dl_platform.h"
|
|
|
|
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
|
#include <stacktrace>
|
|
#include <source_location>
|
|
#endif
|
|
|
|
#include <fmt/format.h>
|
|
|
|
namespace dl
|
|
{
|
|
constexpr std::array<std::string_view, 9> DL_LOG_SYSTEM_LEVLE_NAME = {
|
|
"damage", "err", "warn", "notice", "msg", "info", "test", "pause",
|
|
};
|
|
|
|
|
|
class LogSystem
|
|
{
|
|
public:
|
|
//! 日志等级 越小越严重
|
|
enum class Level
|
|
{
|
|
DAMAGE,
|
|
ERR,
|
|
WARN,
|
|
NOTICE,
|
|
MSG,
|
|
INFO,
|
|
TEST,
|
|
PAUSE,
|
|
};
|
|
struct Setting
|
|
{
|
|
Level _level = Level::INFO;
|
|
bool _bSourceLocation = true;
|
|
bool _bTimeStamp = true;
|
|
bool _bStackTrace = false;
|
|
};
|
|
LogSystem();
|
|
|
|
//! 返回当前设置
|
|
Setting& GetSetting()
|
|
{
|
|
return _stackSetting.top();
|
|
}
|
|
|
|
//! 设置日志输出等级(大于自己不输出)
|
|
void SetLevelLimit(Level level) { _levelLimit = level; }
|
|
Level GetLevelLimit() { return _levelLimit; }
|
|
|
|
//! 检查等级是否被限制输出
|
|
bool IsLevelLimit(Level level)
|
|
{
|
|
return level > _levelLimit;
|
|
}
|
|
|
|
//! 检查等级是否可以弹窗(小于等于自己)
|
|
bool IsLevelMsgBox(Level level)
|
|
{
|
|
return level <= _levelMsgBox;
|
|
}
|
|
|
|
//! 入栈设置
|
|
void Push(const Setting& setting)
|
|
{
|
|
_stackSetting.push(setting);
|
|
}
|
|
|
|
//! 入栈设置
|
|
void PushLevel(Level level)
|
|
{
|
|
Setting setting = GetSetting();
|
|
setting._level = level;
|
|
_stackSetting.push(setting);
|
|
}
|
|
|
|
//! 出栈设置
|
|
void Pop()
|
|
{
|
|
_stackSetting.pop();
|
|
}
|
|
|
|
/**
|
|
* @brief 日志等级转字符串
|
|
*/
|
|
static std::string_view Level2String(Level level)
|
|
{
|
|
size_t index = (size_t)level;
|
|
if (index >= DL_LOG_SYSTEM_LEVLE_NAME.size())
|
|
return "NONE";
|
|
return DL_LOG_SYSTEM_LEVLE_NAME[index];
|
|
}
|
|
private:
|
|
std::stack<Setting> _stackSetting;
|
|
Level _levelLimit; // 限制等级
|
|
Level _levelMsgBox;// 弹窗等级
|
|
};
|
|
extern LogSystem* g_log;
|
|
|
|
class LogLimit
|
|
{
|
|
public:
|
|
//! 默认为最高等级,用于屏蔽消息输出
|
|
LogLimit(LogSystem::Level level = LogSystem::Level::DAMAGE)
|
|
{
|
|
_levelPre = g_log->GetLevelLimit();
|
|
g_log->SetLevelLimit(level);
|
|
}
|
|
~LogLimit()
|
|
{
|
|
g_log->SetLevelLimit(_levelPre);
|
|
}
|
|
private:
|
|
LogSystem::Level _levelPre;
|
|
};
|
|
|
|
/**
|
|
* @brief 输出日志
|
|
*/
|
|
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
|
//void log_imp(std::string_view str, const std::source_location location = std::source_location::current());
|
|
|
|
|
|
void log_pause0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_test0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_info0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_msg0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_notice0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_warn0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_err0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
void log_damage0(std::string_view str, const std::source_location location = std::source_location::current());
|
|
|
|
//! 以当前设置输出
|
|
void log(std::string_view str, const std::source_location location = std::source_location::current());
|
|
|
|
void log_err0(const std::exception& e, const std::source_location location = std::source_location::current());
|
|
|
|
#define log_(str, ...) log(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_pause(str, ...) log_pause0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_test(str, ...) log_test0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_info(str, ...) log_info0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_msg(str, ...) log_msg0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_notice(str, ...) log_notice0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_warn(str, ...) log_warn0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_err(str, ...) log_err0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
#define log_damage(str, ...) log_damage0(fmt::format(str, __VA_ARGS__), std::source_location::current())
|
|
|
|
#endif
|
|
|
|
#if (DL_PLATFORM == DL_PLATFORM_LINUX) || (DL_PLATFORM == DL_PLATFORM_ANDROID)
|
|
|
|
void log(std::string_view str);
|
|
|
|
inline void log_pause0(std::string_view str) { log(str); }
|
|
inline void log_test0(std::string_view str) { log(str); }
|
|
inline void log_info0(std::string_view str) { log(str); }
|
|
inline void log_msg0(std::string_view str) { log(str); }
|
|
inline void log_notice0(std::string_view str) { log(str); }
|
|
inline void log_warn0(std::string_view str) { log(str); }
|
|
inline void log_err0(std::string_view str) { log(str); }
|
|
inline void log_damage0(std::string_view str) { log(str); }
|
|
|
|
|
|
void log_err0(const std::exception& e);
|
|
|
|
#define log_(str, ...) log(fmt::format(str, __VA_ARGS__))
|
|
#define log_pause(str, ...) log_pause0(fmt::format(str, __VA_ARGS__))
|
|
#define log_test(str, ...) log_test0(fmt::format(str, __VA_ARGS__))
|
|
#define log_info(str, ...) log_info0(fmt::format(str, __VA_ARGS__))
|
|
#define log_msg(str, ...) log_msg0(fmt::format(str, __VA_ARGS__))
|
|
#define log_notice(str, ...) log_notice0(fmt::format(str, __VA_ARGS__))
|
|
#define log_warn(str, ...) log_warn0(fmt::format(str, __VA_ARGS__))
|
|
#define log_err(str, ...) log_err0(fmt::format(str, __VA_ARGS__))
|
|
#define log_damage(str, ...) log_damage0(fmt::format(str, __VA_ARGS__))
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |