YSTest: Debug.h 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

Debug.h
浏览该文件的文档.
1 /*
2 © 2011-2016 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 YF_Use_TraceSrc
66 # define YF_Use_TraceSrc 1
67 # endif
68 #endif
69
70
71 namespace platform
72 {
73
85 YF_API bool
86 Echo(string_view) ynoexcept(YF_Platform == YF_Platform_DS);
87
88
94 class YF_API Logger
95 {
96 public:
97 using Level = Descriptions::RecordLevel;
98 using Filter = std::function<bool(Level, Logger&)>;
100 using Sender = std::function<void(Level, Logger&, const char*)>;
101
102 #ifdef NDEBUG
103 Level FilterLevel = Descriptions::Informative;
104 #else
105 Level FilterLevel = Descriptions::Debug;
106 #endif
107
108 private:
110 Filter filter{DefaultFilter};
112 Sender sender{FetchDefaultSender()};
120 Concurrency::recursive_mutex record_mutex;
121
122 public:
124 DefGetter(const ynothrow, const Sender&, Sender, sender)
125
126
130 void
131 SetFilter(Filter);
137 void
138 SetSender(Sender);
139
145 template<typename _func>
146 auto
147 AccessRecord(_func f) -> decltype(f())
148 {
149 Concurrency::lock_guard<Concurrency::recursive_mutex> lck(record_mutex);
150
151 return f();
152 }
153
155 static bool
156 DefaultFilter(Level, Logger&) ynothrow;
157
159
160
164 static YB_NONNULL(3) void
165 DefaultSendLog(Level, Logger&, const char*) ynothrowv;
166
171 static YB_NONNULL(3) void
172 DefaultSendLogToFile(Level, Logger&, const char*) ynothrowv;
174
181 void
182 DoLog(Level, const char*);
184 PDefH(void, DoLog, Level lv, string_view sv)
185 ImplRet(DoLog(lv, sv.data()))
187
188 private:
195 YB_NONNULL(1) void
196 DoLogRaw(Level, const char*);
198 PDefH(void, DoLogRaw, Level lv, string_view sv)
199 ImplRet(DoLogRaw(lv, sv.data()))
201
202 public:
209 void
210 DoLogException(Level level, const std::exception&) ynothrow;
211
217 static Sender
218 FetchDefaultSender(string_view = "YFramework");
219
220 template<typename _fCaller, typename... _tParams>
221 void
222 Log(Level level, _fCaller&& f, _tParams&&... args)
223 {
224 if(filter(level, *this))
225 TryExpr(DoLog(level, yforward(f)(yforward(args)...)))
226 CatchExpr(std::exception& e, DoLogException(level, e), throw)
227 }
228
235 static YB_NONNULL(4) void
236 SendLog(std::ostream&, Level, Logger&, const char*) ynothrowv;
237
239 static YB_NONNULL(1, 4) void
240 SendLogToFile(std::FILE*, Level, Logger&, const char*) ynothrowv;
242 };
243
244
249 YF_API Logger&
250 FetchCommonLogger();
251
252
260 YF_API YB_ATTR_gnu_printf(3, 4) YB_NONNULL(1, 3) string
261 LogWithSource(const char*, int, const char*, ...) ynothrow;
262
263
270 #define YCL_Log(_lv, ...) \
271 platform::FetchCommonLogger().Log( \
272 platform::Descriptions::RecordLevel(_lv), __VA_ARGS__)
273
279 #define YF_TraceRaw(_lv, ...) \
280 YCL_Log(_lv, [&]() -> platform::string { \
281 using platform::sfmt; \
282 \
283 TryRet(sfmt(__VA_ARGS__)) \
284 CatchRet(..., {}) \
285 })
286
293 #if YF_Use_TraceSrc
294 # define YF_Trace(_lv, ...) \
295 YCL_Log(_lv, [&]{ \
296 return platform::LogWithSource(__FILE__, __LINE__, __VA_ARGS__); \
297 })
298 #else
299 # define YF_Trace(_lv, ...) YF_TraceRaw(_lv, __VA_ARGS__)
300 #endif
301
302
310 #if YB_Use_YTrace
311 # define YTraceDe(_lv, ...) YF_Trace(_lv, __VA_ARGS__)
312 #else
313 # define YTraceDe(...)
314 #endif
315
316 } // namespace platform;
317
318 namespace platform_ex
319 {
320
321 #if YF_Multithread == 1
322
337 #if !YCL_Win32
338 YB_NORETURN
339 #endif
340 YF_API void
341 LogAssert(const char*, const char*, int, const char*) ynothrow;
342
343 # if YB_Use_YAssert
344 # undef YAssert
345 # define YAssert(_expr, _msg) \
347 ((_expr) ? void(0) \
348 : platform_ex::LogAssert(#_expr, __FILE__, __LINE__, _msg))
349 # endif
350 #endif
351
352 #if YCL_Win32
353
359 YF_API YB_NONNULL(3) void
360 SendDebugString(platform::Logger::Level, platform::Logger&, const char*)
361 ynothrowv;
362 #elif YCL_Android
363
374 YF_API int
375 MapAndroidLogLevel(platform::Descriptions::RecordLevel);
376
377
383 class YF_API AndroidLogSender
384 {
385 public:
386 using Logger = platform::Logger;
387 using Level = Logger::Level;
388
389 private:
390 string tag;
391
392 public:
397 AndroidLogSender(string_view);
399 DefDeCopyMoveCtorAssignment(AndroidLogSender)
400 ~AndroidLogSender();
401
403 void
404 operator()(Level, Logger&, const char*) const;
405
406 DefGetter(const ynothrow, const string&, Tag, tag)
407 };
408 #endif
409
410 } // namespace platform_ex;
411
412 namespace platform
413 {
414
420 template<typename _type>
421 inline _type&&
422 Nonnull(_type&& p) ynothrowv
423 {
424 YAssertNonnull(p);
425 return yforward(p);
426 }
427
433 template<typename _type>
434 inline _type&&
435 FwdIter(_type&& i) ynothrowv
436 {
437 using ystdex::is_undereferenceable;
438
439 YAssert(!is_undereferenceable(i), "Invalid iterator found.");
440 return yforward(i);
441 }
442
449 template<typename _type>
450 yconstfn auto
451 Deref(_type&& p) -> decltype(*p)
452 {
453 return *FwdIter(yforward(p));
454 }
455
464 inline YB_NONNULL(2) PDefH(string, ComposeMessageWithSignature,
465 const string& msg, const char* sig)
466 ImplRet(msg + " @ " + Nonnull(sig))
467 inline YB_NONNULL(1, 2) PDefH(string, ComposeMessageWithSignature,
468 const char* msg, const char* sig)
469 ImplRet(string(Nonnull(msg)) + " @ " + Nonnull(sig))
470 template<class _type>
471 inline YB_NONNULL(2) string
472 ComposeMessageWithSignature(const _type& msg, const char* sig)
473 {
474 using ystdex::to_string;
475
476 return to_string(msg) + " @ " + Nonnull(sig);
477 }
479
480 } // namespace platform;
481
482 #endif
483
platform::Descriptions::Debug
Definition: YCommon.h:170
platform::msg
const string & msg
Definition: Debug.h:464
platform::Echo
YF_API bool Echo(string_view) ynoexcept(YF_Platform
使用标准输出流打印字符串并刷新。
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
YF_Platform
#define YF_Platform
目标平台。
Definition: Platform.h:231
platform::Logger::Filter
std::function< bool(Level, Logger &)> Filter
Definition: Debug.h:98
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
platform
默认平台命名空间。
Definition: Container.h:47
platform::Deref
yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:451
CHRLib::f
f(d, s)
platform::PDefH
const string const char *sig PDefH(string, ComposeMessageWithSignature, const char *msg, const char *sig) ImplRet(string(Nonnull(msg))+" @ "+Nonnull(sig)) template< class _type > inline YB_NONNULL(2) string ComposeMessageWithSignature(const _type &msg
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:120
platform::YB_ATTR_gnu_printf
YF_API YB_ATTR_gnu_printf(3, 4) YB_NONNULL(1
格式输出日志字符串前追加记录源文件名和行号。
platform::Logger::Sender
std::function< void(Level, Logger &, const char *)> Sender
Definition: Debug.h:100
platform::Logger::record_mutex
Concurrency::recursive_mutex record_mutex
日志记录锁。
Definition: Debug.h:120
DefDeCopyMoveCtorAssignment
#define DefDeCopyMoveCtorAssignment(_t)
Definition: YBaseMacro.h:202
CHRLib::args
_tParams && args
Definition: CharacterProcessing.h:538
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
ystdex::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:280
platform::ImplRet
const string const char *sig ImplRet(msg+" @ "+Nonnull(sig)) inline YB_NONNULL(1
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:161
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
YSLib::level
YF_API size_t level
Definition: YException.h:142
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:111
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
platform::sig
const string const char *sig const char * sig
Definition: Debug.h:473
YSLib::i
return i
Definition: YCoreUtilities.h:176
platform::ynothrowv
YF_API int amode ynothrowv
Definition: FileIO.h:528
YF_API
#define YF_API
Definition: Platform.h:56
platform::YB_NONNULL
YB_NONNULL(2) PDefH(string
组合消息和函数签名字符串。
YModules.h
platform::to_string
return to_string(msg)+" @ "+Nonnull(sig)
YF_Platform_DS
#define YF_Platform_DS
目标平台标识。
Definition: Platform.h:130
platform::LogWithSource
YF_API string LogWithSource(const char *, int, const char *,...) ynothrow
platform::Logger
日志记录器。
Definition: Debug.h:94
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:109
std
Definition: memory.hpp:1183
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
platform::Nonnull
_type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:422
platform::FetchCommonLogger
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
platform::ComposeMessageWithSignature
ComposeMessageWithSignature
Definition: Debug.h:464
YB_NORETURN
#define YB_NORETURN
指定无返回值函数。
Definition: ydef.h:504
platform_ex
平台扩展命名空间。
Definition: Android.h:45
platform::Descriptions::Informative
Definition: YCommon.h:169
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 传递对应参数。
Definition: ydef.h:995
ystdex::single_thread::recursive_mutex
Definition: pseudo_mutex.h:771
platform::Logger::Level
Descriptions::RecordLevel Level
Definition: Debug.h:97
platform::FwdIter
_type && FwdIter(_type &&i) ynothrowv
断言并返回可解引用的迭代器。
Definition: Debug.h:435
ystdex::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:415
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:746
CHRLib::p
const auto p(d)
YSLib::lv
RecordLevel lv
Definition: YException.cpp:58
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:274