Files
dl_old/dl/io/dl_log.cpp
T
2023-12-23 16:39:54 +08:00

308 lines
7.3 KiB
C++

#include "dl_log.h"
#include <cassert>
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <vld.h>
#endif
#include "system/dl_time.h"
#include "base/dl_range_erase.h"
#include "memory/dl_code_cvt.h"
namespace dl
{
LogSystem* g_log = nullptr;
void Output(const std::string& str)
{
std::cout << str;
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
OutputDebugStringW(CodeCvt::Utf8ToWideChar(str).c_str());
#endif
}
void OutputEndl()
{
std::cout << std::endl;
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
OutputDebugStringW(L"\n");
#endif
}
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
HANDLE g_log_handle = INVALID_HANDLE_VALUE;
/**
* @brief 根据日志等级,设置控制台颜色
*/
void SetLevelColor(LogSystem::Level level)
{
switch (level)
{
case LogSystem::Level::PAUSE:
SetConsoleTextAttribute(g_log_handle,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE |
FOREGROUND_INTENSITY);
break;
case LogSystem::Level::TEST:
SetConsoleTextAttribute(g_log_handle,
FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE |
BACKGROUND_BLUE |
FOREGROUND_INTENSITY);
break;
case LogSystem::Level::INFO:
SetConsoleTextAttribute(g_log_handle,
BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE |
BACKGROUND_INTENSITY);
break;
case LogSystem::Level::MSG:
SetConsoleTextAttribute(g_log_handle,
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
BACKGROUND_RED | BACKGROUND_BLUE |
FOREGROUND_INTENSITY);
break;
case LogSystem::Level::NOTICE:
SetConsoleTextAttribute(g_log_handle,
BACKGROUND_GREEN |
BACKGROUND_INTENSITY);
break;
case LogSystem::Level::WARN:
SetConsoleTextAttribute(g_log_handle,
FOREGROUND_GREEN | FOREGROUND_RED |
BACKGROUND_GREEN | BACKGROUND_RED |
FOREGROUND_INTENSITY);
break;
case LogSystem::Level::ERR:
SetConsoleTextAttribute(g_log_handle,
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |
BACKGROUND_RED);
break;
case LogSystem::Level::DAMAGE:
SetConsoleTextAttribute(g_log_handle,
BACKGROUND_RED |
BACKGROUND_INTENSITY);
break;
}
}
/**
* @brief 设置控制台 时间戳颜色
*/
void SetColorTime()
{
SetConsoleTextAttribute(g_log_handle,
BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE |
BACKGROUND_INTENSITY |
FOREGROUND_INTENSITY);
}
/**
* @brief 设置控制台空格颜色
*/
void SetColorBlank()
{
SetColorTime();
//SetConsoleTextAttribute(g_log_handle,
// FOREGROUND_INTENSITY);
// //BACKGROUND_INTENSITY);
}
void log_imp(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
const LogSystem::Setting& setting = g_log->GetSetting();
// 源码位置 vs双击定位 需要输出到最前面
if (setting._bSourceLocation)
{
SetColorTime();
// 此种格式才能在vs里跳转
std::string s = fmt::format("{}({}):",
location.file_name(), location.line());
Output(fmt::format("{:.<48}", s));
}
// 时间戳
if (setting._bTimeStamp)
{
SetColorTime();
Output(fmt::format("|{}", Time::TimeStamp2String(time(0))));
//SetColorBlank();
}
// 等级、函数名、内容
SetLevelColor(setting._level);
bool short_str = str.size() < 48;
std::string s;
s += fmt::format("|{:<7}", LogSystem::Level2String(setting._level));
if(short_str)
s += fmt::format("|{:<48}", str);
else
s += fmt::format("|{:.^48}", "");
{
std::string s1 = location.function_name();
using namespace std::literals;
if (Erase::TailFromLast(s1, '('))
s1.pop_back();
Erase::AllSub(s1, "void"s);
Erase::AllSub(s1, "__cdecl"s);
s += fmt::format("|{:^48}", s1);
}
// 长字符串单独输出到一行
if (!short_str)
s += fmt::format("\n\n{:<48}\n", str);
Output(s);
SetColorBlank();
OutputEndl();
// 堆栈信息
if (setting._bStackTrace)
{
Output(std::to_string(std::stacktrace::current()));
OutputEndl();
}
if (g_log->IsLevelMsgBox(setting._level))
{
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
std::string title = fmt::format("{}({}) {} {}",
location.file_name(), location.line(),
Time::TimeStamp2String(time(0)), LogSystem::Level2String(setting._level));
int ret = MessageBoxW(NULL, CodeCvt::Utf8ToWideChar(str).c_str(),
CodeCvt::Utf8ToWideChar(title).c_str(), MB_ABORTRETRYIGNORE | MB_ICONERROR | MB_DEFBUTTON3);
if (ret == IDABORT)
exit(1);
else if (ret == IDRETRY)
__debugbreak();
else if (ret == IDIGNORE)
;
else
assert(0);
#endif
}
}
#define DL_LOG_OUTPUT(level)\
if (g_log->IsLevelLimit(level))\
return;\
g_log->PushLevel(level);\
log_imp(str, location);\
g_log->Pop()
void log_notice0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::NOTICE);
}
void log_warn0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::WARN);
}
void log_err0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::ERR);
}
void log_damage0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::DAMAGE);
}
void log_pause0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::PAUSE);
}
void log_test0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::TEST);
}
void log_info0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::INFO);
}
void log_msg0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
DL_LOG_OUTPUT(LogSystem::Level::MSG);
}
void log_err0(const std::exception& e, const std::source_location location /*= std::source_location::current()*/)
{
if (g_log->IsLevelLimit(LogSystem::Level::ERR))
return;
g_log->PushLevel(LogSystem::Level::ERR);
log_imp("捕获到C++异常:" + CodeCvt::MultiByteToUtf8(e.what()), location);
g_log->Pop();
}
void log(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
if (g_log->IsLevelLimit(g_log->GetSetting()._level))
return;
log_imp(str, location);
}
#endif
#if (DL_PLATFORM == DL_PLATFORM_LINUX)
void log(std::string_view str)
{
std::cout << str << std::endl;
}
void log_err0(const std::exception& e)
{
if (g_log->IsLevelLimit(LogSystem::Level::ERR))
return;
g_log->PushLevel(LogSystem::Level::ERR);
log("捕获到C++异常:" + CodeCvt::MultiByteToUtf8(e.what()));
g_log->Pop();
}
#endif
#if (DL_PLATFORM == DL_PLATFORM_ANDROID)
#include <android/log.h>
void log(std::string_view s)
{
int prio = ANDROID_LOG_INFO;
const char* tag = "TS";
const char* fmt = "%s";
const char* str = s.data();
__android_log_print(prio, tag, fmt, str);
// std::cout << str << std::endl;
}
void log_err0(const std::exception& e)
{
if (g_log->IsLevelLimit(LogSystem::Level::ERR))
return;
g_log->PushLevel(LogSystem::Level::ERR);
log("捕获到C++异常:" + CodeCvt::MultiByteToUtf8(e.what()));
g_log->Pop();
}
#endif
LogSystem::LogSystem() :
_levelLimit{ Level::PAUSE },
_levelMsgBox{ Level::ERR }
{
_stackSetting.push({});
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
g_log_handle = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
}
}