tools/fw-logger/rs-fw-logger.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include "librealsense2/rs.hpp"
6 #include <fstream>
7 #include <thread>
8 #include "tclap/CmdLine.h"
9 
10 
11 
12 using namespace std;
13 using namespace TCLAP;
14 using namespace rs2;
15 
16 string char2hex(unsigned char n)
17 {
18  string res;
19 
20  do
21  {
22  res += "0123456789ABCDEF"[n % 16];
23  n >>= 4;
24  } while (n);
25 
26  reverse(res.begin(), res.end());
27 
28  if (res.size() == 1)
29  {
30  res.insert(0, "0");
31  }
32 
33  return res;
34 }
35 
37 {
38  auto t = time(nullptr);
39  char buffer[20] = {};
40  const tm* time = localtime(&t);
41  if (nullptr != time)
42  strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", time);
43 
44  return string(buffer);
45 }
46 
47 int main(int argc, char* argv[])
48 {
49  int default_polling_interval_ms = 100;
50  CmdLine cmd("librealsense rs-fw-logger example tool", ' ', RS2_API_VERSION_STR);
51  ValueArg<string> xml_arg("l", "load", "Full file path of HW Logger Events XML file", false, "", "Load HW Logger Events XML file");
52  ValueArg<string> out_arg("o", "out", "Full file path of output file", false, "", "Print Fw logs to output file");
53  ValueArg<int> polling_interval_arg("p", "polling_interval", "Time Interval between each log messages polling (in milliseconds)", false, default_polling_interval_ms, "");
54  SwitchArg flash_logs_arg("f", "flash", "Flash Logs Request", false);
55  cmd.add(xml_arg);
56  cmd.add(out_arg);
57  cmd.add(polling_interval_arg);
58  cmd.add(flash_logs_arg);
59  cmd.parse(argc, argv);
60 
61  log_to_file(RS2_LOG_SEVERITY_WARN, "librealsense.log");
62 
63  auto use_xml_file = false;
64  auto output_file_path = out_arg.getValue();
65  std::ofstream output_file(output_file_path);
66  // write to file if it is open, else write to console
67  std::ostream& out = (!output_file.is_open() ? std::cout : output_file);
68 
69  auto xml_full_file_path = xml_arg.getValue();
70  auto polling_interval_ms = polling_interval_arg.getValue();
71  if (polling_interval_ms < 25 || polling_interval_ms > 300)
72  {
73  out << "Polling interval time provided: " << polling_interval_ms << "ms, is not in the valid range [25,300]. Default value " << default_polling_interval_ms << "ms is used." << std::endl;
74  polling_interval_ms = default_polling_interval_ms;
75  }
76 
77  bool are_flash_logs_requested = flash_logs_arg.isSet();
78 
79  context ctx;
80  device_hub hub(ctx);
81 
82  bool should_loop_end = false;
83 
84  while (!should_loop_end)
85  {
86  try
87  {
88  out << "\nWaiting for RealSense device to connect...\n";
89  auto dev = hub.wait_for_device();
90  out << "RealSense device was connected...\n";
91 
92  setvbuf(stdout, NULL, _IONBF, 0); // unbuffering stdout
93 
94  auto fw_log_device = dev.as<rs2::firmware_logger>();
95 
96  bool using_parser = false;
97  if (!xml_full_file_path.empty())
98  {
99  ifstream f(xml_full_file_path);
100  if (f.good())
101  {
102  std::string xml_content((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>());
103  bool parser_initialized = fw_log_device.init_parser(xml_content);
104  if (parser_initialized)
105  using_parser = true;
106  }
107  }
108 
109  bool are_there_remaining_flash_logs_to_pull = true;
110  auto time_of_previous_polling_ms = std::chrono::high_resolution_clock::now();
111 
112  while (hub.is_connected(dev))
113  {
114  if (are_flash_logs_requested && !are_there_remaining_flash_logs_to_pull)
115  {
116  should_loop_end = true;
117  break;
118  }
119  auto log_message = fw_log_device.create_message();
120  bool result = false;
121  if (are_flash_logs_requested)
122  {
123  result = fw_log_device.get_flash_log(log_message);
124  }
125  else
126  {
127  result = fw_log_device.get_firmware_log(log_message);
128  }
129  if (result)
130  {
131  std::vector<string> fw_log_lines;
132  if (using_parser)
133  {
134  auto parsed_log = fw_log_device.create_parsed_message();
135  bool parsing_result = fw_log_device.parse_log(log_message, parsed_log);
136 
137  stringstream sstr;
138  sstr << datetime_string() << " " << parsed_log.timestamp() << " " << parsed_log.sequence_id()
139  << " " << parsed_log.severity() << " " << parsed_log.thread_name()
140  << " " << parsed_log.file_name() << " " << parsed_log.line()
141  << " " << parsed_log.message();
142 
143  fw_log_lines.push_back(sstr.str());
144  }
145  else
146  {
147  stringstream sstr;
148  sstr << datetime_string() << " FW_Log_Data:";
149  std::vector<uint8_t> msg_data = log_message.data();
150  for (int i = 0; i < msg_data.size(); ++i)
151  {
152  sstr << char2hex(msg_data[i]) << " ";
153  }
154  fw_log_lines.push_back(sstr.str());
155  }
156  for (auto& line : fw_log_lines)
157  out << line << endl;
158  }
159  else
160  {
161  if (are_flash_logs_requested)
162  {
163  are_there_remaining_flash_logs_to_pull = false;
164  }
165  }
166  auto num_of_messages = fw_log_device.get_number_of_fw_logs();
167  if (num_of_messages == 0)
168  {
170  auto time_since_previous_polling_ms = std::chrono::duration_cast<std::chrono::milliseconds>(current_time - time_of_previous_polling_ms).count();
171  if (time_since_previous_polling_ms < polling_interval_ms)
172  {
173  std::this_thread::sleep_for(chrono::milliseconds(polling_interval_ms - time_since_previous_polling_ms));
174  }
175  time_of_previous_polling_ms = std::chrono::high_resolution_clock::now();
176  }
177  }
178  }
179  catch (const error & e)
180  {
181  cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << endl;
182  }
183  }
184 
185  return EXIT_SUCCESS;
186 }
bool is_connected(const device &dev) const
Definition: rs_context.hpp:256
long long current_time()
static const char * log_to_file
Definition: model-views.h:161
Definition: cah-model.h:10
GLsizei const GLchar *const * string
GLdouble n
Definition: glext.h:1966
e
Definition: rmse.py:177
GLenum GLfloat * buffer
GLdouble t
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
GLdouble f
std::ostream & cout()
string datetime_string()
bool isSet() const
Definition: Arg.h:575
T & getValue()
Definition: ValueArg.h:322
void parse(int argc, const char *const *argv)
Definition: CmdLine.h:433
GLint GLsizei count
string char2hex(unsigned char n)
#define RS2_API_VERSION_STR
Definition: rs.h:43
std::ostream & cerr()
void add(Arg &a)
Definition: CmdLine.h:413
int main(int argc, char *argv[])
#define NULL
Definition: tinycthread.c:47
int i
GLuint res
Definition: glext.h:8856
Definition: Arg.h:57
device wait_for_device() const
Definition: rs_context.hpp:240
GLuint64EXT * result
Definition: glext.h:10921
const std::string & get_failed_function() const
Definition: rs_types.hpp:112


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40