forked from chengxumiaodaren/BuriedPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buried_core.cc
91 lines (77 loc) · 2.91 KB
/
buried_core.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "buried_core.h"
#include "common/common_service.h"
#include "context/context.h"
#include "report/buried_report.h"
#include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include "third_party/nlohmann/json.hpp"
void Buried::InitWorkPath_(const std::string& work_dir) {
std::filesystem::path _work_dir(work_dir);
if (!std::filesystem::exists(_work_dir)) {
std::filesystem::create_directories(_work_dir);
}
/*
windows:
传参:D:\\projects\\BuriedPoint
结果:D:\\projects\\BuriedPoint\\buried
linux
传参:/usr/lib
结果:/usr/lib/buried
*/
work_path_ = _work_dir / "buried";//文件路径的拼接
if (!std::filesystem::exists(work_path_)) {
std::filesystem::create_directories(work_path_);
}
}
void Buried::InitLogger_() {
//sinks:控制日志写入的目的地,写入控制台
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
//%c:Date and time representation 日期和时间表示
//%s:Basename of the source file 源文件的名称
//%#:Source line 日志在文件中的行号
//%l:The log level of the message 消息的日志级别
//%v:The actual text to log
//[]
logger_->set_pattern("[%c] [%s:%#] [%l] %v");
logger_->set_level(spdlog::level::trace);
}
std::shared_ptr<spdlog::logger> Buried::Logger() { return logger_; }
Buried::Buried(const std::string& work_dir) {
buried::Context::GetGlobalContext().Start();
InitWorkPath_(work_dir);
InitLogger_();
SPDLOG_LOGGER_INFO(Logger(), "Buried init success");
//SPDLOG_LOGGER_ERROR(Logger(), "err");
}
Buried::~Buried() {}
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;
}
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;
}