YSTest: Debug.h 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

Debug.h
浏览该文件的文档.
1 /*
2 © 2011-2015 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YCL_INC_Debug_h_
29 #define YCL_INC_Debug_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YCLib_YCommon
33 #include YFM_YCLib_Container // for string_view, string, std::ostream,
34 // platform::sfmt;
35 #include YFM_YCLib_Mutex
36
64 #ifndef NDEBUG
65 # ifndef YCL_Use_TraceSrc
66 # define YCL_Use_TraceSrc 1
67 # endif
68 #endif
69
70
71 namespace platform
72 {
73
78
79
80
86 class YF_API Logger
87 {
88 public:
89 using Level = Descriptions::RecordLevel;
90 using Filter = std::function<bool(Level, Logger&)>;
92 using Sender = std::function<void(Level, Logger&, const char*)>;
93
94 #ifdef NDEBUG
95 Level FilterLevel = Descriptions::Informative;
96 #else
97 Level FilterLevel = Descriptions::Debug;
98 #endif
99
100 private:
102 Filter filter{DefaultFilter};
104 Sender sender{FetchDefaultSender()};
112 Concurrency::recursive_mutex record_mutex;
113
114 public:
116 DefGetter(const ynothrow, const Sender&, Sender, sender)
117
118
122 void
123 SetFilter(Filter);
129 void
130 SetSender(Sender);
131
137 template<typename _func>
138 auto
139 AccessRecord(_func f) -> decltype(f())
140 {
141 Concurrency::lock_guard<Concurrency::recursive_mutex> lck(record_mutex);
142
143 return f();
144 }
145
147 static bool
148 DefaultFilter(Level, Logger&) ynothrow;
149
151
152
156 static YB_NONNULL(3) void
157 DefaultSendLog(Level, Logger&, const char*) ynothrowv;
158
163 static YB_NONNULL(3) void
164 DefaultSendLogToFile(Level, Logger&, const char*) ynothrowv;
166
173 void
174 DoLog(Level, const char*);
176 PDefH(void, DoLog, Level lv, string_view sv)
177 ImplRet(DoLog(lv, sv.data()))
179
180 private:
187 YB_NONNULL(1) void
188 DoLogRaw(Level, const char*);
190 PDefH(void, DoLogRaw, Level lv, string_view sv)
191 ImplRet(DoLogRaw(lv, sv.data()))
193
194 public:
201 void
202 DoLogException(Level level, const std::exception&) ynothrow;
203
209 static Sender
210 FetchDefaultSender(string_view = "YFramework");
211
212 template<typename _fCaller, typename... _tParams>
213 void
214 Log(Level level, _fCaller&& f, _tParams&&... args)
215 {
216 if(filter(level, *this))
217 TryExpr(DoLog(level, yforward(f)(yforward(args)...)))
218 CatchExpr(std::exception& e, DoLogException(level, e), throw)
219 }
220
227 static YB_NONNULL(4) void
228 SendLog(std::ostream&, Level, Logger&, const char*) ynothrowv;
229
231 static YB_NONNULL(1, 4) void
232 SendLogToFile(std::FILE*, Level, Logger&, const char*) ynothrowv;
234 };
235
236
241 YF_API Logger&
242 FetchCommonLogger();
243
244
252 YF_API YB_ATTR_gnu_printf(3, 4) YB_NONNULL(1, 3) string
253 LogWithSource(const char*, int, const char*, ...) ynothrow;
254
255
262 #define YCL_Log(_lv, ...) \
263 platform::FetchCommonLogger().Log( \
264 platform::Descriptions::RecordLevel(_lv), __VA_ARGS__)
265
271 #define YCL_TraceRaw(_lv, ...) \
272 YCL_Log(_lv, [&]() -> platform::string { \
273 using platform::sfmt; \
274 \
275 TryRet(sfmt(__VA_ARGS__)) \
276 CatchRet(..., {}) \
277 })
278
285 #if YCL_Use_TraceSrc
286 # define YCL_Trace(_lv, ...) \
287 YCL_Log(_lv, [&]{ \
288 return platform::LogWithSource(__FILE__, __LINE__, __VA_ARGS__); \
289 })
290 #else
291 # define YCL_Trace(_lv, ...) YCL_TraceRaw(_lv, __VA_ARGS__)
292 #endif
293
294
302 #if YB_Use_YTrace
303 # define YTraceDe(_lv, ...) YCL_Trace(_lv, __VA_ARGS__)
304 #else
305 # define YTraceDe(...)
306 #endif
307
308 } // namespace platform;
309
310 namespace platform_ex
311 {
312
313 #if YF_Multithread == 1
314
329 YF_API void
330 LogAssert(const char*, const char*, int, const char*) ynothrow;
331
332 # if YB_Use_YAssert
333 # undef YAssert
334 # define YAssert(_expr, _msg) \
336 ((_expr) ? void(0) \
337 : platform_ex::LogAssert(#_expr, __FILE__, __LINE__, _msg))
338 # endif
339 #endif
340
341 #if YCL_Win32
342
348 YF_API YB_NONNULL(3) void
349 SendDebugString(platform::Logger::Level, platform::Logger&, const char*)
350 ynothrowv;
351 #elif YCL_Android
352
363 YF_API int
364 MapAndroidLogLevel(platform::Descriptions::RecordLevel);
365
366
372 class YF_API AndroidLogSender
373 {
374 public:
375 using Logger = platform::Logger;
376 using Level = Logger::Level;
377
378 private:
379 string tag;
380
381 public:
386 AndroidLogSender(string_view);
388 DefDeCopyMoveCtorAssignment(AndroidLogSender)
389 ~AndroidLogSender();
390
392 void
393 operator()(Level, Logger&, const char*) const;
394
395 DefGetter(const ynothrow, const string&, Tag, tag)
396 };
397 #endif
398
399 } // namespace platform_ex;
400
401 namespace platform
402 {
403
409 template<typename _type>
410 inline _type&&
411 Nonnull(_type&& p) ynothrow
412 {
413 YAssertNonnull(p);
414 return yforward(p);
415 }
416
422 template<typename _type>
423 inline _type&&
424 CheckIter(_type&& i) ynothrow
425 {
426 using ystdex::is_undereferenceable;
427
428 YAssert(!is_undereferenceable(i), "Invalid iterator found.");
429 return yforward(i);
430 }
431
438 template<typename _type>
439 yconstfn auto
440 Deref(_type&& p) -> decltype(*p)
441 {
442 return *CheckIter(yforward(p));
443 }
444
445 } // namespace platform;
446
447 #endif
448
platform::p
while p p
Definition: YCLib/FileSystem.h:225
YSLib::lv
RecordLevel lv
Definition: YException.cpp:58
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
platform::Nonnull
_type && Nonnull(_type &&p) ynothrow
断言并返回非空指针。
Definition: Debug.h:411
platform::Descriptions::Informative
Definition: YCommon.h:159
platform::Logger::Level
Descriptions::RecordLevel Level
Definition: Debug.h:89
DefDeCopyMoveCtorAssignment
#define DefDeCopyMoveCtorAssignment(_t)
Definition: YBaseMacro.h:201
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:105
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
platform::Logger::Filter
std::function< bool(Level, Logger &)> Filter
Definition: Debug.h:90
platform::YB_ATTR_gnu_printf
YF_API YB_ATTR_gnu_printf(3, 4) YB_NONNULL(1
格式输出日志字符串前追加记录源文件名和行号。
platform::LogWithSource
YF_API string LogWithSource(const char *, int, const char *,...) ynothrow
Definition: Debug.cpp:232
platform
默认平台命名空间。
Definition: Container.h:47
platform::FetchCommonLogger
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
Definition: Debug.cpp:223
platform::Logger::Sender
std::function< void(Level, Logger &, const char *)> Sender
Definition: Debug.h:92
std
Definition: memory.hpp:1100
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:440
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:114
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:303
platform_ex
平台扩展命名空间。
Definition: Android.h:45
platform::Logger
日志记录器。
Definition: Debug.h:86
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
YModules.h
YSLib::i
return i
Definition: YCoreUtilities.h:176
platform::ImplRet
const string &s ImplRet(s) YF_API string MakePathString(const char16_t *)
platform::Descriptions::Debug
Definition: YCommon.h:160
ystdex::single_thread::lock_guard
Definition: pseudo_mutex.h:197
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
YSLib::level
YF_API size_t level
Definition: YException.h:141
platform::YB_NONNULL
YF_API YB_NONNULL(1) int uaccess(const char *path
测试路径可访问性。
Definition: YCLib/FileSystem.h:144
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:969
platform::ynothrowv
YF_API int amode ynothrowv
Definition: FileIO.h:491
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:151
YF_API
#define YF_API
Definition: Platform.h:56
ystdex::single_thread::recursive_mutex
Definition: pseudo_mutex.h:88
YSLib::IO::f
const string &path const String &path _func f
Definition: YSLib/Service/FileSystem.h:430
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
platform::CheckIter
_type && CheckIter(_type &&i) ynothrow
检查迭代器。
Definition: Debug.h:424
platform::DefGetter
yconstfn Integer &MaskTrait::AMask MaskTrait::XYZMaskN yconstfn DefGetter(const ynothrow, typename Trait::BType, B, Integer &MaskTrait::XMask) yconstfn DefGetter(const ynothrow
platform::Logger::record_mutex
Concurrency::recursive_mutex record_mutex
日志记录锁。
Definition: Debug.h:112
platform::PDefH
PDefH(string, MakePathString, const char *s) ImplRet(Nonnull(s)) inline PDefH(const string &
构造适合表示路径的字符串。