当前位置: 首页 > news >正文

一家专门做特卖的网站手机版成都网站建站

一家专门做特卖的网站手机版,成都网站建站,网站策划公司,嵌入式和网站开发自定义异常类spdlog_ex 标准库异常类#xff08;std::exception#xff09;系列#xff0c;能满足大多数使用异常的场景#xff0c;但对系统调用异常及错误信息缺乏支持。spdlog通过继承std::exception#xff0c;扩展对系统调用的支持#xff0c;实现自定义异常类spdlo…自定义异常类spdlog_ex 标准库异常类std::exception系列能满足大多数使用异常的场景但对系统调用异常及错误信息缺乏支持。spdlog通过继承std::exception扩展对系统调用的支持实现自定义异常类spdlog_ex。 spdlog_ex类声明只是在std::exception基础上添加了string类型的msg_成员提供支持errno的构造函数。 // include/spdlog/common.h class SPDLOG_API spdlog_ex : public std::exception { public:explicit spdlog_ex(std::string msg);spdlog_ex(const std::string msg, int last_errno);const char *what() const SPDLOG_NOEXCEPT override;private:std::string msg_; };异常 SPDLOG_INLINE spdlog_ex::spdlog_ex(std::string msg): msg_(std::move(msg)) {}SPDLOG_INLINE spdlog_ex::spdlog_ex(const std::string msg, int last_errno) { #ifdef SPDLOG_USE_STD_FORMATmsg_ std::system_error(std::error_code(last_errno, std::generic_category()), msg).what(); #elsememory_buf_t outbuf;fmt::format_system_error(outbuf, last_errno, msg.c_str());msg_ fmt::to_string(outbuf); #endif对于通用的异常spdlog_ex只是将用户传入的异常提示信息存放到msg_。 spdlog_ex对errno的支持主要是将errno转换为对应错误文本信息存放到msg_字符串中。spdlog使用的是ftm库提供的format_system_error来完成转换工作。 what()函数 SPDLOG_INLINE const char *spdlog_ex::what() const SPDLOG_NOEXCEPT {return msg_.c_str(); }what()是基类std::exception定义的virtual函数用户通常通过该接口获取异常信息。spdlog_ex返回存放异常信息的msg_。 异常的使用 spdlog提供了重载函数形式的接口throw_spdlog_ex。 SPDLOG_INLINE void throw_spdlog_ex(const std::string msg, int last_errno) {SPDLOG_THROW(spdlog_ex(msg, last_errno)); }SPDLOG_INLINE void throw_spdlog_ex(std::string msg) { SPDLOG_THROW(spdlog_ex(std::move(msg))); }抛出异常可以是这样 // send flush request to the thread pool SPDLOG_INLINE void spdlog::async_logger::flush_(){SPDLOG_TRY{auto pool_ptr thread_pool_.lock();if (!pool_ptr) {throw_spdlog_ex(async flush: thread pool doesnt exist anymore);}std::futurevoid future pool_ptr-post_flush(shared_from_this(), overflow_policy_);future.get();}SPDLOG_LOGGER_CATCH(source_loc()) }throw_spdlog_ex本质上也是throw spdlog_ex(…)这里多了宏定义SPDLOG_THROW。 #ifdef SPDLOG_NO_EXCEPTIONS#define SPDLOG_TRY#define SPDLOG_THROW(ex) \do { \printf(spdlog fatal error: %s\n, ex.what()); \std::abort(); \} while (0)#define SPDLOG_CATCH_STD #else#define SPDLOG_TRY try#define SPDLOG_THROW(ex) throw(ex)#define SPDLOG_CATCH_STD \catch (const std::exception ) { \} #endif实际上是提供了两种模式。抛出异常不抛出异常。 当没有定义宏SPDLOG_NO_EXCEPTIONS时正常抛出异常对象当定义了宏SPDLOG_NO_EXCEPTIONS时抛出异常替换为直接终止程序abort 因此在spdlog中捕获异常的代码块try-catch看起来会是这样 SPDLOG_INLINE thread_pool::~thread_pool() {// 析构函数不要抛出异常, 但释放线程池资源资源可能发生异常, 因此内部捕获并处理SPDLOG_TRY{for (size_t i 0; i threads_.size(); i) {// 有几个子线程就要post几个terminate的async_msg。post_async_msg_(async_msg(async_msg_type::terminate), async_overflow_policy::block);}for (auto t : threads_) {t.join();}}SPDLOG_CATCH_STD }可以使用自定义捕获catch代码块替换SPDLOG_CATCH_STD。 SPDLOG_INLINE void spdlog::async_logger::backend_flush_() {for (auto sink : sinks_){SPDLOG_TRY{sink-flush();}SPDLOG_LOGGER_CATCH(source_loc())} } #ifndef SPDLOG_NO_EXCEPTIONS#define SPDLOG_LOGGER_CATCH(location) \catch (const std::exception ex) { \if (location.filename) { \err_handler_(fmt_lib::format(SPDLOG_FMT_STRING({} [{}({})]), ex.what(), \location.filename, location.line)); \} else { \err_handler_(ex.what()); \} \} \catch (...) { \err_handler_(Rethrowing unknown exception in logger); \throw; \} #else#define SPDLOG_LOGGER_CATCH(location) #endifcatch (const std::exception ex)捕获所有从 std::exception 派生的异常。 if (location.filename)如果提供了源代码位置信息文件名则格式化错误消息包括异常的 what() 信息和源代码位置。 err_handler_这是一个错误处理函数或对象用于处理和记录错误消息。 catch (…)捕获所有其他类型的异常。 err_handler_(“Rethrowing unknown exception in logger”)记录一个未知异常的错误消息。 throw重新抛出捕获的异常。 如果禁用了异常处理通过定义 SPDLOG_NO_EXCEPTIONS这个宏展开为空。
http://www.hkea.cn/news/14322820/

相关文章:

  • 虚拟机做门户网站如何绑定域名手机网站空间申请
  • 用ps个人网站怎么做网站建设初学者教程
  • 阿里云突发性能适用于做网站吗wordpress免费模板带演示数据
  • 重庆论坛网站建设外贸建站模板下载
  • 什么是网站主题优化做网站的又营业执照的吗
  • 赣州哪里可以做网站建设银行对公网站打不开
  • 国外哪些做问卷赚钱的网站网站被k是什么意思
  • 一个网站的后台肇庆企业自助建站系统
  • 江宁区住房与城乡建设局网站aspnet网站开发书
  • 珠海网站建设陈玉铭深圳做app网站的公司哪家好
  • 灰色调网站wordpress凭密码
  • 网站建设价格便宜win7和WordPress的关系
  • 酒店网站制作策划网站首页线框图怎么做
  • 一块钱涨1000粉网站网站建设手续
  • 自定义手机网站建设上海做ui网站最好的公司
  • 江西住房和城乡建设部网站公司网站怎么做优化
  • 做的比较好网站有哪些红河网站制作
  • asp.net网站开发实例视频教程做家教网站要多少钱
  • 制作网站后台三五互联做的网站怎么样
  • 网站栏目管理软件源码购买一般在哪个网站
  • 周口网站建设73data要怎样建立自己的网站
  • 厦门人才网唯一官方网站登录入口网站手机pc同步
  • 网站标志的原则aspcms 网站栏目管理
  • 百度推广好做吗网站建设优化多少钱
  • 陕西的网站建设公司水果网络营销方案
  • 网站建设设计公司怎么添加网站背景音乐
  • 河北邢台wap网站建设比较好的摄影网站
  • 领先的手机网站设计wordpress标题数据表
  • 网站方案组成要素请网站制作公司费用
  • 网站栏目排序高端大气的网站模板