From d784fc6570dea8ce37ef37704ce5cec2e0e2a04e Mon Sep 17 00:00:00 2001 From: DevWiki Date: Tue, 8 Oct 2024 16:17:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=97=A5=E5=BF=97=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WinDevice/src/Utils/Log.cpp | 71 ++++++++++++++++++++++++--- WinDevice/src/Utils/Log.h | 23 ++++++++- WinDevice/src/Video/ScreenManager.cpp | 6 ++- WinDevice/src/main.cpp | 8 +-- 4 files changed, 93 insertions(+), 15 deletions(-) diff --git a/WinDevice/src/Utils/Log.cpp b/WinDevice/src/Utils/Log.cpp index 83be0a2..be845c2 100644 --- a/WinDevice/src/Utils/Log.cpp +++ b/WinDevice/src/Utils/Log.cpp @@ -1,20 +1,75 @@ #include "Log.h" -#include "spdlog/spdlog.h" -#include "spdlog/sinks/stdout_sinks.h" +#include +#include + +std::shared_ptr WinDevice::Log::logger = nullptr; void WinDevice::Log::Init(LogLevel level, std::string fileName) { - spdlog::stdout_logger_mt("console"); - spdlog::set_level(static_cast(level)); // 设置日志级别为 debug - spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); + logger = spdlog::stdout_color_mt("console"); + logger->set_level(static_cast(level)); + logger->set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); +// spdlog::set_level(static_cast(level)); // 设置日志级别为 debug +// spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); } -void WinDevice::Log::Info(std::string format, std::string args...) +void WinDevice::Log::debug(std::string format) { + logger->debug(format); } -void WinDevice::Log::WInfo(std::wstring format, std::wstring args...) +void WinDevice::Log::debug(std::string format, std::string args...) { - + logger->debug(format, args); +} + +void WinDevice::Log::debugW(std::wstring format, std::wstring args...) +{ + logger->debug(format, args); +} + +void WinDevice::Log::info(std::string format) +{ + logger->info(format); +} + +void WinDevice::Log::info(std::string format, std::string args...) +{ + logger->info(format, args); +} + +void WinDevice::Log::infoW(std::wstring format, std::wstring args...) +{ + logger->info(format, args); +} + +void WinDevice::Log::warn(std::string format) +{ + logger->warn(format); +} + +void WinDevice::Log::warn(std::string format, std::string args...) +{ + logger->warn(format, args); +} + +void WinDevice::Log::warnW(std::wstring format, std::wstring args...) +{ + logger->warn(format, args); +} + +void WinDevice::Log::error(std::string format) +{ + logger->error(format); +} + +void WinDevice::Log::error(std::string format, std::string args...) +{ + logger->error(format, args); +} + +void WinDevice::Log::errorW(std::wstring format, std::wstring args...) +{ + logger->error(format, args); } diff --git a/WinDevice/src/Utils/Log.h b/WinDevice/src/Utils/Log.h index 9d708a0..6206c09 100644 --- a/WinDevice/src/Utils/Log.h +++ b/WinDevice/src/Utils/Log.h @@ -1,5 +1,8 @@ #pragma once #include "spdlog/spdlog.h" +#include "spdlog/spdlog.h" +#include "spdlog/sinks/stdout_sinks.h" +#include #define LOG_FUNC_START() spdlog::info("=====> {0} start <=====", __FUNCTION__) #define LOG_FUNC_END() spdlog::info("=====> {0} end <=====", __FUNCTION__) @@ -16,8 +19,24 @@ namespace WinDevice { public: static void Init(LogLevel level, std::string fileName); - static void Info(std::string format, std::string args...); - static void WInfo(std::wstring format, std::wstring args...); + static void debug(std::string format); + static void debug(std::string format, std::string args...); + static void debugW(std::wstring format, std::wstring args...); + + static void info(std::string format); + static void info(std::string format, std::string args...); + static void infoW(std::wstring format, std::wstring args...); + + static void warn(std::string format); + static void warn(std::string format, std::string args...); + static void warnW(std::wstring format, std::wstring args...); + + static void error(std::string format); + static void error(std::string format, std::string args...); + static void errorW(std::wstring format, std::wstring args...); + + private: + static std::shared_ptr logger; }; } diff --git a/WinDevice/src/Video/ScreenManager.cpp b/WinDevice/src/Video/ScreenManager.cpp index b43ea4e..2bb8dc2 100644 --- a/WinDevice/src/Video/ScreenManager.cpp +++ b/WinDevice/src/Video/ScreenManager.cpp @@ -4,6 +4,8 @@ #include "../Utils/Log.h" #include +using namespace WinDevice; + WinDevice::ScreenManager::ScreenManager() { @@ -123,12 +125,12 @@ BOOL WinDevice::ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) } else { - spdlog::info("Failed to get display resolution."); + Log::error("Failed to get display resolution."); } } else { - spdlog::info("Failed to get display device info."); + spdlog::error("Failed to get display device info."); } return TRUE; } diff --git a/WinDevice/src/main.cpp b/WinDevice/src/main.cpp index 4fe9d64..81a5f76 100644 --- a/WinDevice/src/main.cpp +++ b/WinDevice/src/main.cpp @@ -5,6 +5,7 @@ #include #include "Video/ScreenManager.h" #include "Utils/PerformanceTool.h" +#include "Utils/Log.h" void getInfo() { @@ -20,9 +21,10 @@ void getInfo() { } int main() { -// WinDevice::ScreenManager screenManager; -// screenManager.UpdateDisplayInfo(); - getInfo(); + WinDevice::Log::Init(WinDevice::LogLevel::Debug, ""); + WinDevice::ScreenManager screenManager; + screenManager.UpdateDisplayInfo(); +// getInfo(); std::cout << "Press Enter to exit..." << std::endl; // 输出提示信息 std::cin.get(); // 等待用户输入 return 0;