* 日志打印改进
This commit is contained in:
@@ -13,6 +13,8 @@
|
||||
|
||||
#include "base/dl_range_any.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
//! 移除算法
|
||||
namespace dl::Erase
|
||||
{
|
||||
@@ -63,6 +65,21 @@ bool HeadSub(T& a, const T& b)
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 移除子串,所有
|
||||
* @retval 返回移除数量
|
||||
*/
|
||||
template<typename T>
|
||||
size_t AllSub(T& a, const T& b)
|
||||
{
|
||||
size_t i = 0;
|
||||
while (HeadSub(a, b))
|
||||
{
|
||||
++i;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 移除第一个找到的值
|
||||
* @param[in] container 操作的容器
|
||||
|
||||
+144
-21
@@ -1,42 +1,159 @@
|
||||
#include "dl_log.h"
|
||||
|
||||
#include <cassert>
|
||||
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#include "dl_time.h"
|
||||
#include "base/dl_range_erase.h"
|
||||
|
||||
namespace dl
|
||||
{
|
||||
LogSystem* g_log = nullptr;
|
||||
|
||||
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
||||
void log(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
|
||||
{
|
||||
const LogSystem::Setting& setting = g_log->GetSetting();
|
||||
std::string s;
|
||||
if (setting._bSourceLocation)
|
||||
{
|
||||
s += fmt::format("{}({}): ",
|
||||
location.file_name(), location.line());
|
||||
}
|
||||
s += LogSystem::Level2String(setting._level);
|
||||
s += ": ";
|
||||
s += str;
|
||||
if (setting._bStackTrace)
|
||||
{
|
||||
s += std::to_string(std::stacktrace::current());
|
||||
}
|
||||
std::cout << s << std::endl;
|
||||
|
||||
void Output(const std::string& str)
|
||||
{
|
||||
std::cout << str;
|
||||
|
||||
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
||||
// 转为wstring再输出
|
||||
DWORD len = MultiByteToWideChar(CP_UTF8, NULL, s.c_str(), -1, 0, 0);
|
||||
DWORD len = MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), -1, 0, 0);
|
||||
std::wstring wcs;
|
||||
wcs.resize(len);
|
||||
MultiByteToWideChar(CP_UTF8, NULL, s.c_str(), -1, wcs.data(), len);
|
||||
MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), -1, wcs.data(), len);
|
||||
wcs.pop_back();
|
||||
|
||||
OutputDebugStringW(wcs.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 SetColorBlank()
|
||||
{
|
||||
SetConsoleTextAttribute(g_log_handle,
|
||||
FOREGROUND_INTENSITY |
|
||||
BACKGROUND_INTENSITY);
|
||||
}
|
||||
/**
|
||||
* @brief 设置控制台 时间戳颜色
|
||||
*/
|
||||
void SetColorTime()
|
||||
{
|
||||
SetConsoleTextAttribute(g_log_handle,
|
||||
BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_BLUE |
|
||||
BACKGROUND_INTENSITY |
|
||||
FOREGROUND_INTENSITY);
|
||||
}
|
||||
|
||||
void log(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();
|
||||
std::string s = fmt::format("{}({})",
|
||||
location.file_name(), location.line());
|
||||
Output(fmt::format("{:_<32}", s));
|
||||
}
|
||||
// 时间戳
|
||||
if (setting._bTimeStamp)
|
||||
{
|
||||
SetColorTime();
|
||||
Output(fmt::format("|{}", Time::TimeStamp2String(time(0))));
|
||||
//SetColorBlank();
|
||||
}
|
||||
|
||||
// 等级、函数名、内容
|
||||
SetLevelColor(setting._level);
|
||||
std::string s;
|
||||
s += fmt::format("|{: <7}", LogSystem::Level2String(setting._level));
|
||||
{
|
||||
std::string s1 = location.function_name();
|
||||
using namespace std::literals;
|
||||
Erase::AllSub(s1, "void"s);
|
||||
Erase::AllSub(s1, "__cdecl"s);
|
||||
s += fmt::format("|{:_^24}", s1);
|
||||
}
|
||||
s += fmt::format("|{}", str);
|
||||
Output(s);
|
||||
OutputEndl();
|
||||
// 堆栈信息
|
||||
if (setting._bStackTrace)
|
||||
{
|
||||
Output(std::to_string(std::stacktrace::current()));
|
||||
OutputEndl();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,7 +224,13 @@ void log(std::string_view str)
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
LogSystem::LogSystem()
|
||||
{
|
||||
_stackSetting.push({});
|
||||
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
|
||||
g_log_handle = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
+2
-4
@@ -50,12 +50,10 @@ public:
|
||||
{
|
||||
Level _level = Level::INFO;
|
||||
bool _bSourceLocation = true;
|
||||
bool _bTimeStamp = true;
|
||||
bool _bStackTrace = false;
|
||||
};
|
||||
LogSystem()
|
||||
{
|
||||
_stackSetting.push({});
|
||||
}
|
||||
LogSystem();
|
||||
|
||||
//! 返回当前设置
|
||||
Setting& GetSetting()
|
||||
|
||||
Reference in New Issue
Block a user