| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | // Copyright (c) 2016-2018 Kiwano - Nomango
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | // 
 | 
					
						
							|  |  |  |  | // Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					
						
							|  |  |  |  | // of this software and associated documentation files (the "Software"), to deal
 | 
					
						
							|  |  |  |  | // in the Software without restriction, including without limitation the rights
 | 
					
						
							|  |  |  |  | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
					
						
							|  |  |  |  | // copies of the Software, and to permit persons to whom the Software is
 | 
					
						
							|  |  |  |  | // furnished to do so, subject to the following conditions:
 | 
					
						
							|  |  |  |  | // 
 | 
					
						
							|  |  |  |  | // The above copyright notice and this permission notice shall be included in
 | 
					
						
							|  |  |  |  | // all copies or substantial portions of the Software.
 | 
					
						
							|  |  |  |  | // 
 | 
					
						
							|  |  |  |  | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
					
						
							|  |  |  |  | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
					
						
							|  |  |  |  | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
					
						
							|  |  |  |  | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
					
						
							|  |  |  |  | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
					
						
							|  |  |  |  | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
					
						
							|  |  |  |  | // THE SOFTWARE.
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #pragma once
 | 
					
						
							|  |  |  |  | #include "../macros.h"
 | 
					
						
							| 
									
										
										
										
											2019-08-13 21:16:38 +08:00
										 |  |  |  | #include "../core/core.h"
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | #include <ctime>
 | 
					
						
							|  |  |  |  | #include <iomanip>
 | 
					
						
							|  |  |  |  | #include <sstream>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | #ifndef KGE_LOG
 | 
					
						
							|  |  |  |  | #	ifdef KGE_DEBUG
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:52:49 +08:00
										 |  |  |  | #		define KGE_LOG(FORMAT, ...) ::kiwano::Logger::GetInstance()->Messagef((FORMAT ## "\n"), __VA_ARGS__)
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | #	else
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | #		define KGE_LOG __noop
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | #	endif
 | 
					
						
							|  |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | #ifndef KGE_WARNING_LOG
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:52:49 +08:00
										 |  |  |  | #	define KGE_WARNING_LOG(FORMAT, ...) ::kiwano::Logger::GetInstance()->Warningf((FORMAT ## "\n"), __VA_ARGS__)
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | #ifndef KGE_ERROR_LOG
 | 
					
						
							| 
									
										
										
										
											2019-08-14 21:52:49 +08:00
										 |  |  |  | #	define KGE_ERROR_LOG(FORMAT, ...) ::kiwano::Logger::GetInstance()->Errorf((FORMAT ## "\n"), __VA_ARGS__)
 | 
					
						
							|  |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #ifndef KGE_PRINT
 | 
					
						
							|  |  |  |  | #	define KGE_PRINT(...) ::kiwano::Logger::GetInstance()->Println(__VA_ARGS__)
 | 
					
						
							|  |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #ifndef KGE_PRINTF
 | 
					
						
							|  |  |  |  | #	define KGE_PRINTF(FORMAT, ...) ::kiwano::Logger::GetInstance()->Printf((FORMAT), __VA_ARGS__)
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 	class KGE_API Logger | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		: public Singleton<Logger> | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 		KGE_DECLARE_SINGLETON(Logger); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 	public: | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  |  | 		// <20><>ʾ<EFBFBD><CABE><EFBFBD>رտ<D8B1><D5BF><EFBFBD>̨
 | 
					
						
							|  |  |  |  | 		void ShowConsole(bool show); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		// <20><><EFBFBD><EFBFBD> Logger
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		void Enable(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  |  | 		// <20><><EFBFBD><EFBFBD> Logger
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		void Disable(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void Printf(const WChar* format, ...); | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void Messagef(const WChar * format, ...); | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void Warningf(const WChar* format, ...); | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void Errorf(const WChar* format, ...); | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Print(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Println(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Message(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Messageln(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Warning(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Warningln(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Error(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		void Errorln(_Args&& ... args); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		std::wstreambuf* RedirectOutputStreamBuffer(std::wstreambuf* buf); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		std::wstreambuf* RedirectErrorStreamBuffer(std::wstreambuf* buf); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void ResetOutputStream(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	private: | 
					
						
							|  |  |  |  | 		Logger(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-24 13:33:19 +08:00
										 |  |  |  | 		~Logger(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void Outputf(std::wostream& os, std::wostream&(*color)(std::wostream&), const WChar* prompt, const WChar* format, va_list args) const; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		std::wstring MakeOutputStringf(const WChar* prompt, const WChar* format, va_list args) const; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void OutputLine(std::wostream& os, std::wostream& (*color)(std::wostream&), const WChar* prompt, _Args&& ... args) const; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		void Output(std::wostream& os, std::wostream& (*color)(std::wostream&), const WChar* prompt, _Args&& ... args) const; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		std::wstring MakeOutputString(const WChar* prompt, _Args&& ... args) const; | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 		void ResetConsoleColor() const; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		static std::wostream& DefaultOutputColor(std::wostream& out); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		static std::wostream& OutPrefix(std::wostream& out); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	private: | 
					
						
							|  |  |  |  | 		bool enabled_; | 
					
						
							|  |  |  |  | 		WORD default_stdout_color_; | 
					
						
							|  |  |  |  | 		WORD default_stderr_color_; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		std::wostream output_stream_; | 
					
						
							|  |  |  |  | 		std::wostream error_stream_; | 
					
						
							|  |  |  |  | 	}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	//
 | 
					
						
							|  |  |  |  | 	// details of Logger
 | 
					
						
							|  |  |  |  | 	//
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	namespace __console_colors | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | #define DECLARE_COLOR(COLOR)\
 | 
					
						
							|  |  |  |  | 		extern std::wostream&(stdout_##COLOR)(std::wostream&);\ | 
					
						
							|  |  |  |  | 		extern std::wostream&(stderr_##COLOR)(std::wostream&); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define DECLARE_BG_COLOR(COLOR)\
 | 
					
						
							|  |  |  |  | 		extern std::wostream&(stdout_##COLOR##_bg)(std::wostream&);\ | 
					
						
							|  |  |  |  | 		extern std::wostream&(stderr_##COLOR##_bg)(std::wostream&); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		DECLARE_COLOR(red); | 
					
						
							|  |  |  |  | 		DECLARE_COLOR(green); | 
					
						
							|  |  |  |  | 		DECLARE_COLOR(yellow); | 
					
						
							|  |  |  |  | 		DECLARE_COLOR(blue); | 
					
						
							|  |  |  |  | 		DECLARE_COLOR(white); | 
					
						
							|  |  |  |  | 		DECLARE_COLOR(reset); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 		DECLARE_BG_COLOR(red); | 
					
						
							|  |  |  |  | 		DECLARE_BG_COLOR(green); | 
					
						
							|  |  |  |  | 		DECLARE_BG_COLOR(yellow); | 
					
						
							|  |  |  |  | 		DECLARE_BG_COLOR(blue); | 
					
						
							|  |  |  |  | 		DECLARE_BG_COLOR(white); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #undef DECLARE_COLOR
 | 
					
						
							|  |  |  |  | #undef DECLARE_BG_COLOR
 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	inline void Logger::Enable() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		enabled_ = true; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	inline void Logger::Disable() | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		enabled_ = false; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Print(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		Output(output_stream_, Logger::DefaultOutputColor, nullptr, std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Println(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		OutputLine(output_stream_, Logger::DefaultOutputColor, nullptr, std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Message(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		using namespace __console_colors; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		Output(output_stream_, stdout_blue, nullptr, std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Messageln(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		using namespace __console_colors; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		OutputLine(output_stream_, stdout_blue, nullptr, std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Warning(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		using namespace __console_colors; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		Output(output_stream_, stdout_yellow_bg, L"Warning:", std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Warningln(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		using namespace __console_colors; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		OutputLine(output_stream_, stdout_yellow_bg, L"Warning:", std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Error(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		using namespace __console_colors; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		Output(error_stream_, stderr_red_bg, L"Error:", std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 	inline void Logger::Errorln(_Args&& ... args) | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		using namespace __console_colors; | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 		OutputLine(error_stream_, stderr_red_bg, L"Error:", std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 	void Logger::OutputLine(std::wostream& os, std::wostream& (*color)(std::wostream&), const WChar* prompt, _Args&& ... args) const | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-22 13:21:12 +08:00
										 |  |  |  | 		if (enabled_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 			Output(os, color, prompt, std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 			os << std::endl; | 
					
						
							|  |  |  |  | 			::OutputDebugStringW(L"\r\n"); | 
					
						
							|  |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 	void Logger::Output(std::wostream& os, std::wostream& (*color)(std::wostream&), const WChar* prompt, _Args&& ... args) const | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-04-22 13:21:12 +08:00
										 |  |  |  | 		if (enabled_) | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-08 04:01:52 +08:00
										 |  |  |  | 			std::wstring output = MakeOutputString(prompt, std::forward<_Args>(args)...); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-08 14:15:27 +08:00
										 |  |  |  | 			os << color << output << std::flush; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			::OutputDebugStringW(output.c_str()); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 			ResetConsoleColor(); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	template <typename ..._Args> | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 	std::wstring Logger::MakeOutputString(const WChar* prompt, _Args&& ... args) const | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		std::wstringstream ss; | 
					
						
							|  |  |  |  | 		ss << Logger::OutPrefix; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		if (prompt) | 
					
						
							|  |  |  |  | 			ss << prompt; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 22:49:44 +08:00
										 |  |  |  | 		(void)std::initializer_list<Int32>{((ss << ' ' << args), 0)...}; | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		return ss.str(); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	inline void Logger::ResetConsoleColor() const | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), default_stdout_color_); | 
					
						
							|  |  |  |  | 		::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), default_stderr_color_); | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 	inline std::wostream& Logger::DefaultOutputColor(std::wostream& out) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2019-08-13 15:00:43 +08:00
										 |  |  |  | 		::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), Logger::GetInstance()->default_stdout_color_); | 
					
						
							| 
									
										
										
										
											2019-03-31 13:08:59 +08:00
										 |  |  |  | 		return out; | 
					
						
							|  |  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 13:21:12 +08:00
										 |  |  |  | //
 | 
					
						
							|  |  |  |  | // Display stack trace on exception
 | 
					
						
							|  |  |  |  | //
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "../third-party/StackWalker/StackWalker.h"
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | namespace kiwano | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | { | 
					
						
							|  |  |  |  | 	inline void ThrowIfFailed(HRESULT hr) | 
					
						
							|  |  |  |  | 	{ | 
					
						
							|  |  |  |  | 		if (FAILED(hr)) | 
					
						
							|  |  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2019-04-11 14:40:54 +08:00
										 |  |  |  | 			KGE_ERROR_LOG(L"Fatal error with HRESULT of %08X", hr); | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-22 13:21:12 +08:00
										 |  |  |  | 			StackWalker{}.ShowCallstack(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-31 01:37:06 +08:00
										 |  |  |  | 			static char buffer[1024 + 1]; | 
					
						
							|  |  |  |  | 			sprintf_s(buffer, "Fatal error with HRESULT of %08X", hr); | 
					
						
							|  |  |  |  | 			throw std::runtime_error(buffer); | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } |