2019-04-11 14:40:54 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
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:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// 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
|
2020-01-17 16:55:47 +08:00
|
|
|
|
#include <kiwano/core/Common.h>
|
2020-05-17 20:15:58 +08:00
|
|
|
|
#include <iomanip>
|
|
|
|
|
|
#include <streambuf>
|
2019-10-10 15:09:38 +08:00
|
|
|
|
|
2019-12-25 18:07:57 +08:00
|
|
|
|
#ifndef KGE_SYS_LOG
|
2020-01-21 10:09:55 +08:00
|
|
|
|
#ifdef KGE_DEBUG
|
|
|
|
|
|
#define KGE_SYS_LOG(FORMAT, ...) \
|
2020-02-08 09:59:17 +08:00
|
|
|
|
::kiwano::Logger::GetInstance().Printf(::kiwano::Logger::Level::System, FORMAT, __VA_ARGS__)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
#else
|
|
|
|
|
|
#define KGE_SYS_LOG __noop
|
|
|
|
|
|
#endif
|
2019-03-31 01:37:06 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-12-17 20:47:55 +08:00
|
|
|
|
#ifndef KGE_WARN
|
2020-02-08 09:59:17 +08:00
|
|
|
|
#define KGE_WARN(FORMAT, ...) ::kiwano::Logger::GetInstance().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
|
2020-02-08 09:59:17 +08:00
|
|
|
|
#define KGE_ERROR(FORMAT, ...) ::kiwano::Logger::GetInstance().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
|
2020-02-08 09:59:17 +08:00
|
|
|
|
#define KGE_LOG(...) ::kiwano::Logger::GetInstance().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
|
2020-02-08 09:59:17 +08:00
|
|
|
|
#define KGE_LOGF(FORMAT, ...) ::kiwano::Logger::GetInstance().Printf(::kiwano::Logger::Level::Info, FORMAT, __VA_ARGS__)
|
2019-03-31 01:37:06 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
2020-05-17 20:15:58 +08:00
|
|
|
|
#ifndef KGE_LOG_STREAM
|
|
|
|
|
|
#define KGE_LOG_STREAM() ::kiwano::Logger::GetInstance().GetOutputStream()
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef KGE_ERROR_STREAM
|
|
|
|
|
|
#define KGE_ERROR_STREAM() ::kiwano::Logger::GetInstance().GetErrorStream()
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
* @brief <EFBFBD><EFBFBD>־
|
2020-01-21 10:09:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
class KGE_API Logger : public Singleton<Logger>
|
|
|
|
|
|
{
|
|
|
|
|
|
friend Singleton<Logger>;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
enum class Level
|
|
|
|
|
|
{
|
2020-03-19 14:28:50 +08:00
|
|
|
|
Info, ///< <20><>Ϣ
|
|
|
|
|
|
System, ///< ϵͳ
|
|
|
|
|
|
Warning, ///< <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
Error ///< <20><><EFBFBD><EFBFBD>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ӡ<EFBFBD><D3A1>־
|
|
|
|
|
|
/// @param level <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param format <20><>ʽ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
2020-02-10 13:47:00 +08:00
|
|
|
|
void Printf(Level level, const char* format, ...);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ӡ<EFBFBD><D3A1>־
|
|
|
|
|
|
/// @param level <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param args <20><><EFBFBD><EFBFBD>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
template <typename... _Args>
|
|
|
|
|
|
void Print(Level level, _Args&&... args);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ӡһ<D3A1><D2BB><EFBFBD><EFBFBD>־
|
|
|
|
|
|
/// @param level <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param args <20><><EFBFBD><EFBFBD>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
template <typename... _Args>
|
|
|
|
|
|
void Println(Level level, _Args&&... args);
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Enable();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void Disable();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-10 13:47:00 +08:00
|
|
|
|
OutputStream& GetOutputStream();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-04-01 15:19:19 +08:00
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-10 13:47:00 +08:00
|
|
|
|
OutputStream& GetErrorStream();
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-04-01 15:19:19 +08:00
|
|
|
|
std::streambuf* RedirectOutputStream(std::streambuf* buf);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-04-01 15:19:19 +08:00
|
|
|
|
/// @brief <20>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
std::streambuf* RedirectErrorStream(std::streambuf* buf);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-04-01 15:19:19 +08:00
|
|
|
|
/// @brief <20><>ʾ<EFBFBD><CABE><EFBFBD>رտ<D8B1><D5BF><EFBFBD>̨
|
|
|
|
|
|
/// @note <20>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
void ShowConsole(bool show);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
Logger();
|
|
|
|
|
|
|
|
|
|
|
|
~Logger();
|
|
|
|
|
|
|
|
|
|
|
|
void Prepare(Level level, StringStream& sstream);
|
|
|
|
|
|
|
|
|
|
|
|
void Output(Level level, StringStream& sstream);
|
|
|
|
|
|
|
2020-04-01 15:19:19 +08:00
|
|
|
|
void ResetStreamToStdStream();
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
void ResetConsoleColor() const;
|
|
|
|
|
|
|
|
|
|
|
|
OutputStream& DefaultOutputColor(OutputStream& out);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
bool enabled_;
|
|
|
|
|
|
WORD default_stdout_color_;
|
|
|
|
|
|
WORD default_stderr_color_;
|
|
|
|
|
|
|
|
|
|
|
|
OutputStream output_stream_;
|
|
|
|
|
|
OutputStream error_stream_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
inline void Logger::Enable()
|
|
|
|
|
|
{
|
|
|
|
|
|
enabled_ = true;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
inline void Logger::Disable()
|
|
|
|
|
|
{
|
|
|
|
|
|
enabled_ = false;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
template <typename... _Args>
|
|
|
|
|
|
void Logger::Print(Level level, _Args&&... args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!enabled_)
|
|
|
|
|
|
return;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
StringStream sstream;
|
|
|
|
|
|
Prepare(level, sstream);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
// Format message
|
|
|
|
|
|
(void)std::initializer_list<int>{ ((sstream << ' ' << args), 0)... };
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Output(level, sstream);
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
template <typename... _Args>
|
|
|
|
|
|
void Logger::Println(Level level, _Args&&... args)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!enabled_)
|
|
|
|
|
|
return;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
StringStream sstream;
|
|
|
|
|
|
Prepare(level, sstream);
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
// Format message
|
|
|
|
|
|
(void)std::initializer_list<int>{ ((sstream << ' ' << args), 0)... };
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
|
sstream << "\r\n";
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Output(level, sstream);
|
|
|
|
|
|
}
|
2019-12-17 20:47:55 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
inline void Logger::ResetConsoleColor() const
|
|
|
|
|
|
{
|
|
|
|
|
|
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), default_stdout_color_);
|
|
|
|
|
|
::SetConsoleTextAttribute(::GetStdHandle(STD_ERROR_HANDLE), default_stderr_color_);
|
|
|
|
|
|
}
|
2019-12-17 20:47:55 +08:00
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
|
inline OutputStream& Logger::DefaultOutputColor(OutputStream& out)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), default_stdout_color_);
|
|
|
|
|
|
return out;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
|
inline OutputStream& Logger::GetOutputStream()
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return output_stream_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-10 13:47:00 +08:00
|
|
|
|
inline OutputStream& Logger::GetErrorStream()
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return error_stream_;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
} // namespace kiwano
|