🛰️航天仿真算法库 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
39enum class ELogLevel
40{
41 eDebug,
42 eInfo,
43 eWarning,
44 eError,
45 eCritical,
46 eFatal
47};
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 : context_{ context }
85 , level_{ level }
86 , stream_{}
87 {}
88 LoggerStream(ELogLevel level, const char* fileName, int lineNumber, const char* functionName, const char* categoryName = nullptr)
89 : context_{ fileName, lineNumber, functionName, categoryName }
90 , level_{ level }
91 , stream_{}
92 {}
93 LoggerStream(const LoggerStream& other)
94 : context_{ other.context_ }
95 , level_{ other.level_ }
96 , stream_{}
97 {}
99 {
100 aLogMessage(level_, context_, stream_.str().c_str());
101 }
102 inline LoggerStream& space()
103 {
104 stream() << ' '; return *this;
105 }
106 inline LoggerStream& operator<<(bool t)
107 {
108 stream() << (t ? "true" : "false"); return space();
109 }
110 inline LoggerStream& operator<<(char t)
111 {
112 stream() << t; return space();
113 }
114 inline LoggerStream& operator<<(signed short t)
115 {
116 stream() << t; return space();
117 }
118 inline LoggerStream& operator<<(unsigned short t)
119 {
120 stream() << t; return space();
121 }
122 inline LoggerStream& operator<<(signed int t)
123 {
124 stream() << t; return space();
125 }
126 inline LoggerStream& operator<<(unsigned int t)
127 {
128 stream() << t; return space();
129 }
130 inline LoggerStream& operator<<(signed long t)
131 {
132 stream() << t; return space();
133 }
134 inline LoggerStream& operator<<(unsigned long t)
135 {
136 stream() << t; return space();
137 }
138 inline LoggerStream& operator<<(float t)
139 {
140 stream() << t; return space();
141 }
142 inline LoggerStream& operator<<(double t)
143 {
144 stream() << t; return space();
145 }
146 inline LoggerStream& operator<<(const char* t)
147 {
148 stream() << (t); return space();
149 }
150 inline LoggerStream& operator<<(const void* t)
151 {
152 stream() << t; return space();
153 }
154 inline std::ostringstream& stream()
155 {
156 return stream_;
157 }
158protected:
161 std::ostringstream stream_;
162};
163
165{
166public:
167 inline NoopStream& space()
168 {
169 return *this;
170 }
171 template<typename T>
172 inline NoopStream& operator<<(const T&)
173 {
174 return *this;
175 }
176};
177
178
180{
181public:
183 :context_{}
184 {}
185 MessageLogger(const char* file, int line, const char* function)
186 : context_{file, line, function}
187 {}
188 template<typename ...Args>
189 inline void debug(const char* msg, Args&& ...args) const
190 {
191 aLogMessage(ELogLevel::eDebug, context_, msg, std::forward<Args>(args)...);
192 };
193 template<typename ...Args>
194 inline void noDebug(const char*, Args&& ...args) const{}
195 template<typename ...Args>
196 void info(const char* msg, Args&& ...args) const
197 {
198 aLogMessage(ELogLevel::eInfo, context_, msg, std::forward<Args>(args)...);
199 }
200 template<typename ...Args>
201 inline void warning(const char* msg, Args&& ...args) const
202 {
203 aLogMessage(ELogLevel::eWarning, context_, msg, std::forward<Args>(args)...);
204 }
205 template<typename ...Args>
206 inline void error(const char* msg, Args&& ...args) const
207 {
208 aLogMessage(ELogLevel::eError, context_, msg, std::forward<Args>(args)...);
209 }
210 template<typename ...Args>
211 inline void critical(const char* msg, Args&& ...args) const
212 {
213 aLogMessage(ELogLevel::eCritical, context_, msg, std::forward<Args>(args)...);
214 }
215 template<typename ...Args>
216 inline void fatal(const char* msg, Args&& ...args) const
217 {
218 aLogMessage(ELogLevel::eFatal, context_, msg, std::forward<Args>(args)...);
219 }
220 inline LoggerStream debug() const{return LoggerStream(ELogLevel::eDebug, context_); }
221 inline NoopStream noDebug() const{ return NoopStream(); }
222 inline LoggerStream info() const{return LoggerStream(ELogLevel::eInfo, context_); }
223 inline LoggerStream warning() const{return LoggerStream(ELogLevel::eWarning, context_);}
224 inline LoggerStream error() const{return LoggerStream(ELogLevel::eError, context_);}
225 inline LoggerStream critical() const{return LoggerStream(ELogLevel::eCritical, context_); };
226 inline LoggerStream fatal() const{return LoggerStream(ELogLevel::eFatal, context_); };
227private:
228 MessageLogContext context_;
229};
230
234AST_NAMESPACE_END
235
236
237// 日志宏定义
238
239#ifdef NDEBUG
240#define aDebug(...) while(false) AST_PREPEND_NAMESPACE(MessageLogger)().noDebug(__VA_ARGS__)
241#else
242#define aDebug(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).debug(__VA_ARGS__)
243#endif
244#define aInfo(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).info(__VA_ARGS__)
245#define aWarning(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).warning(__VA_ARGS__)
246#define aError(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).error(__VA_ARGS__)
247#define aCritical(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).critical(__VA_ARGS__)
248#define aFatal(...) AST_PREPEND_NAMESPACE(MessageLogger)(__FILE__, __LINE__, __FUNCTION__).fatal(__VA_ARGS__)
249
250
251#define AST_CHECK_NULLPTR(variable) if(variable == nullptr){aError(#variable " is notset(nullptr)"); return eErrorNullPtr;}
252#define AST_CHECK_ERRCODE(rc, msg) if(rc){aError(msg); return rc;}
253#define AST_CHECK_INVALID(condition) if(condition){aError("invalid parameter, with " #condition); return eErrorInvalidParam;}
254
255
定义 Logger.hpp:81
MessageLogContext context_
上下文信息
定义 Logger.hpp:159
ELogLevel level_
等级
定义 Logger.hpp:160
std::ostringstream stream_
缓冲区
定义 Logger.hpp:161
日志消息的上下文信息
定义 Logger.hpp:54
定义 Logger.hpp:180
定义 Logger.hpp:165
ELogLevel
日志等级
定义 Logger.hpp:40
@ eWarning
警告信息
@ eDebug
调试信息
@ eInfo
一般信息
@ eFatal
致命错误
@ eCritical
严重错误
Unit T
特斯拉
定义 Unit.cpp:492