Windows
Windows SDK接入
下载SDK
接入业务可以直接前往 制品库 下载二进制文件。
我们提供Bugly-Windows SDK和一个BuglyDemo示例工程,如果接入过程中有编译连接错误,可以参考BuglyDemo工程的配置。
接入SDK
- 首次使用,需要在 Bugly平台 注册对应的产品组,及添加平台,以便获得对应的 AppKey 和 AppID 供后续接入使用。
- 参考BuglyDemo工程,设置不同构建配置下的
外部包含目录
和库目录
。 - 在
链接器
-->输入
-->附加依赖项
中添加bugly_logger.lib
。 - 将
BuglySDK\lib\x64_release
目录中的client_extension.dll
crashpad_handler.exe
crashpad_handler_extension.exe
bugly_logger.dll
拷贝到业务应用exe的同目录下。 - 在App启动的时候初始化Bugly:
/// bugly 相关头文件
#include "bugly_agent.h"
/// appid和appkey,专业版去这里创建产品
/// https://bugly.tds.qq.com/v2/index/main
#define BUGLY_APP_ID L"your app id"
#define BUGLY_APP_KEY L"your app key"
#define BUGLY_APP_BUNDLE_ID L"com.your.app"
/// <summary>
/// 设置bugly工作配置目录,bugly捕获到异常后生成数据会记录在这里
/// </summary>
std::wstring GenBuglyDataDirW() {
wchar_t tmpPathBuffer[1024] = { 0 };
::GetTempPath(1024, tmpPathBuffer);
std::wstring result = tmpPathBuffer;
return result + L"BuglyDemo";
}
/// <summary>
/// 获取crashpad_handler.exe所在的路径
/// </summary>
std::wstring GenHandlerPathW() {
std::wstring path = getExecutableDir();
path += L"\\crashpad_handler.exe";
return path;
}
/// <summary>
/// crash 回调函数,bugly捕获到crash后,会先回调给业务
/// 业务可以在这里添加一些日志,连同crash一起上报到bugly后台
/// </summary>
void CrashCallback(IBuglyAgent* agent, void* context) {
if (nullptr == agent) {
return;
}
std::wstring log_path = L"C:\\your_apps_log_path\\your_log_file.log";
agent->SetLogFilePath(GetCurrentProcessId(), L"Log1", 4, log_path.c_str(), log_path.size());
}
/// <summary>
/// 初始化bugly,用于监控和上报 crash
/// </summary>
bool InitBugly() {
std::wstring handler = GenHandlerPathW();
std::wstring reportsDir = GenBuglyDataDirW();
// 如果是监控windows系统服务进程,这个改成true,普通程序为false
bool bIsServerProcess = false;
std::wstring metricsDir = reportsDir;
wchar_t pipe_name[MAX_PATH] = {0};
BuglyAgentResult result = CreateBuglyAgent(&bugly_agent);
if (result != BuglyAgentResult::kSuccess || bugly_agent == nullptr) {
return false;
}
int bugly_init_result = bugly_agent->InitBuglyManager2(handler.c_str(),
reportsDir.c_str(),
metricsDir.c_str(),
BUGLY_APPID,
BUGLY_APPKEY,
L"3.16.0.2",
BUGLY_BUNDLE_ID,
APP_NAME,
APP_DISP_NAME,
"123.456.789",
true, // is_reuse_bugly_server
5000,
true, // is_pop_dialog
true, // is_need_attach_info
true, // is_need_upload
false, // is_monitor_self
bIsServerProcess, // is_server_process
L"InitAccountID2", // account_id
nullptr
);
if (bugly_init_result != BUGLY_INIT_NO_ERROR) {
return false;
}
// 设置userid
bugly_agent->SetKey(L"account_id", L"BuglyDemoUserId");
// 可选,除了crsah线程以外,额外将其他所有线程调用栈都报上去。
bugly_agent->SetEnableDumpAllThreadCallstack(true);
// 可选,初始化Bulgy logger
auto buglyLogDir = GenBuglyLogDir();
bugly_agent->LoadBuglyLoggerModule(buglyLogDir.c_str(), nullptr);
// 注册extra handler,用于监控strcpy_s一类安全函数抛出的异常以及虚函数调用purecall错误...
std::wstring extraHandlerModule = getExecutableDir();
extraHandlerModule += L"\\bugly_extra_handler.dll";
bugly_agent->EnablExtraHandler(true, extraHandlerModule.c_str());
// 设置crash回调
bugly_agent->SetCrashCallback(CrashCallback, nullptr);
// minidump收集indirectlyRefMemory, 第二个参数为额外收集内存的大小限额,不填的话默认就是10MB
bugly_agent->SetEnableGatherIndirectlyReferencedMemory(true);
// 设置渠道
bugly_agent->SetKey("app_channel", "MyChannel");
// 设置进程名
std::string pname_key = "procname_by_procid_";
pname_key += std::to_string(::GetCurrentProcessId());
bugly_agent->SetKey(pname_key.c_str(), "My ProcessName_by_pid");
return true;
}
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: 在此处放置代码。
// 初始化全局字符串
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_DEMO, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
....
/// 初始化Bugly
bool bugly_init_result = InitBugly();
// 如果第一次失败,再重试一次(很重要)
if (!bugly_init_result) {
bugly_init_result = InitBugly();
}
}
在程序中构造一个crash,非调试运行触发crash后检查Bugly的诊断窗口是否弹出,再去 Bugly平台 检查上报是否成功。
控制crash后是否弹窗:
//整个禁用弹框
bugly_agent->SetKey("is_pop_dialog", "false");
//白名单,指定进程才弹
bugly_agent->SetKey("is_pop_dialog", "allowlist");
bugly_agent->SetKey("add_popdlg_proc", std::to_string(::GetCurrentProcessId()).c_str());多进程的进程名设置:
/// 在初始化bugly后,使用如下特殊的key设置进程名
std::string pname_key = "procname_by_procid_";
pname_key += std::to_string(::GetCurrentProcessId());
bugly_agent->SetKey(pname_key.c_str(), "My ProcessName_by_pid");
/// 也就是
/// bugly_agent->SetKey("procname_by_procid_123", "pid 为123的processName");
/// bugly_agent->SetKey("procname_by_procid_456", "pid 为456的processName");
/// bugly_agent->SetKey("procname_by_procid_789", "pid 为789的processName");
更多的接口使用,请参考BuglyDemo/BuglyDemo.cpp和BuglySDK/include中的相关头文件