This tutorial shows how to change the logging behaviors of RDK client.
#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <iostream>
#include <string>
using namespace flexiv;
namespace {
constexpr char kDefaultLogPattern[] = "[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v";
}
void PrintHelp()
{
std::cout << "Required arguments: [robot SN]" << std::endl;
std::cout << " robot SN: Serial number of the robot to connect to. "
"Remove any space, for example: Rizon4s-123456" << std::endl;
std::cout << "Optional arguments: None" << std::endl;
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 2 ||
rdk::utility::ProgramArgsExistAny(argc, argv, {
"-h",
"--help"})) {
PrintHelp();
return 1;
}
std::string robot_sn = argv[1];
spdlog::info(
">>> Tutorial description <<<\nThis tutorial shows how to change the logging behaviors of "
"RDK client.\n");
spdlog::set_level(spdlog::level::warn);
try {
rdk::Robot robot(robot_sn);
} catch (const std::exception& e) {
spdlog::error(e.what());
}
auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("example.log", true);
std::vector<spdlog::sink_ptr> sinks {console_sink, file_sink};
spdlog::init_thread_pool(10240, 1);
auto tp = spdlog::thread_pool();
auto logger
= std::make_shared<spdlog::async_logger>("global_logger", sinks.begin(), sinks.end(), tp);
logger->set_pattern(kDefaultLogPattern);
spdlog::set_default_logger(logger);
spdlog::set_level(spdlog::level::info);
try {
rdk::Robot robot(robot_sn);
} catch (const std::exception& e) {
spdlog::error(e.what());
}
spdlog::warn("This message should also appear in the log file");
spdlog::info("Program finished");
return 0;
}