15. 整体功能组装

大家好,我是程序喵。


在前面的章节中,我向大家详细介绍了项目中各个子模块的实现,包括公共信息获取模块、多线程模块、上报模块、数据库模块、加解密模块以及三方库等模块。


这些子模块就好比汽车的几个车轮,它们各自拥有特定的功能和作用,但单独运行时并不能完成整个系统的正常工作。就像一辆汽车需要所有车轮协同工作才能行驶一样,我们需要将这些子模块进行整合,形成一个core模块,来完成整体的埋点上报功能。


core模块就像是汽车的发动机,它负责将各个子模块串联起来,使其协同工作。通过有效的整合,能够更好地发挥各个子模块的功能,实现完整的埋点上报功能。


首先看下CMakeLists.txt



这里将很多模块的源文件整体编译,生成一个静态库或者静态库。


其中的buried_core.cc就可以理解为咱上面说的core模块,用于整合所有子模块。


看下buried_core.h中暴露的接口:

struct Buried {
public:
struct Config {
std::string host;
std::string port;
std::string topic;
std::string user_id;
std::string app_version;
std::string app_name;
std::string custom_data;
};

public:
Buried(const std::string& work_dir);

~Buried();

BuriedResult Start(const Config& config);

BuriedResult Report(std::string title, std::string data, uint32_t priority);

public:
std::shared_ptr<spdlog::logger> Logger();

private:
void InitWorkPath_(const std::string& work_dir);

void InitLogger_();

private:
std::shared_ptr<spdlog::logger> logger_;
std::unique_ptr<buried::BuriedReport> buried_report_;

std::filesystem::path work_path_;
};


这段代码展示了C++埋点SDK的一些关键实现。首先,在buried_core.h中定义了一个名为Buried的结构体,其中包含了一些公共接口和私有成员变量。


公共接口包括:

  1. Buried(const std::string& work_dir):使用工作目录构造Buried对象。
  2. ~Buried():析构函数。
  3. BuriedResult Start(const Config& config):通过Config启动整个对象的上报能力。
  4. BuriedResult Report(std::string title, std::string data, uint32_t priority):使用Report接口上报某条数据。
  5. std::shared_ptr<spdlog::logger> Logger():提供对外的Logger接口,用于获取此对象内部的Logger实例。


私有成员变量包括:

  1. std::shared_ptr<spdlog::logger> logger_:Logger对象。
  2. std::unique_ptr<buried::BuriedReport> buried_report_:BuriedReport对象。
  3. std::filesystem::path work_path_:工作路径。


通过调用公共接口,可以构造对象、启动上报能力,并上报数据。


再看core模块的具体实现

Buried::Buried(const std::string& work_dir) {
buried::Context::GetGlobalContext().Start();
InitWorkPath_(work_dir);
InitLogger_();

SPDLOG_LOGGER_INFO(Logger(), "Buried init success");
}


构造函数Buried::Buried(const std::string& work_dir)中,首先启动了Context模块,然后设置了工作目录和Logger对象。


InitLogger_方法

void Buried::InitLogger_() {
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();

std::filesystem::path _log_dir = work_path_ / "buried.log";
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>(
_log_dir.string(), true);

logger_ = std::shared_ptr<spdlog::logger>(
new spdlog::logger("buried_sink", {console_sink, file_sink}));

// ref: <https://github.com/gabime/spdlog/wiki/3.-Custom-formatting>
logger_->set_pattern("[%c] [%s:%#] [%l] %v");
logger_->set_level(spdlog::level::trace);
}


在InitLogger_方法中,首先创建了一个用于控制台输出的console_sink对象,并将其与文件输出的sinks::basic_file_sink_mt对象一起传递给logger_对象的构造函数。文件输出的路径为工作目录下的buried.log文件。


然后,通过set_pattern方法设置了日志的格式,包括时间戳、文件名、行号和日志内容等。最后,设置了日志的级别为trace级别,即最详细的日志级别。


这样,Logger对象就构造完成了,可以通过调用Logger()方法获取到此对象。


Start方法

BuriedResult Buried::Start(const Config& config) {
buried::CommonService common_service;
common_service.host = config.host;
common_service.port = config.port;
common_service.topic = config.topic;
common_service.user_id = config.user_id;
common_service.app_version = config.app_version;
common_service.app_name = config.app_name;
common_service.custom_data = nlohmann::json::parse(config.custom_data);

buried_report_ = std::make_unique<buried::BuriedReport>(
logger_, std::move(common_service), work_path_.string());
buried_report_->Start();
return BuriedResult::kBuriedOk;
}


Start方法接收一个Config对象作为参数。首先,它创建了一个buried::CommonService对象,并将config中的各项配置赋值给common_service的对应成员变量。然后,它使用logger_、common_service和work_path_.string()作为参数,创建了一个新的buried::BuriedReport对象,最后,调用buried_report_的Start方法,启动了埋点上报功能。


通过将配置信息传递给CommonService对象,能够获取到上报所需的host、port、topic、user id、app version、app name和custom data等信息。然后,SDK使用这些信息初始化BuriedReport对象,才启动的埋点上报功能。


Report方法:

BuriedResult Buried::Report(std::string title, std::string data,
uint32_t priority) {
buried::BuriedData buried_data;
buried_data.title = std::move(title);
buried_data.data = std::move(data);
buried_data.priority = priority;
buried_report_->InsertData(buried_data);
return BuriedResult::kBuriedOk;
}


这个方法接受三个参数:title、data和priority。首先,它创建一个BuriedData对象,然后将title和data移动到BuriedData对象的对应成员变量中。接下来,它将priority赋值给BuriedData对象的priority成员变量。最后,通过调用buried_report_对象的InsertData方法,将BuriedData对象传递给BuriedReport对象,实现数据的上报功能。


到这里,整个项目代码已经完成了组装,你应该能够实现一个完整的埋点SDK功能。不知道你是否已经尝试运行整个项目。


详细代码请参考:https://github.com/chengxumiaodaren/BuriedPoint/blob/main/src/buried_core.cc


0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
程序喵
下载 APP