YSTest: YException.h 源文件

YSTest PreAlpha_b900_20201012 The YSLib Test Project

YException.h
浏览该文件的文档.
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 #ifndef YSL_INC_Core_YException_h_
29 #define YSL_INC_Core_YException_h_ 1
30
31 #include "YModules.h"
32 #include YFM_YSLib_Core_YFunc // for function;
33 #include <exception> // for mandated header;
34 #include <stdexcept> // for mandated header;
35 #include YFM_YSLib_Adaptor_YTextBase // for string_view;
36 #include <string> // for std::string;
37 #include <ystdex/functional.hpp> // for ystdex::nonvoid_result_t,
38 // ystdex::invoke_result_t, ystdex::invoke_nonvoid;
39
40 namespace YSLib
41 {
42
44
45 using GeneralEvent = std::runtime_error;
47
48
50 class YF_API LoggedEvent : public GeneralEvent
51 {
52 private:
54 RecordLevel level;
55
56 public:
62 YB_NONNULL(2)
64 LoggedEvent(const char* = "", RecordLevel = Emergent);
65 LoggedEvent(const string_view, RecordLevel = Emergent);
67
71 LoggedEvent(const GeneralEvent&, RecordLevel = Emergent);
73 DefDeCopyCtor(LoggedEvent)
78 ~LoggedEvent() override;
79
81 DefGetter(const ynothrow, RecordLevel, Level, level)
82 };
83
84
90 class YF_API FatalError : public GeneralEvent
91 {
92 private:
98 shared_ptr<string> content;
99
100 public:
107 YB_NONNULL(2)
108 FatalError(const char*, string_view);
110 DefDeCopyCtor(FatalError)
115 ~FatalError() override;
116
118 DefGetter(const ynothrow, string_view, Content, Deref(content))
119 DefGetter(const ynothrow, const char*, Title, what())
120 };
122
123
125
126
130 using ExtractedLevelPrinter = function<void(const char*, size_t)>;
131 template<typename _type>
132 using GLevelTracer = function<void(_type, RecordLevel)>;
133 using ExceptionTracer = GLevelTracer<const std::exception&>;
134
135
137
138
140
147 YF_API YB_NONNULL(2) void
148 PrintCriticalFor(const ExtractedLevelPrinter&, const char*, RecordLevel = Err,
149 size_t = 0) ynothrow;
150
155 YF_API YB_NONNULL(1) void
156 TraceException(const char*, RecordLevel = Err, size_t level = 0) ynothrow;
158
159
165 YF_API void
166 TraceExceptionType(const std::exception&, RecordLevel = Err) ynothrow;
168
176 YF_API void
177 ExtractAndTrace(const std::exception&, RecordLevel = Err) ynothrow;
178
184 YF_API void
185 ExtractException(const ExtractedLevelPrinter&, const std::exception&,
186 size_t = 0) ynothrow;
187
189
190
196 YF_API bool
197 TryExecute(function<void()>, const char* = {}, RecordLevel = Alert,
198 ExceptionTracer = ExtractAndTrace);
199
204 template<typename _fCallable, typename... _tParams>
205 ystdex::nonvoid_result_t<ystdex::invoke_result_t<_fCallable, _tParams...>>
206 TryInvoke(_fCallable&& f, _tParams&&... args) ynothrow
207 {
208 TryRet(ystdex::invoke_nonvoid(yforward(f), yforward(args)...))
209 CatchExpr(std::exception& e, ExtractAndTrace(e))
210 CatchExpr(..., YF_TraceRaw(Emergent, "Unknown exception found."))
211 return {};
212 }
213
223 template<typename _func>
224 bool
225 FilterExceptions(_func f, const char* desc = {}, RecordLevel lv = Alert,
226 ExceptionTracer trace = ExtractAndTrace) ynothrow
227 {
228 return !TryInvoke([=]{
229 return !TryExecute(f, desc, lv, trace);
230 });
231 }
233
234 } // namespace YSLib;
235
236 #endif
237
YF_TraceRaw
#define YF_TraceRaw(_lv,...)
YFramework 直接日志追踪。
Definition: Debug.h:378
CHRLib::e
const char const char * e
Definition: CharacterProcessing.h:112
YSLib::TryInvoke
ystdex::nonvoid_result_t< ystdex::invoke_result_t< _fCallable, _tParams... > > TryInvoke(_fCallable &&f, _tParams &&... args) ynothrow
调用函数并尝试返回。
Definition: YException.h:206
ystdex::invoke_nonvoid
nonvoid_result_t< invoke_result_t< _fCallable, _tParams... > > invoke_nonvoid(_fCallable &&f, _tParams &&... args)
调用可调用对象，保证返回值非空。
Definition: invoke.hpp:572
platform::Descriptions::Emergent
@ Emergent
Definition: YCommon.h:179
platform::Descriptions::RecordLevel
RecordLevel
记录等级。
Definition: YCommon.h:178
ystdex::cpp2017::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:437
YSLib::FatalError::content
shared_ptr< string > content
Definition: YException.h:98
YF_API
#define YF_API
Definition: Platform.h:56
YSLib::GeneralEvent
std::runtime_error GeneralEvent
一般运行时异常事件类。
Definition: YException.h:46
YSLib::GLevelTracer
function< void(_type, RecordLevel)> GLevelTracer
Definition: YException.h:132
CHRLib::f
f(d, s)
YSLib::lv
const char RecordLevel lv
Definition: YException.cpp:61
platform::Descriptions::Alert
@ Alert
Definition: YCommon.h:180
YSLib::LoggedEvent
记录日志的异常事件类。
Definition: YException.h:51
yforward
#define yforward(_expr)
根据参数类型使用 std::forward 转发对应参数。
Definition: ydef.h:1508
YSLib::ExceptionTracer
GLevelTracer< const std::exception & > ExceptionTracer
Definition: YException.h:133
YSLib::ExtractedLevelPrinter
function< void(const char *, size_t)> ExtractedLevelPrinter
打印带有层次信息的函数类型。
Definition: YException.h:130
std
Definition: optional.h:982
functional.hpp
函数和可调用对象。
YSLib::ExtractAndTrace
YF_API void ExtractAndTrace(const std::exception &, RecordLevel=Err) ynothrow
使用 TraceException 展开和跟踪异常类型和信息。
YSLib::FatalError::YB_NONNULL
YB_NONNULL(2) FatalError(const char *
构造：使用标题和内容。
YSLib::FilterExceptions
bool FilterExceptions(_func f, const char *desc={}, RecordLevel lv=Alert, ExceptionTracer trace=ExtractAndTrace) ynothrow
调用函数并过滤宿主异常。
Definition: YException.h:225
ystdex::nonvoid_result_t
cond_t< not_< is_void< _type > >, _type, pseudo_output > nonvoid_result_t
若类型不是空类型则取后备结果类型（默认为 pseudo_output ）。
Definition: invoke.hpp:100
platform::Descriptions::Err
@ Err
Definition: YCommon.h:182
YSLib::TryExecute
YF_API bool TryExecute(function< void()>, const char *={}, RecordLevel=Alert, ExceptionTracer=ExtractAndTrace)
执行并尝试记录异常。
ystdex::cpp2017::invoke_result_t
_t< invoke_result< _fCallable, _tParams... > > invoke_result_t
Definition: invoke.hpp:393
CatchExpr
#define CatchExpr(_ex,...)
指定表达式作为异常处理器。
Definition: YBaseMacro.h:129
YSLib
YSLib 命名空间。
Definition: AndroidHost.h:46
YSLib::ExtractException
YF_API void ExtractException(const ExtractedLevelPrinter &, const std::exception &, size_t=0) ynothrow
展开指定层次的异常并使用指定参数记录。
YModules.h
YSLib 用户界面模块配置文件。
YSLib::ynothrow
YB_PURE const _type size_t n ynothrow
Definition: YCoreUtilities.h:169
YSLib::RecordLevel
YF_API const char RecordLevel
Definition: YException.h:148
platform::Deref
YB_PURE yconstfn auto Deref(_type &&p) -> decltype(*p)
断言并解引用非空指针。
Definition: Debug.h:547
YSLib::TraceExceptionType
YF_API void TraceExceptionType(const std::exception &, RecordLevel=Err) ynothrow
通过 YF_TraceRaw 跟踪记录异常类型。
YSLib::LoggedEvent::level
RecordLevel level
Definition: YException.h:54
YSLib::_tParams
YB_ATTR_nodiscard YB_PURE typename _tParams
Definition: ValueNode.h:1294
DefDeCopyCtor
#define DefDeCopyCtor(_t)
Definition: YBaseMacro.h:162
YSLib::YB_NONNULL
YB_ATTR_nodiscard YF_API YB_NONNULL(1, 2) ValueNode LoadNPLA1File(const char *disp
载入 NPLA1 配置文件。
DefGetter
#define DefGetter(_q, _t, _n,...)
Definition: YBaseMacro.h:241
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
TryRet
#define TryRet(...)
指定表达式在 try 块中求值并返回。
Definition: YBaseMacro.h:124
YSLib::args
YB_ATTR_nodiscard YB_PURE typename _tParams && args
Definition: ValueNode.h:1299
YSLib::FatalError
致命错误。
Definition: YException.h:91
YSLib::level
YF_API size_t level
Definition: YException.h:156