🛰️航天仿真算法库 SpaceAST 0.0.1
载入中...
搜索中...
未找到
Logger.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include <iostream> // for std::cout
25#include <sstream> // for std::ostringstream
26#include <stdarg.h> // for va_list
27
28
29AST_NAMESPACE_BEGIN
30
48
49
50
51
54{
55public:
57 : line(0)
58 , file(nullptr)
59 , function(nullptr)
60 , category(nullptr)
61 {}
62 MessageLogContext(const char* fileName, int lineNumber, const char* functionName, const char* categoryName = nullptr)
63 : line(lineNumber)
64 , file(fileName)
65 , function(functionName)
66 , category(categoryName)
67 {}
68
69 int line;
70 const char* file;
71 const char* function;
72 const char* category;
73};
74
75
76AST_UTIL_CAPI void aLogMessageV(ELogLevel level, const MessageLogContext& context, const char* format, va_list ap);
77AST_UTIL_CAPI void aLogMessage(ELogLevel level, const MessageLogContext& context, const char* format, ...);
78
79
81{
82public:
83 LoggerStream(ELogLevel level, const MessageLogContext& context)
84 : m_context{ context }
85 , m_level{ level }
86 {}
87 LoggerStream(ELogLevel level, const char* fileName, int lineNumber, const char* functionName, const char* categoryName = nullptr)
88 :m_context{ fileName, lineNumber, functionName, categoryName }
89 , m_level{ level }
90 {}
91 LoggerStream(const LoggerStream& other)
92 :m_context{ other.m_context }
93 , m_level{ other.m_level }
94 {}
96 {
97 aLogMessage(m_level, m_context, m_stream.str().c_str());
98 }
99 inline LoggerStream& space()
100 {
101 stream() << ' '; return *this;
102 }
103 inline LoggerStream& operator<<(bool t)
104 {
105 stream() << (t ? "true" : "false"); return space();
106 }
107 inline LoggerStream& operator<<(char t)
108 {
109 stream() << t; return space();
110 }
111 inline LoggerStream& operator<<(signed short t)
112 {
113 stream() << t; return space();
114 }
115 inline LoggerStream& operator<<(unsigned short t)
116 {
117 stream() << t; return space();
118 }
119 inline LoggerStream& operator<<(signed int t)
120 {
121 stream() << t; return space();
122 }
123 inline LoggerStream& operator<<(unsigned int t)
124 {
125 stream() << t; return space();
126 }
127 inline LoggerStream& operator<<(signed long t)
128 {
129 stream() << t; return space();
130 }
131 inline LoggerStream& operator<<(unsigned long t)
132 {
133 stream() << t; return space();
134 }
135 inline LoggerStream& operator<<(float t)
136 {
137 stream() << t; return space();
138 }
139 inline LoggerStream& operator<<(double t)
140 {
141 stream() << t; return space();
142 }
143 inline LoggerStream& operator<<(const char* t)
144 {
145 stream() << (t); return space();
146 }
147 inline LoggerStream& operator<<(const void* t)
148 {
149 stream() << t; return space();
150 }
151 inline std::ostringstream& stream()
152 {
153 return m_stream;
154 }
155protected:
158 std::ostringstream m_stream;
159};
160
162{
163public:
164 inline NoopStream& space()
165 {
166 return *this;
167 }
168 template<typename T>
169 inline NoopStream& operator<<(const T&)
170 {
171 return *this;
172 }
173};
174
175
177{
178public:
180 :m_context{}
181 {}
182 MessageLogger(const char* file, int line, const char* function)
183 : m_context{file, line, function}
184 {}
185 template<typename ...Args>
186 inline void debug(const char* msg, Args&& ...args) const
187 {
188 aLogMessage(eDebug, m_context, msg, std::forward<Args>(args)...);
189 };
190 template<typename ...Args>
191 inline void noDebug(const char*, Args&& ...args) const{}
192 template<typename ...Args>
193 void info(const char* msg, Args&& ...args) const
194 {
195 aLogMessage(eInfo, m_context, msg, std::forward<Args>(args)...);
196 }
197 template<typename ...Args>
198 inline void warning(const char* msg, Args&& ...args) const
199 {
200 aLogMessage(eWarning, m_context, msg, std::forward<Args>(args)...);
201 }
202 template<typename ...Args>
203 inline void error(const char* msg, Args&& ...args) const
204 {
205 aLogMessage(eError, m_context, msg, std::forward<Args>(args)...);
206 }
207 template<typename ...Args>
208 inline void critical(const char* msg, Args&& ...args) const
209 {
210 aLogMessage(eCritical, m_context, msg, std::forward<Args>(args)...);
211 }
212 template<typename ...Args>
213 inline void fatal(const char* msg, Args&& ...args) const
214 {
215 aLogMessage(eFatal, m_context, msg, std::forward<Args>(args)...);
216 }
217 inline LoggerStream debug() const{return LoggerStream(eDebug, m_context); }
218 inline NoopStream noDebug() const{ return NoopStream(); }
219 inline LoggerStream info() const{return LoggerStream(eInfo, m_context); }
220 inline LoggerStream warning() const{return LoggerStream(eWarning, m_context);}
221 inline LoggerStream error() const{return LoggerStream(eError, m_context);}
222 inline LoggerStream critical() const{return LoggerStream(eCritical, m_context); };
223 inline LoggerStream fatal() const{return LoggerStream(eFatal, m_context); };
224private:
225 MessageLogContext m_context;
226};
227
231AST_NAMESPACE_END
232
233
234// 日志宏定义
235
236#ifdef NDEBUG
237#define aDebug(...) while(false) AST_PREPEND_NAMESPACE(MessageLogger)().noDebug(__VA_ARGS__)
238#else
239#define aDebug(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).debug(__VA_ARGS__)
240#endif
241#define aInfo(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).info(__VA_ARGS__)
242#define aWarning(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).warning(__VA_ARGS__)
243#define aError(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).error(__VA_ARGS__)
244#define aCritical(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).critical(__VA_ARGS__)
245#define aFatal(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).fatal(__VA_ARGS__)
246
247
248#define AST_CHECK_NULLPTR(variable) if(variable == nullptr){aError(#variable " is notset(nullptr)"); return eErrorNullPtr;}
249#define AST_CHECK_ERRCODE(rc, msg) if(rc){aError(msg); return rc;}
250#define AST_CHECK_INVALID(condition) if(condition){aError("invalid parameter, with " #condition); return eErrorInvalidParam;}
251
252
定义 Logger.hpp:81
std::ostringstream m_stream
缓冲区
定义 Logger.hpp:158
ELogLevel m_level
等级
定义 Logger.hpp:157
MessageLogContext m_context
上下文信息
定义 Logger.hpp:156
日志消息的上下文信息
定义 Logger.hpp:54
const char * file
文件
定义 Logger.hpp:70
const char * category
分类
定义 Logger.hpp:72
const char * function
函数
定义 Logger.hpp:71
int line
行数
定义 Logger.hpp:69
定义 Logger.hpp:177
定义 Logger.hpp:162
ELogLevel
日志等级
定义 Logger.hpp:40
@ eCritical
严重错误
定义 Logger.hpp:45
@ eDebug
调试信息
定义 Logger.hpp:41
@ eFatal
致命错误
定义 Logger.hpp:46
@ eWarning
警告信息
定义 Logger.hpp:43
@ eInfo
一般信息
定义 Logger.hpp:42
@ eError
错误信息
定义 Logger.hpp:44