26#include <kiwano/core/Common.h>
27#include <kiwano/core/Time.h>
28#include <kiwano/base/ObjectBase.h>
32#define KGE_DEBUG_LOG(...) ::kiwano::Logger::GetInstance().Log(::kiwano::LogLevel::Debug, __VA_ARGS__)
34#define KGE_DEBUG_LOG __noop
39#define KGE_LOG(...) ::kiwano::Logger::GetInstance().Log(::kiwano::LogLevel::Info, __VA_ARGS__)
43#define KGE_NOTICE(...) ::kiwano::Logger::GetInstance().Log(::kiwano::LogLevel::Notice, __VA_ARGS__)
47#define KGE_WARN(...) ::kiwano::Logger::GetInstance().Log(::kiwano::LogLevel::Warning, __VA_ARGS__)
51#define KGE_ERROR(...) ::kiwano::Logger::GetInstance().Log(::kiwano::LogLevel::Error, __VA_ARGS__)
56#define KGE_DEBUG_LOGF(FORMAT, ...) ::kiwano::Logger::GetInstance().Logf(::kiwano::LogLevel::Debug, FORMAT, __VA_ARGS__)
58#define KGE_DEBUG_LOGF __noop
63#define KGE_LOGF(FORMAT, ...) ::kiwano::Logger::GetInstance().Logf(::kiwano::LogLevel::Info, FORMAT, __VA_ARGS__)
67#define KGE_NOTICEF(FORMAT, ...) ::kiwano::Logger::GetInstance().Logf(::kiwano::LogLevel::Notice, FORMAT, __VA_ARGS__)
71#define KGE_WARNF(FORMAT, ...) ::kiwano::Logger::GetInstance().Logf(::kiwano::LogLevel::Warning, FORMAT, __VA_ARGS__)
75#define KGE_ERRORF(FORMAT, ...) ::kiwano::Logger::GetInstance().Logf(::kiwano::LogLevel::Error, FORMAT, __VA_ARGS__)
79#define KGE_THROW(MESSAGE) \
82 KGE_ERRORF("An exception occurred: %s", MESSAGE); \
83 kiwano::StackTracer().Print(); \
84 throw kiwano::RuntimeError(MESSAGE); \
89#define KGE_THROW_IF(EXPRESSION, MESSAGE) \
96#ifndef KGE_THROW_SYSTEM_ERROR
97#define KGE_THROW_SYSTEM_ERROR(ERRCODE, MESSAGE) \
100 KGE_ERRORF("An exception occurred (%#x): %s", ERRCODE, MESSAGE); \
101 kiwano::StackTracer().Print(); \
102 throw kiwano::SystemError(std::error_code(kiwano::error_enum(ERRCODE)), MESSAGE); \
106#ifdef KGE_PLATFORM_WINDOWS
107#ifndef KGE_THROW_IF_FAILED
108#define KGE_THROW_IF_FAILED(HR, MESSAGE) \
111 KGE_THROW_SYSTEM_ERROR(HR, MESSAGE); \
120KGE_DECLARE_SMART_PTR(LogFormater);
121KGE_DECLARE_SMART_PTR(LogProvider);
143 virtual void FormatHeader(std::ostream& out, LogLevel level,
ClockTime time) = 0;
145 virtual void FormatFooter(std::ostream& out, LogLevel level) = 0;
147 String GetLevelLabel(LogLevel level)
const;
159 void Resize(
size_t size);
163 const char* GetRaw()
const;
170 int_type overflow(int_type ch)
override;
172 int_type underflow()
override;
174 pos_type seekpos(pos_type sp, std::ios_base::openmode which)
override;
176 pos_type seekoff(off_type off, std::ios_base::seekdir dir,
177 std::ios_base::openmode which = std::ios_base::in)
override;
180 Vector<char_type> buf_;
181 char_type* seek_high_;
195 virtual void Flush();
197 void Write(LogLevel level,
const char* msg);
199 void SetLevel(LogLevel level);
204 virtual void WriteMessage(LogLevel level,
const char* msg) = 0;
221 void Init()
override;
223 void Flush()
override;
226 void WriteMessage(LogLevel level,
const char* msg)
override;
229 typedef std::ostream&(*ConsoleColor)(std::ostream&);
231 ConsoleColor GetColor(LogLevel level);
241 FileLogProvider(
const String& filepath, std::ios_base::openmode mode = std::ios_base::out);
245 void Init()
override;
247 void Flush()
override;
250 void WriteMessage(LogLevel level,
const char* msg)
override;
269 void Logf(LogLevel level,
const char* format, ...);
275 template <
typename... _Args>
276 void Log(LogLevel level, _Args&&... args);
292 void SetLevel(LogLevel level);
297 void AddProvider(LogProviderPtr provider);
302 void SetFormater(LogFormaterPtr formater);
307 LogFormaterPtr GetFormater();
312 void ResizeBuffer(
size_t buffer_size);
316 void ShowConsole(
bool show);
323 std::iostream& GetFormatedStream(LogLevel level,
LogBuffer* buffer);
325 void WriteToProviders(LogLevel level,
LogBuffer* buffer);
330 LogFormaterPtr formater_;
332 std::iostream stream_;
333 Vector<LogProviderPtr> providers_;
349 formater_ = formater;
352template <
typename... _Args>
361 std::lock_guard<std::mutex> lock(mutex_);
364 auto& stream = this->GetFormatedStream(level, &this->buffer_);
365 (void)std::initializer_list<int>{ ((stream <<
' ' << args), 0)... };
368 this->WriteToProviders(level, &this->buffer_);
控制台日志生产者
Definition: Logger.h:215
文件日志生产者
Definition: Logger.h:239
日志流缓冲
Definition: Logger.h:155
日志生产者
Definition: Logger.h:189
日志记录器
Definition: Logger.h:261
void Enable()
启用日志
Definition: Logger.h:337
void SetFormater(LogFormaterPtr formater)
设置日志格式
Definition: Logger.h:347
void Disable()
禁用日志
Definition: Logger.h:342
void Log(LogLevel level, _Args &&... args)
打印日志
Definition: Logger.h:353
基础对象
Definition: ObjectBase.h:137
Definition: Singleton.h:28
时钟时间
Definition: Time.h:74