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

小学网站建设教程创网站

小学网站建设教程,创网站,东莞网络推广运营企业,河池市都安县建设局网站QT 应用中集成 Sentry QT应用中集成 SentrySentry SDK for C/C注册 Sentry 账号QT 应用中集成 Sentry触发 Crash 上报 QT应用中集成 Sentry Sentry 是一个开源的错误监控和日志记录平台,旨在帮助开发团队实时捕获、跟踪和解决软件应用程序中的错误和异常。它提供了…

QT 应用中集成 Sentry

  • QT应用中集成 Sentry
    • Sentry SDK for C/C++
    • 注册 Sentry 账号
    • QT 应用中集成 Sentry
    • 触发 Crash 上报

QT应用中集成 Sentry

Sentry 是一个开源的错误监控和日志记录平台,旨在帮助开发团队实时捕获、跟踪和解决软件应用程序中的错误和异常。它提供了强大的错误监控和日志记录工具,使开发团队能够快速定位并解决生产环境中的问题。

在现代软件开发中,错误和异常是不可避免的。良好的错误监控和日志记录系统可以帮助开发团队快速响应和解决问题,提高软件的可靠性和稳定性。通过及时发现和修复问题,可以降低用户遭遇错误的概率,提升用户体验,同时也有助于保护品牌声誉。

本文将介绍如何集成 Sentry 到 QT应用程序,实现一个完善的错误监控和日志记录功能,解决 C 端产品因为用户环境等原因导致的难于定位、难于收集有效信息的痛点。

Sentry SDK for C/C++

第一步,前往 https://github.com/getsentry/sentry-native 下载 Sentry Native SDK。Sentry Native SDK 是一个针对本机应用程序的错误和崩溃报告客户端,针对 C 和 C++ 进行了优化,支持Sentry 20.6.0及更高版本。截止本文撰写的时间,最新版本是 0.7.0,直接下载 sentry-native.zip 这个包。
在这里插入图片描述
解压 sentry-native.zip 压缩包:

unzip sentry-native.zip -d sentry-native

第二步,编译 Sentry Native SDK。

$ cd sentry-native/
$ cmake -B build -D SENTRY_BACKEND=crashpad -D SENTRY_INTEGRATION_QT=YES
$ cmake --build build --parallel
# 将头文件和库文件等安装到 install 目录
$ cmake --install build --prefix install --config RelWithDebInfo
# 查看 install 下的目录结构,如果系统没有 tree 命令,使用 sudo apt install tree 安装
$ tree install
install
├── bin
│   └── crashpad_handler
├── include
│   └── sentry.h
└── lib├── cmake│   └── sentry│       ├── sentry-config.cmake│       ├── sentry-config-version.cmake│       ├── sentry_crashpad-targets.cmake│       ├── sentry_crashpad-targets-relwithdebinfo.cmake│       ├── sentry-targets.cmake│       └── sentry-targets-relwithdebinfo.cmake└── libsentry.so

如果出现如下错误:

CMake Error at /home/alex/.local/lib/python3.8/site-packages/cmake/data/share/cmake-3.27/Modules/FindCURL.cmake:176 (message):CURL: Required feature AsynchDNS is not found
Call Stack (most recent call first):CMakeLists.txt:278 (find_package)

解决方法:

$ sudo apt install libcurl4-openssl-dev

可以看到,编译出来的 Sentry Native SDK 包含头文件、库文件,还有一个 crashpad_handler,需要注意的是,crashpad_handler 这个文件需要放到应用程序所在的目录下。

注册 Sentry 账号

Sentry 服务可以本地部署,也可以直接使用 Sentry 提供的免费服务。对于个人开发者来说,Sentry 提供的免费服务够用,没有必要自行去折腾。

访问 https://sentry.io 网站,首先注册一个账号,这个过程和大多数注册过程相同,按照提示进行即可,这里就不赘述。

注册完成后,出现如下界面:
在这里插入图片描述
点击上面的那个Start按钮,会一步步直到你配置项目。
在这里插入图片描述
先点上面的 Desktop,就会出现桌面项目,然后点 QT,最后点击右下角的 Configure SDK。出现针对 QT 的集成代码示例,参照代码示例就可以完成在 QT 项目中集成 Sentry SDK。
在这里插入图片描述
点击右下角的 View Sample Error,系统会生成一个 Error 示例,在 Sentry 控制台可以看到:
在这里插入图片描述
上图展现了一个 Sentry 上报事件的详细信息。

QT 应用中集成 Sentry

借助于 Sentry native SDK,我们很容易集成 Sentry。下面用一个简单的示例程序进行说明,为了简单起见,我们创建一个控制台程序,项目起名为 SentryDemo。
在这里插入图片描述
按照前面 Qt SDK 配置提示的那样,在 main.cpp 中加入 sentry 初始化代码:

    sentry_options_t *options = sentry_options_new();sentry_options_set_dsn(options, "https://47e01ddf9ed4e2ce58914c67cd315d07@o4506677667561472.ingest.sentry.io/4506683131428864");// This is also the default-path. For further information and recommendations:// https://docs.sentry.io/platforms/native/configuration/options/#database-pathsentry_options_set_database_path(options, ".sentry-native");sentry_options_set_release(options, "my-project-name@2.3.12");sentry_options_set_debug(options, 1);sentry_init(options);

其中第二行代码要换成你的 Sentry 项目的 DSN,这个值可以在 sentry.io 的项目设置中查到。
在这里插入图片描述
接下来加入一个简单的事件捕获代码:

    sentry_capture_event(sentry_value_new_message_event(/*   level */ SENTRY_LEVEL_INFO,/*  logger */ "custom",/* message */ "It works!"));

为了使得代码编译通过,还得修改项目工程 pro 文件,加入:

INCLUDEPATH += $${PWD}/../thirdparty/sentry-native/install/include
LIBS += -L$${PWD}/../thirdparty/sentry-native/install/lib -lsentry

这样就得到了 SentryDemo 可执行程序,需要注意的是,在运行 SentryDemo 前,需要将前一步编译 Sentry Native SDK 得到的 libsentry.so 和 crashpad_handler 复制到和可执行程序相同的目录。执行结果如下:

$ ./SentryDemo 
[sentry] INFO using database path "/work/mywork/qt-in-action/source/build-SentryDemo-Desktop_Qt_5_15_2_GCC_64bit-Debug/.sentry-native"
[sentry] DEBUG starting transport
[sentry] DEBUG starting background worker thread
[sentry] DEBUG starting backend
[sentry] DEBUG starting crashpad backend with handler "/work/mywork/qt-in-action/source/build-SentryDemo-Desktop_Qt_5_15_2_GCC_64bit-Debug/crashpad_handler"
[sentry] DEBUG background worker thread started
[sentry] DEBUG using minidump URL "https://o4506677667561472.ingest.sentry.io:443/api/4506683131428864/minidump/?sentry_client=sentry.native/0.7.0&sentry_key=47e01ddf9ed4e2ce58914c67cd315d07"
[sentry] INFO started crashpad client handler
[sentry] DEBUG setting up Qt integration
[sentry] DEBUG processing and pruning old runs
[sentry] DEBUG merging scope into event
[sentry] DEBUG trying to read modules from /proc/self/maps
[sentry] DEBUG read 57 modules from /proc/self/maps
[sentry] DEBUG adding attachments to envelope
[sentry] DEBUG sending envelope
[sentry] DEBUG submitting task to background worker thread
[sentry] DEBUG executing task on worker thread
*   Trying 34.120.195.249:443...
* TCP_NODELAY set
* Connected to o4506677667561472.ingest.sentry.io (34.120.195.249) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crtCApath: /etc/ssl/certs
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: C=US; ST=California; L=San Francisco; O=Sentry; CN=ingest.sentry.io
*  start date: Nov  2 00:00:00 2023 GMT
*  expire date: Dec  2 23:59:59 2024 GMT
*  subjectAltName: host "o4506677667561472.ingest.sentry.io" matched cert's "*.ingest.sentry.io"
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert Global G2 TLS RSA SHA256 2020 CA1
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x55ad8d179b20)
> POST /api/4506683131428864/envelope/ HTTP/2
Host: o4506677667561472.ingest.sentry.io
user-agent: sentry.native/0.7.0
accept: */*
x-sentry-auth:Sentry sentry_key=47e01ddf9ed4e2ce58914c67cd315d07, sentry_version=7, sentry_client=sentry.native/0.7.0
content-type:application/x-sentry-envelope
content-length:13896* We are completely uploaded and fine
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
< HTTP/2 200 
< server: nginx
< date: Sat, 03 Feb 2024 13:54:11 GMT
< content-type: application/json
< content-length: 41
< access-control-allow-origin: *
< vary: origin,access-control-request-method,access-control-request-headers
< access-control-expose-headers: x-sentry-error,x-sentry-rate-limits,retry-after
< cross-origin-resource-policy: cross-origin
< x-envoy-upstream-service-time: 0
< strict-transport-security: max-age=31536000; includeSubDomains; preload
< via: 1.1 google
< alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000
< 
{"id":"8a84cd16adab42f49611bb32adfe72f0"}* Connection #0 to host o4506677667561472.ingest.sentry.io left intact

前往 sentry.io,可以看到我们收到如下消息上报:
在这里插入图片描述
点开,可以查看事件的详细信息。
在这里插入图片描述
可以看到,详细信息中包含操作系统版本、程序发布版本等等。

看到这里,你可能觉得,这不就是一个日志系统吗?

当然,你的确可以把 Sentry 当做一个在线日志系统,还是加强版本的。事件消息可以分级(level),还可以自定义类别(logger),还可以分版本(release)。

不过这样做,就有点暴殄天物了,因为 Sentry 还可以收集 crash 日志。

触发 Crash 上报

在程序中加入一段能引发内存访问错误的代码,模拟程序崩溃,看看会发生什么。

static void *invalid_mem = (void *)1;static void
trigger_crash()
{memset((char *)invalid_mem, 1, 100);
}

在 main 函数中调用 trigger_crash,运行之后,在 sentry.io 管理端就可以看到有新的 crash 报告上来:
在这里插入图片描述
点开可以查看详情:
在这里插入图片描述
这样,我们在程序崩溃时,就得到了错误上报,还包含了 crash 堆栈,通过调试符号,我们可以根据偏移地址,找到对应的源码行。

Sentry 的强大在于,我们可以上传调试符号文件,Sentry 自动分析符号文件,直接定位到源代码行,省去了我们使用 addr2line 之类的工具。

如何生成调试符号文件,如何上传调试符号文件,咱们在下一章再见。


文章转载自:

http://f9a2i1v6.bwkhp.cn
http://JxfbsYc4.bwkhp.cn
http://5wgWwacJ.bwkhp.cn
http://IR7f3okH.bwkhp.cn
http://i9ftnPys.bwkhp.cn
http://GtHSUZHR.bwkhp.cn
http://hn1S0Oq2.bwkhp.cn
http://3PXDYEYI.bwkhp.cn
http://A8RXLTw2.bwkhp.cn
http://UUXSkREd.bwkhp.cn
http://DwhNerTj.bwkhp.cn
http://IF3EK2ws.bwkhp.cn
http://v5CcC16n.bwkhp.cn
http://zJemjYtK.bwkhp.cn
http://VZZSQaPQ.bwkhp.cn
http://jLotpiBW.bwkhp.cn
http://Hhu2e6qZ.bwkhp.cn
http://XVHXL6mw.bwkhp.cn
http://M1Og6iem.bwkhp.cn
http://tJ1MIqAL.bwkhp.cn
http://ejGeIlWy.bwkhp.cn
http://ARpyJhuf.bwkhp.cn
http://x77fmhpI.bwkhp.cn
http://P0bjmnt7.bwkhp.cn
http://spIJNMBc.bwkhp.cn
http://82tMlMkK.bwkhp.cn
http://Si4J55gC.bwkhp.cn
http://41GHmhLf.bwkhp.cn
http://XlymMh0D.bwkhp.cn
http://O7GDdCxu.bwkhp.cn
http://www.dtcms.com/wzjs/735945.html

相关文章:

  • 开鲁网站seo站长工具jsp网站开发实例
  • 网站建设托管合同赣州有没有做网站的
  • 南京网站建设公司排名公司创建一个网站需要多少钱
  • 手机开发网站怎么做建站源码
  • 做网站用什么插件网站建设电话营销话术
  • 电子商务网站的管理网站建设为主题调研材料
  • wordpress企业站市长调度国庆假期安全稳定工作
  • 网站建设之开展电子商务网站建设陆金手指谷哥7
  • 如何建立新的企业网站宁波微信开发
  • 做网站买哪家的主机好常州网络推广价格
  • 商贸办公网站入口网站建设最基础的是什么意思
  • 网站开发教程H5做教育机构网站
  • 网站设计制作花多少钱济南房产查询网官网
  • 科凡网站建设怎么样品牌推广图片
  • 顺企网萍乡网站建设网站设计开发报价
  • 网站投放铝单板设计师招聘网
  • 网站备案承诺书奔驰宝马游戏网站建设
  • 域名命名网站新媒体一键发布平台
  • 大连模板网站制作公司电话网站建设大赛
  • 北京华诚传媒有限公司官方网站上海手机软件开发公司
  • 建站行业获客平面设计接单兼职
  • 网站后台注入皮肤自做头像的网站
  • 嘉定郑州阳网站建设网站建设偶像
  • 国家水资源监控能力建设网站免费虚拟主机空间申请
  • 广州建网站的网络公司wordpress调用缩略图尺寸
  • 网站甚而模板文章网站后台
  • 重庆网站建站系统平台广西桂林旅游攻略
  • 做百度推广一定要有自已网站设计素材下载网站
  • 做家电家具回收用哪个网站好高端网站定制开发
  • 网站开发成本估计昆明市住房和城乡建设局网站