YSTest: YException.cpp 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YException.cpp
浏览该文件的文档.
1 /*
2 © 2010-2016, 2018-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 #include "YSLib/Core/YModules.h"
29 #include YFM_YSLib_Core_YException // for Nonnull, Logger, to_std_string,
30 // ystdex::bind1, std::placeholders::_2;
31 #include <ystdex/exception.h> // for ystdex::handle_nested;
32 #include <cstdio> // for std::fprintf, stderr;
33
34 namespace YSLib
35 {
36
37 LoggedEvent::LoggedEvent(const char* str, RecordLevel lv)
38 : GeneralEvent(Nonnull(str)),
39 level(lv)
40 {}
41 LoggedEvent::LoggedEvent(string_view sv, RecordLevel lv)
42 : GeneralEvent(to_std_string(sv)),
43 level(lv)
44 {}
45 LoggedEvent::LoggedEvent(const GeneralEvent& e, RecordLevel lv)
46 : GeneralEvent(e),
47 level(lv)
48 {}
49 ImplDeDtor(LoggedEvent)
50
51
52 FatalError::FatalError(const char* t, string_view c)
53 : GeneralEvent(Nonnull(t)),
54 content((yunused(Nonnull(c.data())), make_shared<string>(string(c))))
55 {}
56 ImplDeDtor(FatalError)
57
58
59 void
60 PrintCriticalFor(const ExtractedLevelPrinter& print, const char* str,
61 RecordLevel lv, size_t level) ynothrow
62 {
63 TryExpr(print(Nonnull(str), level))
64 CatchExpr(..., YF_TraceRaw(lv < Critical ? lv : Critical,
65 "Failure @ PrintCriticalFor."))
66 }
67
68 void
69 TraceException(const char* str, RecordLevel lv, size_t level) ynothrow
70 {
71 TryExpr(
72 YF_TraceRaw(lv, "%s%s", std::string(level, ' ').c_str(), Nonnull(str)))
73 CatchExpr(..., YF_TraceRaw(lv < Critical ? lv : Critical,
74 "Failure @ TraceException."))
75 }
76
77 void
78 TraceExceptionType(const std::exception& e, RecordLevel lv) ynothrow
79 {
80 YF_TraceRaw(lv, "Caught std::exception[%s].", typeid(e).name());
81 }
82
83 void
84 ExtractAndTrace(const std::exception& e, RecordLevel lv) ynothrow
85 {
86 TraceExceptionType(e, lv);
87 ExtractException(ystdex::bind1(TraceException, lv, std::placeholders::_2),
88 e);
89 }
90
91 void
92 ExtractException(const ExtractedLevelPrinter& print, const std::exception& e,
93 size_t level) ynothrow
94 {
95 TryExpr(print(e.what(), level))
96 CatchExpr(..., PrintCriticalFor(print, "Exception occurred when printing"
97 " @ ExtractException.", Critical, level))
98 TryExpr(ystdex::handle_nested(e, [&, level](std::exception& ex) ynothrow{
99 ExtractException(print, ex, level + 1);
100 }))
101 CatchExpr(..., PrintCriticalFor(print, "Unknown nested exception found"
102 " nested on calling ystdex::handle_nested @ ExtractException.",
103 Critical, level))
104 }
105
106 bool
107 TryExecute(function<void()> f, const char* desc, RecordLevel lv,
108 ExceptionTracer trace)
109 {
110 try
111 {
112 TryExpr(f())
113 catch(...)
114 {
115 if(desc)
116 YF_TraceRaw(Notice, "Exception filtered: %s.", desc);
117 throw;
118 }
119 return {};
120 }
121 CatchExpr(std::exception& e, trace(e, lv))
122 return true;
123 }
124
125 } // namespace YSLib;
126
YF_TraceRaw
#define YF_TraceRaw(_lv,...)
YFramework 直接日志追踪。
Definition: Debug.h:378
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
platform::Nonnull
YB_PURE _type && Nonnull(_type &&p) ynothrowv
断言并返回非空参数。
Definition: Debug.h:518
CHRLib::c
c
Definition: CharacterProcessing.h:84
ystdex::bind1
auto bind1(_func &&f, _tParams &&... args) -> decltype(std::bind(std::forward< decltype(f)>(f), std::placeholders::_1, std::forward< decltype(args)>(args)...))
复合调用 std::bind 和 std::placeholders::_1 。
Definition: functional.hpp:53
ystdex::cpp2017::basic_string
Definition: string.hpp:77
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:178
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
ystdex::make_shared
std::shared_ptr< _type > make_shared(std::initializer_list< _tValue > il)
使用指定类型的初始化列表构造指定类型的 std::shared_ptr 对象。
Definition: memory.hpp:855
ImplDeDtor
#define ImplDeDtor(_t)
Definition: YBaseMacro.h:183
YModules.h
YSLib 核心模块配置文件。
YSLib::GeneralEvent
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: YException.h:46
YSLib::ImplDeDtor
ImplDeDtor(LoggedEvent) FatalError
Definition: YException.cpp:49
platform::strings::to_std_string
_tString to_std_string(basic_string_view< _tChar > sv)
Definition: Container.h:256
CHRLib::f
f(d, s)
YSLib::lv
const char RecordLevel lv
Definition: YException.cpp:61
platform::Descriptions::Critical
@ Critical
Definition: YCommon.h:181
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:51
ystdex::cpp2017::data
auto data(_tRange &c) -> decltype(c.data())
Definition: range.hpp:581
YSLib::ExceptionTracer
GLevelTracer< const std::exception & > ExceptionTracer
Definition: YException.h:133
platform::Descriptions::Notice
@ Notice
Definition: YCommon.h:184
YSLib::ExtractedLevelPrinter
function< void(const char *, size_t)> ExtractedLevelPrinter
打印带有层次信息的函数类型。
Definition: YException.h:130
YSLib::ExtractAndTrace
YF_API void ExtractAndTrace(const std::exception &, RecordLevel=Err) ynothrow
使用 TraceException 展开和跟踪异常类型和信息。
YSLib::LoggedEvent::LoggedEvent
LoggedEvent(const string_view, RecordLevel=Emergent)
Definition: YException.cpp:41
YSLib::TryExecute
YF_API bool TryExecute(function< void()>, const char *={}, RecordLevel=Alert, ExceptionTracer=ExtractAndTrace)
执行并尝试记录异常。
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
exception.h
标准库异常扩展接口。
YSLib::ExtractException
YF_API void ExtractException(const ExtractedLevelPrinter &, const std::exception &, size_t=0) ynothrow
展开指定层次的异常并使用指定参数记录。
YSLib::str
const char * str
Definition: YException.cpp:60
YSLib::RecordLevel
YF_API const char RecordLevel
Definition: YException.h:148
NPL::A1::name
const char * name
Definition: NPLA1.h:1876
YSLib::TraceExceptionType
YF_API void TraceExceptionType(const std::exception &, RecordLevel=Err) ynothrow
通过 YF_TraceRaw 跟踪记录异常类型。
ystdex
<memory> 特性测试宏。
Definition: ydef.h:1262
NPL::c_str
YF_API const char src c_str())) YF_API void InstallDirectory(const string &
安装目录：复制目录树。
Definition: Dependency.h:140
ystdex::handle_nested
void handle_nested(_tEx &e, _func &&f)
处理嵌套异常。
Definition: ystdex/exception.h:96
ystdex::function
function_base< function_traits< _fSig, empty_function_policy::throwing >, _fSig > function
函数包装类模板。
Definition: function.hpp:900
const
const Decapsulate const Decapsulate &y ynothrow ContextNode &ctx const
Definition: NPLA1Forms.cpp:1629
ystdex::cpp2017::string
basic_string< char > string
Definition: string.hpp:772
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:1199
TryExpr
#define TryExpr(...)
指定表达式在 try 块中求值。
Definition: YBaseMacro.h:120
yunused
#define yunused(...)
标记未使用的表达式。
Definition: ydef.h:1458
YSLib::FatalError
致命错误。
Definition: YException.h:91
YSLib::level
YF_API size_t level
Definition: YException.h:156