YSTest: Debug.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

Debug.h
浏览该文件的文档.
1 /*
2 © 2011-2020 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 <ystdex/function.hpp> // for ystdex::function;
36 #include YFM_YCLib_Mutex // for Concurrency;
37 #include <ystdex/swap.hpp> // for ystdex::copy_and_swap;
38
66 #ifndef NDEBUG
67 # ifndef YF_Use_TraceSrc
68 # define YF_Use_TraceSrc 1
69 # endif
70 #endif
71
72
73 namespace platform
74 {
75
87 YF_API bool
88 Echo(string_view) ynoexcept(YF_Platform == YF_Platform_DS);
89
90
96 class YF_API Logger
97 {
98 public:
104 template<class _tString = platform::string>
105 struct Formatter
106 {
107 using value_type = _tString;
108
109 template<typename... _tParams>
110 inline value_type
111 operator()(_tParams&&... args)
112 {
113 return ystdex::sfmt<value_type>(yforward(args)...);
114 }
115 };
116 using Level = Descriptions::RecordLevel;
117 using Filter = ystdex::function<bool(Level, Logger&)>;
119 using Sender = ystdex::function<void(Level, Logger&, const char*)>;
120
122 #ifdef NDEBUG
123 Level FilterLevel = Descriptions::Informative;
124 #else
125 Level FilterLevel = Descriptions::Debug;
126 #endif
127
128 private:
130 Filter filter{DefaultFilter};
132 Sender sender{FetchDefaultSender()};
140 mutable Concurrency::recursive_mutex record_mutex{};
141
142 public:
144
145
150 DefDeCtor(Logger)
152 Logger(const Logger&);
158 Logger(Logger&&) ynothrow;
160
162 PDefHOp(Logger&, =, const Logger& logger)
163 ImplRet(ystdex::copy_and_swap(*this, logger))
165 PDefHOp(Logger&, =, Logger&& logger) ynothrow
166 ImplRet(swap(*this, logger), *this)
167
169 DefGetter(const ynothrow, const Sender&, Sender, sender)
170
175 void
176 SetFilter(Filter);
182 void
183 SetSender(Sender);
184
190 template<typename _func>
191 auto
192 AccessRecord(_func f) -> decltype(f())
193 {
194 Concurrency::lock_guard<Concurrency::recursive_mutex> lck(record_mutex);
195
196 return f();
197 }
198
200 YB_PURE static bool
201 DefaultFilter(Level, Logger&) ynothrow;
202
204
205
210 static YB_NONNULL(3) void
211 DefaultSendLog(Level, Logger&, const char*) ynothrowv;
212
217 static YB_NONNULL(3) void
218 DefaultSendLogToFile(Level, Logger&, const char*) ynothrowv;
220
227 void
228 DoLog(Level, const char*);
230 PDefH(void, DoLog, Level lv, string_view sv)
231 ImplRet(DoLog(lv, sv.data()))
233
234 private:
241 YB_NONNULL(3) void
242 DoLogRaw(Level, const char*);
244 PDefH(void, DoLogRaw, Level lv, string_view sv)
245 ImplRet(DoLogRaw(lv, sv.data()))
247
248 public:
255 void
256 DoLogException(Level level, const std::exception&) ynothrow;
257
264 static Sender
265 FetchDefaultSender(string_view = "YFramework");
266
273 template<typename _fCaller, typename... _tParams>
274 void
275 Log(Level level, _fCaller&& f, _tParams&&... args)
276 {
277 if(filter(level, *this))
278 TryExpr(DoLog(level, yforward(f)(yforward(args)...)))
279 CatchExpr(std::exception& e, DoLogException(level, e), throw)
280 }
281
287 static void
288 LogFailure() ynothrow;
289
296 static YB_NONNULL(3) void
298 SendLog(std::ostream&, Level, const char*) ynothrowv;
299
301 static YB_NONNULL(1, 3) void
302 SendLogToFile(std::FILE*, Level, const char*) ynothrowv;
304
318 template<typename _fCaller, typename... _tParams>
319 inline void
320 Trace(Level level, _fCaller&& f, _tParams&&... args) ynothrow
321 {
322 TryExpr(Log(level, yforward(f), yforward(args)...))
323 CatchExpr(..., LogFailure())
324 }
325
332 template<typename... _tParams>
333 inline void
334 TraceFormat(Level level, _tParams&&... args) ynothrow
335 {
336 Trace(level, Formatter<>(), yforward(args)...);
337 }
339
344 YF_API friend void
345 swap(Logger&, Logger&) ynothrow;
346 };
347
348
353 YF_API Logger&
354 FetchCommonLogger();
355
356
364 YF_API YB_ATTR_gnu_printf(3, 4) YB_NONNULL(1, 3) string
365 LogWithSource(const char*, int, const char*, ...);
366
367
378 #define YF_TraceRaw(_lv, ...) \
379 platform::FetchCommonLogger().TraceFormat( \
380 platform::Descriptions::RecordLevel(_lv), __VA_ARGS__) \
381
382
387 #if YF_Use_TraceSrc
388 # define YF_Trace(_lv, ...) \
389 platform::FetchCommonLogger().Trace(platform::Descriptions::RecordLevel( \
390 _lv), platform::LogWithSource, __FILE__, __LINE__, __VA_ARGS__)
391 #else
392 # define YF_Trace(_lv, ...) YF_TraceRaw(_lv, __VA_ARGS__)
393 #endif
394
395
396
404 #if YB_Use_YTrace
405 # define YTraceDe(_lv, ...) YF_Trace(_lv, __VA_ARGS__)
406 #else
407 # define YTraceDe(...)
408 #endif
409
410 } // namespace platform;
411
412 namespace platform_ex
413 {
414
415 #if YF_Multithread == 1
416
431 #if !YCL_Win32
432 YB_NORETURN
433 #endif
434 YF_API void
435 LogAssert(const char*, const char*, int, const char*) ynothrow;
436
437 # if YB_Use_YAssert
438 # undef YAssert
439 # define YAssert(_expr, _msg) \
441 ((_expr) ? void(0) \
442 : platform_ex::LogAssert(#_expr, __FILE__, __LINE__, _msg))
443 # endif
444 #endif
445
446 #if YCL_Win32
447
453 YF_API YB_NONNULL(3) void
454 SendDebugString(platform::Logger::Level, platform::Logger&, const char*)
455 ynothrowv;
456 #elif YCL_Android
457
468 YF_API int
469 MapAndroidLogLevel(platform::Descriptions::RecordLevel);
470
471
478 class YF_API AndroidLogSender
479 {
480 public:
481 using Logger = platform::Logger;
482 using Level = Logger::Level;
483
484 private:
485 string tag;
486
487 public:
492 AndroidLogSender(string_view);
494 DefDeCopyMoveCtorAssignment(AndroidLogSender)
496 ~AndroidLogSender();
497
499 void
500 operator()(Level, Logger&, const char*) const;
501
502 DefGetter(const ynothrow, const string&, Tag, tag)
503 };
504 #endif
505
506 } // namespace platform_ex;
507
508 namespace platform
509 {
510
516 template<typename _type>
517 YB_PURE inline _type&&
518 Nonnull(_type&& p) ynothrowv
519 {
520 YAssertNonnull(p);
521 return yforward(p);
522 }
523
529 template<typename _type>
530 YB_PURE inline _type&&
531 FwdIter(_type&& i) ynothrowv
532 {
533 using ystdex::is_undereferenceable;
534
535 YAssert(!is_undereferenceable(i), "Invalid iterator found.");
536 return yforward(i);
537 }
538
545 template<typename _type>
546 YB_PURE yconstfn auto
547 Deref(_type&& p) -> decltype(*p)
548 {
549 return *FwdIter(yforward(p));
550 }
551
560 YB_NONNULL(2) YB_PURE inline PDefH(std::string, ComposeMessageWithSignature,
561 const std::string& msg, const char* sig)
562 ImplRet(msg + " @ " + Nonnull(sig))
563 YB_NONNULL(1, 2) YB_PURE inline PDefH(std::string, ComposeMessageWithSignature,
564 const char* msg, const char* sig)
565 ImplRet(std::string(Nonnull(msg)) + " @ " + Nonnull(sig))
566 template<class _type>
567 YB_NONNULL(2) YB_PURE inline std::string
568 ComposeMessageWithSignature(const _type& msg, const char* sig)
569 {
570 using ystdex::to_string;
571
572 return to_string(msg) + " @ " + Nonnull(sig);
573 }
575
576 } // namespace platform;
577
578 #endif
579
platform::Echo
YF_API bool Echo(string_view) ynoexcept(YF_Platform
使用标准输出流打印字符串并刷新。
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
platform::Logger::Level
Descriptions::RecordLevel Level
Definition: Debug.h:116
platform::Nonnull
YB_PURE _type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:518
ystdex::cpp2017::swap
void swap(optional< _type > &x, optional< _type > &y)
Definition: optional.h:888
platform::Logger::Formatter::value_type
_tString value_type
Definition: Debug.h:107
YB_PURE
#define YB_PURE
指示函数或函数模板实例为纯函数。
Definition: ydef.h:919
platform_ex
平台扩展命名空间。
Definition: Android.h:46
platform::Logger::DefDeCtor
DefDeCtor(Logger) Logger(const Logger &)
无参数构造：使用过滤器和发送器。
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:178
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
ystdex::function_base
函数包装类模板。
Definition: function.hpp:578
YF_Platform
#define YF_Platform
目标平台。
Definition: Platform.h:231
swap.hpp
交换操作。
platform_ex::YB_NONNULL
YB_ATTR_nodiscard YB_NONNULL(1) inline PDefH(u16string
构造适合表示路径的 char16_t 字符串。
YF_API
#define YF_API
Definition: Platform.h:56
ystdex::copy_and_swap
_type & copy_and_swap(_type &obj, const _type2 &new_val)
复制对象并交换。
Definition: swap.hpp:514
platform::Logger::YB_NONNULL
static YB_NONNULL(3) void DefaultSendLog(Level
默认发送器：使用 std::cerr 输出。
CHRLib::f
f(d, s)
YAssert
#define YAssert(_expr, _msg)
Definition: cassert.h:154
platform::FwdIter
YB_PURE _type && FwdIter(_type &&i) ynothrowv
断言并返回可解引用的迭代器。
Definition: Debug.h:531
CHRLib::args
YB_ATTR_nodiscard _tParams && args
Definition: CharacterProcessing.h:649
YSLib::lv
const char RecordLevel lv
Definition: YException.cpp:61
platform::FetchCommonLogger
YF_API Logger & FetchCommonLogger()
取公共日志记录器。
function.hpp
函数基本操作和调用包装对象。
DefDeCopyMoveCtorAssignment
#define DefDeCopyMoveCtorAssignment(_t)
Definition: YBaseMacro.h:202
platform::ImplRet
YB_ATTR_nodiscard const char *s ImplRet(Nonnull(s)) YB_ATTR_nodiscard inline PDefH(const string &
YSLib::IO::_tString
const string &path const String &path class class class _tString
Definition: YSLib/Service/FileSystem.h:612
YB_NORETURN
#define YB_NORETURN
指定无返回值函数。
Definition: ydef.h:877
YF_Platform_DS
#define YF_Platform_DS
目标平台标识。
Definition: Platform.h:130
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
platform::ynothrow
const _tString &path ynothrow
Definition: YCLib/FileSystem.h:453
ystdex::threading::lock_guard
Definition: pseudo_mutex.h:223
ystdex::cpp2017::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:581
filter
Filter filter
Definition: NPLA1.cpp:360
platform::Descriptions::Informative
@ Informative
Definition: YCommon.h:185
platform::Logger::Formatter::operator()
value_type operator()(_tParams &&... args)
Definition: Debug.h:111
platform::Logger::TraceFormat
void TraceFormat(Level level, _tParams &&... args) ynothrow
以参数指定的等级和参数指定的格式记录日志并保证无异常抛出。
Definition: Debug.h:334
std
Definition: optional.h:982
platform::Logger::Logger
Logger(Logger &&) ynothrow
转移构造：转移过滤等级、转移过滤器和发送器，使用新创建的互斥量。
YAssertNonnull
#define YAssertNonnull(_expr)
Definition: cassert.h:165
platform::Logger::DefaultFilter
static YB_PURE bool DefaultFilter(Level, Logger &) ynothrow
默认过滤：仅允许等级不大于阈值的日志被记录。
YSLib::i
return i
Definition: YCoreUtilities.h:177
ystdex::single_thread::recursive_mutex
Definition: pseudo_mutex.h:773
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
platform::YB_NONNULL
YB_NONNULL(2) YB_PURE inline PDefH(std
组合消息和函数签名字符串。
Definition: Debug.h:560
platform::Logger::Formatter
日志格式化器。
Definition: Debug.h:106
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
platform
默认平台命名空间。
Definition: Container.h:56
YModules.h
YSLib 用户界面模块配置文件。
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:46
platform::Deref
YB_PURE yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:547
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:1219
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
platform::Logger::swap
YF_API friend void swap(Logger &, Logger &) ynothrow
交换：交换所有互斥量以外的数据成员。
platform::PDefH
YB_ATTR_nodiscard PDefH(string, MakePathString, u16string_view sv) ImplRet(MakePathString(sv.data())) using FileNodeID
文件节点标识类型。
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
platform::Logger
日志记录器。
Definition: Debug.h:97
platform::ynothrowv
YB_ATTR_nodiscard YF_API int amode ynothrowv
Definition: FileIO.h:550
platform::LogWithSource
YF_API string LogWithSource(const char *, int, const char *,...)
platform::p
return p p
Definition: YCLib/FileSystem.h:209
platform::Logger::LogFailure
static void LogFailure() ynothrow
记录失败输出。
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
platform::YB_ATTR_gnu_printf
YF_API YB_ATTR_gnu_printf(3, 4) YB_NONNULL(1
格式输出日志字符串前追加记录源文件名和行号。
platform::bool
YB_ATTR_nodiscard std::FILE *fp bool
Definition: FileIO.h:1271
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
ynoexcept
#define ynoexcept(...)
YSLib 无异常抛出保证：指定特定的异常规范。
Definition: ydef.h:1151
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:1073
YSLib::level
YF_API size_t level
Definition: YException.h:156
platform::Descriptions::Debug
@ Debug
Definition: YCommon.h:186