Files
dl_old/dl/io/dl_log.cpp
T
2023-07-27 17:42:05 +08:00

238 lines
5.4 KiB
C++

#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;
void Output(const std::string& str)
{
std::cout << str;
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
// 转为wstring再输出
DWORD len = MultiByteToWideChar(CP_UTF8, NULL, str.c_str(), -1, 0, 0);
std::wstring wcs;
wcs.resize(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();
}
}
void log_notice0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::NOTICE);
log(str, location);
g_log->Pop();
}
void log_warn0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::WARN);
log(str, location);
g_log->Pop();
}
void log_err0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::ERR);
log(str, location);
g_log->Pop();
}
void log_damage0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::DAMAGE);
log(str, location);
g_log->Pop();
}
void log_pause0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::PAUSE);
log(str, location);
g_log->Pop();
}
void log_test0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::TEST);
log(str, location);
g_log->Pop();
}
void log_info0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::INFO);
log(str, location);
g_log->Pop();
}
void log_msg0(std::string_view str, const std::source_location location /*= std::source_location::current()*/)
{
g_log->PushLevel(LogSystem::Level::MSG);
log(str, location);
g_log->Pop();
}
#endif
#if (DL_PLATFORM == DL_PLATFORM_LINUX)
void log(std::string_view str)
{
std::cout << str << std::endl;
}
#endif
LogSystem::LogSystem()
{
_stackSetting.push({});
#if (DL_PLATFORM == DL_PLATFORM_WIN32)
g_log_handle = GetStdHandle(STD_OUTPUT_HANDLE);
#endif
}
}