| D++ (DPP) C++ Discord API Bot Library

Search
Loading...
Searching...
No Matches
Integrating with spdlog

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:

#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <iomanip>
#include <dpp/dpp.h>
#include <fmt/format.h>
int main(int argc, char const *argv[]) {
dpp::cluster bot("token");
const std::string log_name = "mybot.log";
/* Set up spdlog logger */
std::shared_ptr<spdlog::logger> log;
spdlog::init_thread_pool(8192, 2);
std::vector<spdlog::sink_ptr> sinks;
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
auto rotating = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(log_name, 1024 * 1024 * 5, 10);
sinks.push_back(stdout_sink);
sinks.push_back(rotating);
log = std::make_shared<spdlog::async_logger>("logs", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
spdlog::register_logger(log);
log->set_pattern("%^%Y-%m-%d %H:%M:%S.%e [%L] [th#%t]%$ : %v");
log->set_level(spdlog::level::level_enum::debug);
/* Integrate spdlog logger to D++ log events */
bot.on_log([&bot, &log](const dpp::log_t & event) {
switch (event.severity) {
case dpp::ll_trace:
log->trace("{}", event.message);
break;
case dpp::ll_debug:
log->debug("{}", event.message);
break;
case dpp::ll_info:
log->info("{}", event.message);
break;
case dpp::ll_warning:
log->warn("{}", event.message);
break;
case dpp::ll_error:
log->error("{}", event.message);
break;
case dpp::ll_critical:
default:
log->critical("{}", event.message);
break;
}
});
/* Add the rest of your events */
bot.start(dpp::st_wait);
return 0;
}
dpp::cluster
The cluster class represents a group of shards and a command queue for sending and receiving commands...
Definition cluster.h:89
dpp::ll_error
@ ll_error
Error.
Definition misc-enum.h:81
dpp::ll_info
@ ll_info
Information.
Definition misc-enum.h:71
dpp::ll_debug
@ ll_debug
Debug.
Definition misc-enum.h:66
dpp::ll_trace
@ ll_trace
Trace.
Definition misc-enum.h:61
dpp::ll_critical
@ ll_critical
Critical.
Definition misc-enum.h:86
dpp::ll_warning
@ ll_warning
Warning.
Definition misc-enum.h:76
dpp::st_wait
@ st_wait
Wait forever on a condition variable. The cluster will spawn threads for each shard and start() will ...
Definition cluster.h:72
dpp::log_t
Log messages.
Definition dispatcher.h:201
dpp::log_t::message
std::string message
Log Message.
Definition dispatcher.h:213
dpp::log_t::severity
loglevel severity
Severity.
Definition dispatcher.h:208
D++ Library version 9.0.13 D++ Library version 9.0.12 D++ Library version 9.0.11 D++ Library version 9.0.10 D++ Library version 9.0.9 D++ Library version 9.0.8 D++ Library version 9.0.7 D++ Library version 9.0.6 D++ Library version 9.0.5 D++ Library version 9.0.4 D++ Library version 9.0.3 D++ Library version 9.0.2 D++ Library version 9.0.1 D++ Library version 9.0.0 D++ Library version 1.0.2 D++ Library version 1.0.1 D++ Library version 1.0.0