From 9c5292f4f5c7fe36794c9b33d24c4b95e932f114 Mon Sep 17 00:00:00 2001 From: baiyfcu Date: Wed, 24 Jul 2024 13:56:22 +0800 Subject: [PATCH] =?UTF-8?q?=E9=81=BF=E5=85=8DkBroadcastLogEvent=E8=A2=AB?= =?UTF-8?q?=E5=A4=96=E9=83=A8=E5=BC=95=E7=94=A8=E6=96=B0=E5=A2=9E=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8CAssert=5FThrow=E5=AE=9E=E7=8E=B0=E4=BB=8Ezlme?= =?UTF-8?q?diakit=E8=BF=81=E7=A7=BB=E7=BD=AE=E6=AD=A4,=E4=B8=A4=E8=80=85?= =?UTF-8?q?=E5=9D=87=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Util/logger.cpp | 2 ++ src/Util/logger.h | 2 ++ src/Util/util.cpp | 14 ++++++++++++++ src/Util/util.h | 15 +++++++++++++++ 4 files changed, 33 insertions(+) diff --git a/src/Util/logger.cpp b/src/Util/logger.cpp index 9be4e8be1..fc82c8312 100644 --- a/src/Util/logger.cpp +++ b/src/Util/logger.cpp @@ -283,6 +283,8 @@ void EventChannel::write(const Logger &logger, const LogContextPtr &ctx) { NOTICE_EMIT(BroadcastLogEventArgs, kBroadcastLogEvent, logger, ctx); } +const std::string &EventChannel::getBroadcastLogEventName() { return kBroadcastLogEvent;} + ///////////////////ConsoleChannel/////////////////// ConsoleChannel::ConsoleChannel(const string &name, LogLevel level) : LogChannel(name, level) {} diff --git a/src/Util/logger.h b/src/Util/logger.h index 191a21c91..b88673554 100644 --- a/src/Util/logger.h +++ b/src/Util/logger.h @@ -243,6 +243,8 @@ class EventChannel : public LogChannel { public: //输出日志时的广播名 static const std::string kBroadcastLogEvent; + //toolkit目前仅只有一处全局变量被外部引用,减少导出相关定义,导出以下函数避免导出kBroadcastLogEvent + static const std::string& getBroadcastLogEventName(); //日志广播参数类型和列表 #define BroadcastLogEventArgs const Logger &logger, const LogContextPtr &ctx diff --git a/src/Util/util.cpp b/src/Util/util.cpp index e63b9c927..43396b45a 100644 --- a/src/Util/util.cpp +++ b/src/Util/util.cpp @@ -650,3 +650,17 @@ void Creator::onDestoryException(const type_info &info, const exception &ex) { } // namespace toolkit + +extern "C" { +void Assert_Throw(int failed, const char *exp, const char *func, const char *file, int line, const char *str) { + if (failed) { + toolkit::_StrPrinter printer; + printer << "Assertion failed: (" << exp ; + if(str && *str){ + printer << ", " << str; + } + printer << "), function " << func << ", file " << file << ", line " << line << "."; + throw toolkit::AssertFailedException(printer); + } +} +} \ No newline at end of file diff --git a/src/Util/util.h b/src/Util/util.h index 1d53cc9c7..158d6b9b5 100644 --- a/src/Util/util.h +++ b/src/Util/util.h @@ -195,6 +195,12 @@ class ObjectStatistic{ return instance; \ } +class AssertFailedException : public std::runtime_error { +public: + template + AssertFailedException(T && ...args) : std::runtime_error(std::forward(args)...) {} +}; + std::string makeRandStr(int sz, bool printable = true); std::string hexdump(const void *buf, size_t len); std::string hexmem(const void* buf, size_t len); @@ -432,4 +438,13 @@ class AnyStorage : public std::unordered_map { }; } // namespace toolkit + +#ifdef __cplusplus +extern "C" { +#endif +extern void Assert_Throw(int failed, const char *exp, const char *func, const char *file, int line, const char *str); +#ifdef __cplusplus +} +#endif + #endif /* UTIL_UTIL_H_ */