Debian Codegraph @

docpages/example_programs/misc/spdlog.md

raw symbols SCIP
1 \page spdlog Integrating with spdlog
2
3 If you want to make your bot use spdlog, like aegis does, you can attach it to the `on_log` event. You can do this as follows:
4
5 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cpp
6 #include <spdlog/spdlog.h>
7 #include <spdlog/async.h>
8 #include <spdlog/sinks/stdout_color_sinks.h>
9 #include <spdlog/sinks/rotating_file_sink.h>
10 #include <iomanip>
11 #include <dpp/dpp.h>
12 #include <fmt/format.h>
13
14 int main(int argc, char const *argv[]) {
15 dpp::cluster bot("token");
16
17 const std::string log_name = "mybot.log";
18
19 /* Set up spdlog logger */
20 std::shared_ptr<spdlog::logger> log;
21 spdlog::init_thread_pool(8192, 2);
22 std::vector<spdlog::sink_ptr> sinks;
23 auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
24 auto rotating = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_name, 1024 * 1024 * 5, 10);
25 sinks.push_back(stdout_sink);
26 sinks.push_back(rotating);
27 log = std::make_shared<spdlog::async_logger>("logs", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
28 spdlog::register_logger(log);
29 log->set_pattern("%^%Y-%m-%d %H:%M:%S.%e [%L] [th#%t]%$ : %v");
30 log->set_level(spdlog::level::level_enum::debug);
31
32 /* Integrate spdlog logger to D++ log events */
33 bot.on_log([&bot, &log](const dpp::log_t & event) {
34 switch (event.severity) {
35 case dpp::ll_trace:
36 log->trace("{}", event.message);
37 break;
38 case dpp::ll_debug:
39 log->debug("{}", event.message);
40 break;
41 case dpp::ll_info:
42 log->info("{}", event.message);
43 break;
44 case dpp::ll_warning:
45 log->warn("{}", event.message);
46 break;
47 case dpp::ll_error:
48 log->error("{}", event.message);
49 break;
50 case dpp::ll_critical:
51 default:
52 log->critical("{}", event.message);
53 break;
54 }
55 });
56
57 /* Add the rest of your events */
58
59 bot.start(dpp::st_wait);
60 return 0;
61 }
62 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~