Flexiv RDK APIs  1.6.0
basics10_logging_behavior.cpp
1 
8 #include <flexiv/rdk/robot.hpp>
9 #include <flexiv/rdk/utility.hpp>
10 #include <spdlog/spdlog.h>
11 #include <spdlog/async.h>
12 #include <spdlog/sinks/stdout_color_sinks.h>
13 #include <spdlog/sinks/basic_file_sink.h>
14 
15 #include <iostream>
16 #include <string>
17 
18 using namespace flexiv;
19 
20 namespace {
21 constexpr char kDefaultLogPattern[] = "[%Y-%m-%d %H:%M:%S.%e] [%^%l%$] %v";
22 }
23 
25 void PrintHelp()
26 {
27  // clang-format off
28  std::cout << "Required arguments: [robot_sn]" << std::endl;
29  std::cout << " robot_sn: Serial number of the robot to connect. Remove any space, e.g. Rizon4s-123456" << std::endl;
30  std::cout << "Optional arguments: None" << std::endl;
31  std::cout << std::endl;
32  // clang-format on
33 }
34 
35 int main(int argc, char* argv[])
36 {
37  // Program Setup
38  // =============================================================================================
39  // Parse parameters
40  if (argc < 2 || rdk::utility::ProgramArgsExistAny(argc, argv, {"-h", "--help"})) {
41  PrintHelp();
42  return 1;
43  }
44  // Serial number of the robot to connect to. Remove any space, for example: Rizon4s-123456
45  std::string robot_sn = argv[1];
46 
47  // Print description
48  spdlog::info(
49  ">>> Tutorial description <<<\nThis tutorial shows how to change the logging behaviors of "
50  "RDK client.\n");
51 
52  // Suppress log messages from RDK client
53  // =========================================================================================
54  // Change log level to suppress certain type of log messages from RDK client. The log level
55  // setting will apply globally to all following spdlog messages
56  spdlog::set_level(spdlog::level::warn);
57  // messages spdlog::set_level(spdlog::level::off); ///< Suppress all messages
58 
59  // Instantiate RDK client, all info messages are suppressed
60  try {
61  rdk::Robot robot(robot_sn);
62  } catch (const std::exception& e) {
63  spdlog::error(e.what());
64  }
65 
66  // Output all log messages to a file
67  // =========================================================================================
68  // Create a logger that outputs the messages to both the console and a log file
69  auto console_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
70  auto file_sink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("example.log", true);
71  std::vector<spdlog::sink_ptr> sinks {console_sink, file_sink};
72  // Since writing to file takes some time, so we use async logger to avoid blocking the program
73  spdlog::init_thread_pool(10240, 1);
74  auto tp = spdlog::thread_pool();
75  auto logger
76  = std::make_shared<spdlog::async_logger>("global_logger", sinks.begin(), sinks.end(), tp);
77  // Use default pattern for log messages
78  logger->set_pattern(kDefaultLogPattern);
79 
80  // Use the logger configured above as the default global logger
81  spdlog::set_default_logger(logger);
82 
83  // Allow all messages
84  spdlog::set_level(spdlog::level::info);
85 
86  // Instantiate RDK client again, all messages are printed to console and output to a log file
87  try {
88  rdk::Robot robot(robot_sn);
89  } catch (const std::exception& e) {
90  spdlog::error(e.what());
91  }
92 
93  spdlog::warn("This message should also appear in the log file");
94  spdlog::info("Program finished");
95 
96  return 0;
97 }
Main interface with the robot, containing several function categories and background services.
Definition: robot.hpp:25