From f7cc01d294948c718e40917402eecda2b83f238e Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Mon, 8 Apr 2019 04:01:52 +0800 Subject: [PATCH] update Logger --- Easy2D/base/Object.cpp | 2 +- Easy2D/base/logs.cpp | 88 ++++++++++++++++++++++++++++++++ Easy2D/base/logs.h | 102 +++++++++++++++++++------------------- Easy2D/common/closure.hpp | 14 ++++++ 4 files changed, 153 insertions(+), 53 deletions(-) diff --git a/Easy2D/base/Object.cpp b/Easy2D/base/Object.cpp index 3dbe186f..b2d48c9c 100644 --- a/Easy2D/base/Object.cpp +++ b/Easy2D/base/Object.cpp @@ -110,7 +110,7 @@ namespace easy2d E2D_LOG(L"-------------------------- All Objects --------------------------"); for (const auto object : tracing_objects) { - E2D_LOG(object->DumpObject().c_str()); + E2D_LOG(L"%s", object->DumpObject().c_str()); } E2D_LOG(L"------------------------- Total size: %d -------------------------", tracing_objects.size()); } diff --git a/Easy2D/base/logs.cpp b/Easy2D/base/logs.cpp index 599aa21d..4d5ca245 100644 --- a/Easy2D/base/logs.cpp +++ b/Easy2D/base/logs.cpp @@ -113,4 +113,92 @@ namespace easy2d return error_stream_.rdbuf(buf); } + void Logger::Printf(const wchar_t* format, ...) + { + va_list args = nullptr; + va_start(args, format); + + Outputf(output_stream_, Logger::DefaultOutputColor, nullptr, format, args); + + va_end(args); + } + + void Logger::Messagef(const wchar_t* format, ...) + { + using namespace __console_colors; + + va_list args = nullptr; + va_start(args, format); + + Outputf(output_stream_, stdout_blue, nullptr, format, args); + + va_end(args); + } + + void Logger::Warningf(const wchar_t* format, ...) + { + using namespace __console_colors; + + va_list args = nullptr; + va_start(args, format); + + Outputf(output_stream_, stdout_yellow_bg, L" Warning:", format, args); + + va_end(args); + } + + void Logger::Errorf(const wchar_t* format, ...) + { + using namespace __console_colors; + + va_list args = nullptr; + va_start(args, format); + + Outputf(error_stream_, stderr_red_bg, L" Error:", format, args); + + va_end(args); + } + + void Logger::Outputf(std::wostream& os, std::wostream& (*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, va_list args) const + { + if (enabled_ && has_console_) + { + std::wstring output = MakeOutputString(prompt, format, args); + + os << color << output; + ::OutputDebugStringW(output.c_str()); + + ResetConsoleColor(); + } + } + + std::wstring Logger::MakeOutputStringf(const wchar_t* prompt, const wchar_t* format, va_list args) const + { + static wchar_t temp_buffer[1024 * 3 + 1]; + + std::wstringstream ss; + ss << Logger::OutPrefix; + + if (prompt) + ss << prompt; + + if (format) + { + const auto len = ::_vscwprintf(format, args) + 1; + ::_vsnwprintf_s(temp_buffer, len, len, format, args); + + ss << ' ' << temp_buffer; + } + return ss.str(); + } + + std::wostream& Logger::OutPrefix(std::wostream& out) + { + std::time_t unix = std::time(nullptr); + std::tm tmbuf; + localtime_s(&tmbuf, &unix); + out << std::put_time(&tmbuf, L"[easy2d] %H:%M:%S"); + return out; + } + } diff --git a/Easy2D/base/logs.h b/Easy2D/base/logs.h index 33e16a9d..99f4061e 100644 --- a/Easy2D/base/logs.h +++ b/Easy2D/base/logs.h @@ -27,18 +27,18 @@ #ifndef E2D_LOG # ifdef E2D_DEBUG -# define E2D_LOG(FORMAT, ...) easy2d::Logger::Instance().Messageln((FORMAT), __VA_ARGS__) +# define E2D_LOG(FORMAT, ...) easy2d::Logger::Instance().Messagef((FORMAT ## "\n"), __VA_ARGS__) # else # define E2D_LOG __noop # endif #endif #ifndef E2D_WARNING_LOG -# define E2D_WARNING_LOG(FORMAT, ...) easy2d::Logger::Instance().Warningln((FORMAT), __VA_ARGS__) +# define E2D_WARNING_LOG(FORMAT, ...) easy2d::Logger::Instance().Warningf((FORMAT ## "\n"), __VA_ARGS__) #endif #ifndef E2D_ERROR_LOG -# define E2D_ERROR_LOG(FORMAT, ...) easy2d::Logger::Instance().Errorln((FORMAT), __VA_ARGS__) +# define E2D_ERROR_LOG(FORMAT, ...) easy2d::Logger::Instance().Errorf((FORMAT ## "\n"), __VA_ARGS__) #endif namespace easy2d @@ -53,29 +53,37 @@ namespace easy2d void Disable(); - template - void Print(const wchar_t* format, _Args&&... args); + void Printf(const wchar_t* format, ...); + + void Messagef(const wchar_t * format, ...); + + void Warningf(const wchar_t* format, ...); + + void Errorf(const wchar_t* format, ...); template - void Println(const wchar_t* format, _Args&&... args); + void Print(_Args&& ... args); template - void Message(const wchar_t * format, _Args&&... args); + void Println(_Args&& ... args); template - void Messageln(const wchar_t * format, _Args&&... args); + void Message(_Args&& ... args); template - void Warning(const wchar_t* format, _Args&&... args); + void Messageln(_Args&& ... args); template - void Warningln(const wchar_t* format, _Args&&... args); + void Warning(_Args&& ... args); template - void Error(const wchar_t* format, _Args&&... args); + void Warningln(_Args&& ... args); template - void Errorln(const wchar_t* format, _Args&&... args); + void Error(_Args&& ... args); + + template + void Errorln(_Args&& ... args); std::wstreambuf* RedirectOutputStreamBuffer(std::wstreambuf* buf); @@ -86,14 +94,18 @@ namespace easy2d private: Logger(); - template - void OutputLine(std::wostream& os, std::wostream&(*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, _Args&&... args) const; + void Outputf(std::wostream& os, std::wostream&(*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, va_list args) const; + + std::wstring MakeOutputStringf(const wchar_t* prompt, const wchar_t* format, va_list args) const; template - void Output(std::wostream& os, std::wostream&(*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, _Args&&... args) const; + void OutputLine(std::wostream& os, std::wostream& (*color)(std::wostream&), const wchar_t* prompt, _Args&& ... args) const; template - std::wstring MakeOutputString(const wchar_t* prompt, const wchar_t* format, _Args&&... args) const; + void Output(std::wostream& os, std::wostream& (*color)(std::wostream&), const wchar_t* prompt, _Args&& ... args) const; + + template + std::wstring MakeOutputString(const wchar_t* prompt, _Args&& ... args) const; void ResetConsoleColor() const; @@ -155,65 +167,65 @@ namespace easy2d } template - inline void Logger::Print(const wchar_t* format, _Args&&... args) + inline void Logger::Print(_Args&& ... args) { - Output(output_stream_, Logger::DefaultOutputColor, nullptr, format, std::forward<_Args>(args)...); + Output(output_stream_, Logger::DefaultOutputColor, nullptr, std::forward<_Args>(args)...); } template - inline void Logger::Println(const wchar_t* format, _Args&&... args) + inline void Logger::Println(_Args&& ... args) { - OutputLine(output_stream_, Logger::DefaultOutputColor, nullptr, format, std::forward<_Args>(args)...); + OutputLine(output_stream_, Logger::DefaultOutputColor, nullptr, std::forward<_Args>(args)...); } template - inline void Logger::Message(const wchar_t * format, _Args&&... args) + inline void Logger::Message(_Args&& ... args) { using namespace __console_colors; - Output(output_stream_, stdout_blue, nullptr, format, std::forward<_Args>(args)...); + Output(output_stream_, stdout_blue, nullptr, std::forward<_Args>(args)...); } template - inline void Logger::Messageln(const wchar_t * format, _Args&&... args) + inline void Logger::Messageln(_Args&& ... args) { using namespace __console_colors; - OutputLine(output_stream_, stdout_blue, nullptr, format, std::forward<_Args>(args)...); + OutputLine(output_stream_, stdout_blue, nullptr, std::forward<_Args>(args)...); } template - inline void Logger::Warning(const wchar_t* format, _Args&&... args) + inline void Logger::Warning(_Args&& ... args) { using namespace __console_colors; - Output(output_stream_, stdout_yellow_bg, L"Warning: ", format, std::forward<_Args>(args)...); + Output(output_stream_, stdout_yellow_bg, L"Warning:", std::forward<_Args>(args)...); } template - inline void Logger::Warningln(const wchar_t* format, _Args&&... args) + inline void Logger::Warningln(_Args&& ... args) { using namespace __console_colors; - OutputLine(output_stream_, stdout_yellow_bg, L"Warning: ", format, std::forward<_Args>(args)...); + OutputLine(output_stream_, stdout_yellow_bg, L"Warning:", std::forward<_Args>(args)...); } template - inline void Logger::Error(const wchar_t* format, _Args&&... args) + inline void Logger::Error(_Args&& ... args) { using namespace __console_colors; - Output(error_stream_, stderr_red_bg, L"Error: ", format, std::forward<_Args>(args)...); + Output(error_stream_, stderr_red_bg, L"Error:", std::forward<_Args>(args)...); } template - inline void Logger::Errorln(const wchar_t* format, _Args&&... args) + inline void Logger::Errorln(_Args&& ... args) { using namespace __console_colors; - OutputLine(error_stream_, stderr_red_bg, L"Error: ", format, std::forward<_Args>(args)...); + OutputLine(error_stream_, stderr_red_bg, L"Error:", std::forward<_Args>(args)...); } template - inline void Logger::OutputLine(std::wostream& os, std::wostream&(*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, _Args&&... args) const + void Logger::OutputLine(std::wostream& os, std::wostream& (*color)(std::wostream&), const wchar_t* prompt, _Args&& ... args) const { if (enabled_ && has_console_) { - Output(os, color, prompt, format, std::forward<_Args>(args)...); + Output(os, color, prompt, std::forward<_Args>(args)...); os << std::endl; ::OutputDebugStringW(L"\r\n"); @@ -221,11 +233,11 @@ namespace easy2d } template - inline void Logger::Output(std::wostream& os, std::wostream&(*color)(std::wostream&), const wchar_t* prompt, const wchar_t* format, _Args&&... args) const + void Logger::Output(std::wostream& os, std::wostream& (*color)(std::wostream&), const wchar_t* prompt, _Args&& ... args) const { if (enabled_ && has_console_) { - std::wstring output = MakeOutputString(prompt, format, std::forward<_Args>(args)...); + std::wstring output = MakeOutputString(prompt, std::forward<_Args>(args)...); os << color << output; ::OutputDebugStringW(output.c_str()); @@ -235,20 +247,15 @@ namespace easy2d } template - inline std::wstring Logger::MakeOutputString(const wchar_t* prompt, const wchar_t* format, _Args&&... args) const + std::wstring Logger::MakeOutputString(const wchar_t* prompt, _Args&& ... args) const { - static wchar_t temp_buffer[1024 * 3 + 1]; - - const auto len = ::_scwprintf(format, std::forward<_Args>(args)...); - ::swprintf_s(temp_buffer, len + 1, format, std::forward<_Args>(args)...); - std::wstringstream ss; ss << Logger::OutPrefix; if (prompt) ss << prompt; - ss << temp_buffer; + (void)std::initializer_list{((ss << ' ' << args), 0)...}; return ss.str(); } @@ -264,15 +271,6 @@ namespace easy2d ::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), Logger::Instance().default_stdout_color_); return out; } - - inline std::wostream& Logger::OutPrefix(std::wostream& out) - { - std::time_t unix = std::time(nullptr); - std::tm tmbuf; - localtime_s(&tmbuf, &unix); - out << std::put_time(&tmbuf, L"[easy2d] %H:%M:%S "); - return out; - } } namespace easy2d diff --git a/Easy2D/common/closure.hpp b/Easy2D/common/closure.hpp index 87860b71..40b0fc30 100644 --- a/Easy2D/common/closure.hpp +++ b/Easy2D/common/closure.hpp @@ -70,6 +70,12 @@ namespace easy2d { return std::bind(_Func, _Ptr, std::_Ph<_Num + 1>()...); } + + template + static inline std::function<_Ret(_Args...)> MakeFunc(_Ty* _Ptr, _Ret(_Ty::* _Func)(_Args...) const, Seq<_Num...>) + { + return std::bind(_Func, _Ptr, std::_Ph<_Num + 1>()...); + } }; } @@ -80,4 +86,12 @@ namespace easy2d return ClosureHelper<_Ty, _Ret, _Args...>:: MakeFunc(_Ptr, _Func, typename Gen<_Args...>::SeqType{}); } + + template + inline std::function<_Ret(_Args...)> Closure(_Ty* _Ptr, _Ret(_Ty::*_Func)(_Args...) const) + { + using namespace __closure__detail; + return ClosureHelper<_Ty, _Ret, _Args...>:: + MakeFunc(_Ptr, _Func, typename Gen<_Args...>::SeqType{}); + } }