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 <iomanip>
|
|
|
|
|
|
#include <sstream>
|
2019-10-11 21:55:29 +08:00
|
|
|
|
#include <kiwano/macros.h>
|
2019-12-23 18:05:08 +08:00
|
|
|
|
#include <kiwano/core/common.h>
|
2019-10-10 15:09:38 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
#ifndef KGE_SYS_LOG
|
2019-04-11 14:40:54 +08:00
|
|
|
|
# ifdef KGE_DEBUG
|
2019-12-25 18:07:57 +08:00
|
|
|
|
# define KGE_SYS_LOG(FORMAT, ...) ::kiwano::Logger::instance().Printf(::kiwano::Logger::Level::System, FORMAT, __VA_ARGS__)
|
2019-03-31 01:37:06 +08:00
|
|
|
|
# else
|
2019-12-25 18:07:57 +08:00
|
|
|
|
# define KGE_SYS_LOG __noop
|
2019-03-31 01:37:06 +08:00
|
|
|
|
# endif
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-12-17 20:47:55 +08:00
|
|
|
|
#ifndef KGE_WARN
|
2019-12-25 18:07:57 +08:00
|
|
|
|
# define KGE_WARN(FORMAT, ...) ::kiwano::Logger::instance().Printf(::kiwano::Logger::Level::Warning, FORMAT, __VA_ARGS__)
|
2019-03-31 01:37:06 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-12-17 20:47:55 +08:00
|
|
|
|
#ifndef KGE_ERROR
|
2019-12-25 18:07:57 +08:00
|
|
|
|
# define KGE_ERROR(FORMAT, ...) ::kiwano::Logger::instance().Printf(::kiwano::Logger::Level::Error, FORMAT, __VA_ARGS__)
|
2019-08-14 21:52:49 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
#ifndef KGE_LOG
|
|
|
|
|
|
# define KGE_LOG(...) ::kiwano::Logger::instance().Println(::kiwano::Logger::Level::Info, __VA_ARGS__)
|
2019-08-14 21:52:49 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
#ifndef KGE_LOGF
|
|
|
|
|
|
# define KGE_LOGF(FORMAT, ...) ::kiwano::Logger::instance().Printf(::kiwano::Logger::Level::Info, 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-12-25 18:07:57 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
|
|
|
|
|
* @brief <EFBFBD><EFBFBD>־
|
|
|
|
|
|
*/
|
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-12-20 17:49:16 +08:00
|
|
|
|
friend Singleton<Logger>;
|
2019-03-31 13:08:59 +08:00
|
|
|
|
|
|
|
|
|
|
public:
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
enum class Level
|
|
|
|
|
|
{
|
|
|
|
|
|
Info, ///< <20><>Ϣ
|
|
|
|
|
|
System, ///< ϵͳ
|
|
|
|
|
|
Warning, ///< <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
Error ///< <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
using OutputStream = std::wostream;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>̨<EFBFBD><CCA8>ɫ
|
|
|
|
|
|
using ConsoleColor = Function<OutputStream& (OutputStream&)>;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ӡ<EFBFBD><D3A1>־
|
|
|
|
|
|
/// @param level <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param format <20><>ʽ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|
|
|
|
|
void Printf(Level level, const wchar_t* format, ...);
|
2019-03-31 13:08:59 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ӡ<EFBFBD><D3A1>־
|
|
|
|
|
|
/// @param level <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param args <20><><EFBFBD><EFBFBD>
|
2019-03-31 13:08:59 +08:00
|
|
|
|
template <typename ..._Args>
|
2019-12-25 18:07:57 +08:00
|
|
|
|
void Print(Level level, _Args&& ... args);
|
2019-03-31 13:08:59 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD>־
|
|
|
|
|
|
/// @param level <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param args <20><><EFBFBD><EFBFBD>
|
2019-03-31 13:08:59 +08:00
|
|
|
|
template <typename ..._Args>
|
2019-12-25 18:07:57 +08:00
|
|
|
|
void Println(Level level, _Args&& ... args);
|
2019-03-31 13:08:59 +08:00
|
|
|
|
|
2019-12-31 10:37:29 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ʾ<EFBFBD><CABE><EFBFBD>رտ<D8B1><D5BF><EFBFBD>̨
|
|
|
|
|
|
/// @note <20>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void ShowConsole(bool show);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|
|
|
|
|
void Enable();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
|
|
|
|
|
void Disable();
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-12-17 20:47:55 +08:00
|
|
|
|
std::wostream& GetOutputStream();
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-12-17 20:47:55 +08:00
|
|
|
|
std::wostream& GetErrorStream();
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-31 13:08:59 +08:00
|
|
|
|
std::wstreambuf* RedirectOutputStreamBuffer(std::wstreambuf* buf);
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-31 13:08:59 +08:00
|
|
|
|
std::wstreambuf* RedirectErrorStreamBuffer(std::wstreambuf* buf);
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
/// \~chinese
|
|
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2019-03-31 13:08:59 +08:00
|
|
|
|
void ResetOutputStream();
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
Logger();
|
|
|
|
|
|
|
2019-04-24 13:33:19 +08:00
|
|
|
|
~Logger();
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
void Prepare(Level level, StringStream& sstream);
|
2019-09-29 22:23:13 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
void Output(Level level, StringStream& sstream);
|
2019-09-29 22:23:13 +08:00
|
|
|
|
|
2019-03-31 13:08:59 +08:00
|
|
|
|
void ResetConsoleColor() const;
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
OutputStream& DefaultOutputColor(OutputStream& out);
|
2019-03-31 13:08:59 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
bool enabled_;
|
|
|
|
|
|
WORD default_stdout_color_;
|
|
|
|
|
|
WORD default_stderr_color_;
|
|
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
OutputStream output_stream_;
|
|
|
|
|
|
OutputStream error_stream_;
|
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-12-25 18:07:57 +08:00
|
|
|
|
void Logger::Print(Level level, _Args&& ... args)
|
2019-03-31 13:08:59 +08:00
|
|
|
|
{
|
2019-12-25 18:07:57 +08:00
|
|
|
|
if (!enabled_)
|
|
|
|
|
|
return;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
StringStream sstream;
|
|
|
|
|
|
Prepare(level, sstream);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
// Format message
|
|
|
|
|
|
(void)std::initializer_list<int>{((sstream << ' ' << args), 0)...};
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
Output(level, sstream);
|
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-12-25 18:07:57 +08:00
|
|
|
|
void Logger::Println(Level level, _Args&& ... args)
|
2019-03-31 13:08:59 +08:00
|
|
|
|
{
|
2019-12-25 18:07:57 +08:00
|
|
|
|
if (!enabled_)
|
|
|
|
|
|
return;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
StringStream sstream;
|
|
|
|
|
|
Prepare(level, sstream);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
// Format message
|
|
|
|
|
|
(void)std::initializer_list<int>{((sstream << ' ' << args), 0)...};
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
sstream << L"\r\n";
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
Output(level, sstream);
|
2019-03-31 13:08:59 +08:00
|
|
|
|
}
|
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-12-25 18:07:57 +08:00
|
|
|
|
inline Logger::OutputStream& Logger::DefaultOutputColor(OutputStream& out)
|
2019-03-31 13:08:59 +08:00
|
|
|
|
{
|
2019-12-25 18:07:57 +08:00
|
|
|
|
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), default_stdout_color_);
|
2019-03-31 13:08:59 +08:00
|
|
|
|
return out;
|
|
|
|
|
|
}
|
2019-12-17 20:47:55 +08:00
|
|
|
|
|
|
|
|
|
|
inline std::wostream& Logger::GetOutputStream()
|
|
|
|
|
|
{
|
|
|
|
|
|
return output_stream_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline std::wostream& Logger::GetErrorStream()
|
|
|
|
|
|
{
|
|
|
|
|
|
return error_stream_;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
}
|