API 说明

最近更新时间:2025-12-19 15:00:12

我的收藏
本文详细介绍 Windows 端性能监控 Pro SDK 的各功能接口,帮助您更加灵活、深度地使用 SDK。
说明:
所有的接口都需要包含 bugly_agent.h 头文件。

初始化接口

1. 创建 IBuglyAgent 对象。
/// 该放在定义在bugly_agent.h头文件中,功能就是加载client_extension.dll,并调用其导出函数创建IBuglyAgent对象
/// 如有需要,业务可以实现自己的CreateBuglyAgent,比如当业务需要加载指定目录的client_extension.dll时。
/// 注意:导出函数CreateBuglyAgent永远返回同一个IBuglyAgent对象,即IBuglyAgent是一个全局单例。
BA_INLINE BuglyAgentResult CreateBuglyAgent(IBuglyAgent** agent) {
char agent_dll[MAX_PATH];
::StringCchCopyA(agent_dll, _countof(agent_dll), BUGLY_AGENT_NAME);
if (strstr(BUGLY_AGENT_NAME, ".dll") == nullptr) {
::StringCchCatA(agent_dll, _countof(agent_dll), ".dll");
}

auto handle = LoadLibraryExA(agent_dll, nullptr, LOAD_LIBRARY_SEARCH_USER_DIRS | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (nullptr == handle) {
handle = LoadLibraryA(agent_dll);
}

if (handle == nullptr) {
return BuglyAgentResult::kNotFound;
}

auto create_func = reinterpret_cast<PfCreateBuglyAgent>(::GetProcAddress(handle, "CreateBuglyAgent"));
if (create_func == nullptr) {
return BuglyAgentResult::kNotFound;
}

*agent = create_func();
return BuglyAgentResult::kSuccess;
}
2. 初始化 Bugly。
/**
* @description: first 进程的初始化,和InitBuglyManager的区别是:InitBuglyManager2接口合并了InitBuglyManager和SetHandlerIPCPipe
* 如果is_reuse_bugly_server为true,InitBuglyManager2会先判断当前指定的pipename的crashpad_handler进程是否已经存在,如果
* 存在就直接复用,不存在就拉起新的。如果is_reuse_bugly_server为false,那么和InitBuglyManager一样直接新拉起一个crashpad handler进程
* @param handler 上报进程路径,绝对路径
* @param database crash 数据路径,绝对路径
* @param metrics_dir crash 数据路径,绝对路径
* @param app_id app id
* @param app_key app key
* @param app_version 版本号
* @param bundle_id qci id
* @param is_reuse_bugly_server 是否复用已有的crashpad_handler.exe进程,如果为false,就直接拉起新的crashpad_hander.exe
* @param timeout timeout 最大等待时间,单位 ms
* @param is_pop_dialog 是否弹窗提示用户 crash 信息
* @param is_need_attach_info 是否发送 crash 进程窗口及系统磁盘附加信息
* @param is_need_upload 是否上传, 如果为 false, 则只记录保存在本地.
* @param is_monitor_sel
* @param is_server_process 是否由系统服务进程调用
* @param account_id 初始用户id,供dau上报使用,当账号更新后可以调用SetKey通知bugly更新
* @param pipename 和crashpad_hander.exe连接的管道名称,如果为null,就像以前一样随机生成一个,随机生成的可以用GetHandlerIPCPipeName接口获取
* @return: 0 为成功
*/
virtual int InitBuglyManager2(const wchar_t* handler,
const wchar_t* database,
const wchar_t* metrics_dir,
const wchar_t* app_id,
const wchar_t* app_key,
const wchar_t* app_version,
const wchar_t* bundle_id,
const wchar_t* app_name,
const wchar_t* app_disp_name,
const char* build_id,
bool is_reuse_bugly_server,
unsigned int timeout,
bool is_pop_dialog,
bool is_need_attach_info,
bool is_need_upload = true,
bool is_monitor_sel = false,
bool is_server_process = false,
const wchar_t* account_id = nullptr,
const wchar_t* pipename = nullptr) = 0;

设置 crash 回调

/// 设置crash回调,可以在crash回调中设置需要附带上传的日志文件
virtual void SetCrashCallback(pfBuglyCrashCallback callback, void* context) = 0;

设置 crash 时上报日志接口


/**
* @description: 设置本进程的日志路径,上报crash时会将该日志打包上传;注意:如果日志文件超过5MB会被丢弃
* @param process_id 要更新日志文件路径的进程 id,如果该方法是在crash回调用调用,直接传本进程PID即可。
* @param tag 日志 tag 名称, 同一个进程如果要打捞多份日志的化需要用 tag 进行区分
* @param tag_len tag最大缓冲区,字符数,最大 20
* @param log_file_path 日志文件路径,
* @param log_file_path_len 日志文件路径最大缓冲区,字符数,最大 MAX_PATH
* @return: 0 为成功
* */
virtual int SetLogFilePath(unsigned long process_id,
const wchar_t* tag,
unsigned int tag_len,
const wchar_t* log_file_path,
unsigned int log_file_path_len) = 0;

更新 UserID

/// 设置userid
bugly_agent->SetKey(L"account_id", L"BuglyDemoUserId");

上报自定义错误

可以参考 Demo 中的 CustomErrorTest 函数,上报的数据将在 错误 页面中展示。

/**
* @description: 通用自定义错误上报接口,使用内存传递错误信息,对于可能触发很频繁的错误推荐使用该接口
* @param addr "#0 _MyHomePageState._incrementCounter.<anonymous closure> (package:flutter_application_1/main.dart:81:33)"
* @param message "type 'double' is not a subtype of type 'int' in type cast"
* @param name "_CastError(Serious)"
* @param call_stack full call_stack string
* @param thread_name "main"
* @param additional_info 后续扩展
*/
virtual int ReportCommonCustomError(const char* addr,
const char* message,
const char* name,
const char* call_stack,
const char* thread_name) = 0;

设置 minidump 包含更多信息


/**
* @description: 生成minidump时是否收集更多内存信息,打开这个会增加dmp文件大小,强烈建议打开
* @param bEnable: 是否收集更多内存
* @param maxSize: 收集额外内存大小限额,默认10MB
*/
virtual void SetEnableGatherIndirectlyReferencedMemory(bool bEnable, uint32_t maxSize = 10485760) = 0;

设置上报所有线程的堆栈

/**
* @description: 上报所有线程的调用栈。默认只上报crash线程的调用栈
* @param bEnable: 是否上报所有线程调用栈
*/
virtual void SetEnableDumpAllThreadCallstack(bool bEnable) = 0;

设置自定义字段

/**
* @description: 设置上报的自定义字段,限制和setkey一样,最大长度都是100
* @param key: 自定义字段的key
* @param value: 自定义字段的value,如果value为null或者空字符串,则删除对应的kv
*/
virtual void SetCustomField(const wchar_t* key, const wchar_t* value) = 0;