tools/fw-logger/fw-logs-xml-helper.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 #include "fw-logs-xml-helper.h"
4 #include <string.h>
5 #include <fstream>
6 #include <iostream>
7 #include <memory>
8 
9 using namespace std;
10 
11 namespace fw_logger
12 {
13  fw_logs_xml_helper::fw_logs_xml_helper(string xml_full_file_path)
14  : _init_done(false),
15  _xml_full_file_path(xml_full_file_path)
16  {}
17 
18 
20  {
21  // TODO: Add cleanup code
22  }
23 
25  {
26  if (_init_done)
27  {
28  *node = _xml_doc.first_node();
29  return true;
30  }
31 
32  return false;
33  }
34 
36  {
37  try
38  {
39  if (_xml_full_file_path.empty())
40  return false;
41 
42  rapidxml::file<> xml_file(_xml_full_file_path.c_str());
43 
44  _document_buffer.resize(xml_file.size() + 2);
45  memcpy(_document_buffer.data(), xml_file.data(), xml_file.size());
46  _document_buffer[xml_file.size()] = '\0';
47  _document_buffer[xml_file.size() + 1] = '\0';
48  _xml_doc.parse<0>(_document_buffer.data());
49 
50  return true;
51  }
52  catch (...)
53  {
54  _document_buffer.clear();
55  throw;
56  }
57 
58  return false;
59  }
60 
62  {
64  return _init_done;
65  }
66 
68  {
69  xml_node<> *xml_root_node_list;
70 
71  if (!init())
72  return false;
73 
74  if (!get_root_node(&xml_root_node_list))
75  {
76  return false;
77  }
78 
79  string root_name(xml_root_node_list->name(), xml_root_node_list->name() + xml_root_node_list->name_size());
80 
81  // check if Format is the first root name.
82  if (root_name.compare("Format") != 0)
83  return false;
84 
85  xml_node<>* events_node = xml_root_node_list->first_node();
86 
87 
88  if (!build_meta_data_structure(events_node, log_meta_data))
89  return false;
90 
91  return true;
92  }
93 
94 
95  bool fw_logs_xml_helper::build_meta_data_structure(xml_node<> *xml_node_list_of_events, fw_logs_formating_options* logs_formating_options)
96  {
97  node_type res = none;
98  int id{};
99  int num_of_params{};
100  string line;
101 
102  // loop through all elements in the Format.
103  for (xml_node<>* node = xml_node_list_of_events; node; node = node->next_sibling())
104  {
105  line.clear();
106  res = get_next_node(node, &id, &num_of_params, &line);
107  if (res == event)
108  {
109  fw_log_event log_event(num_of_params, line);
110  logs_formating_options->_fw_logs_event_list.insert(pair<int, fw_log_event>(id, log_event));
111  }
112  else if (res == file)
113  {
114  logs_formating_options->_fw_logs_file_names_list.insert(kvp(id, line));
115  }
116  else if (res == thread)
117  {
118  logs_formating_options->_fw_logs_thread_names_list.insert(kvp(id, line));
119  }
120  else if (res == enums)
121  {
122  for (xml_node<>* enum_node = node->first_node(); enum_node; enum_node = enum_node->next_sibling())
123  {
124  for (xml_attribute<>* attribute = enum_node->first_attribute(); attribute; attribute = attribute->next_attribute())
125  {
126  string attr(attribute->name(), attribute->name() + attribute->name_size());
127  if (attr.compare("Name") == 0)
128  {
129  string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
130  vector<kvp> xml_kvp;
131 
132  for (xml_node<>* enum_value_node = enum_node->first_node(); enum_value_node; enum_value_node = enum_value_node->next_sibling())
133  {
134  int key = 0;
135  string value_str;
136  for (xml_attribute<>* attribute = enum_value_node->first_attribute(); attribute; attribute = attribute->next_attribute())
137  {
138  string attr(attribute->name(), attribute->name() + attribute->name_size());
139  if (attr.compare("Value") == 0)
140  {
141  value_str = std::string(attribute->value(), attribute->value() + attribute->value_size());
142  }
143  if (attr.compare("Key") == 0)
144  {
145  try
146  {
147  auto key_str = std::string(attribute->value());
148  key = std::stoi(key_str, nullptr);
149  }
150  catch (...) {}
151  }
152  }
153  xml_kvp.push_back(std::make_pair(key, value_str));
154  }
155  logs_formating_options->_fw_logs_enum_names_list.insert(pair<string, vector<kvp>>(name_attr_str, xml_kvp));
156  }
157  }
158  }
159  }
160  else
161  return false;
162  }
163 
164  return true;
165  }
166 
168  {
169 
170  string tag(node->name(), node->name() + node->name_size());
171 
172  if (tag.compare("Event") == 0)
173  {
174  if (get_event_node(node, id, num_of_params, line))
175  return event;
176  }
177  else if (tag.compare("File") == 0)
178  {
179  if (get_file_node(node, id, line))
180  return file;
181  }
182  else if (tag.compare("Thread") == 0)
183  {
184  if (get_thread_node(node, id, line))
185  return thread;
186  }
187  else if (tag.compare("Enums") == 0)
188  {
189  return enums;
190  }
191  return none;
192  }
193 
194  bool fw_logs_xml_helper::get_enum_name_node(xml_node<>* node_file, int* thread_id, string* enum_name)
195  {
196  for (xml_attribute<>* attribute = node_file->first_attribute(); attribute; attribute = attribute->next_attribute())
197  {
198  string attr(attribute->name(), attribute->name() + attribute->name_size());
199 
200  if (attr.compare("Name") == 0)
201  {
202  string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
203  *enum_name = name_attr_str;
204  continue;
205  }
206  else
207  return false;
208  }
209 
210  return true;
211  }
212  bool fw_logs_xml_helper::get_enum_value_node(xml_node<>* node_file, int* thread_id, string* enum_name)
213  {
214  for (xml_attribute<>* attribute = node_file->first_attribute(); attribute; attribute = attribute->next_attribute())
215  {
216  string attr(attribute->name(), attribute->name() + attribute->name_size());
217 
218  if (attr.compare("Value") == 0)
219  {
220  string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
221  *enum_name = name_attr_str;
222  continue;
223  }
224  else
225  return false;
226  }
227 
228  return true;
229  }
230  bool fw_logs_xml_helper::get_thread_node(xml_node<>* node_file, int* thread_id, string* thread_name)
231  {
232  for (xml_attribute<>* attribute = node_file->first_attribute(); attribute; attribute = attribute->next_attribute())
233  {
234  string attr(attribute->name(), attribute->name() + attribute->name_size());
235 
236  if (attr.compare("id") == 0)
237  {
238  string id_attr_str(attribute->value(), attribute->value() + attribute->value_size());
239  *thread_id = stoi(id_attr_str);
240  continue;
241  }
242  else if (attr.compare("Name") == 0)
243  {
244  string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
245  *thread_name = name_attr_str;
246  continue;
247  }
248  else
249  return false;
250  }
251 
252  return true;
253  }
254 
255  bool fw_logs_xml_helper::get_file_node(xml_node<>* node_file, int* file_id, string* file_name)
256  {
257  for (xml_attribute<>* attribute = node_file->first_attribute(); attribute; attribute = attribute->next_attribute())
258  {
259  string attr(attribute->name(), attribute->name() + attribute->name_size());
260 
261  if (attr.compare("id") == 0)
262  {
263  string id_attr_str(attribute->value(), attribute->value() + attribute->value_size());
264  *file_id = stoi(id_attr_str);
265  continue;
266  }
267  else if (attr.compare("Name") == 0)
268  {
269  string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
270  *file_name = name_attr_str;
271  continue;
272  }
273  else
274  return false;
275  }
276  return true;
277  }
278 
279  bool fw_logs_xml_helper::get_event_node(xml_node<>* node_event, int* event_id, int* num_of_params, string* line)
280  {
281  for (xml_attribute<>* attribute = node_event->first_attribute(); attribute; attribute = attribute->next_attribute())
282  {
283  string attr(attribute->name(), attribute->name() + attribute->name_size());
284 
285  if (attr.compare("id") == 0)
286  {
287  string id_attr_str(attribute->value(), attribute->value() + attribute->value_size());
288  *event_id = stoi(id_attr_str);
289  continue;
290  }
291  else if (attr.compare("numberOfArguments") == 0)
292  {
293  string num_of_args_attr_str(attribute->value(), attribute->value() + attribute->value_size());
294  *num_of_params = stoi(num_of_args_attr_str);
295  continue;
296  }
297  else if (attr.compare("format") == 0)
298  {
299  string format_attr_str(attribute->value(), attribute->value() + attribute->value_size());
300  *line = format_attr_str;
301  continue;
302  }
303  else
304  return false;
305  }
306  return true;
307  }
308 }
std::unordered_map< std::string, std::vector< std::pair< int, std::string > > > _fw_logs_enum_names_list
boost_foreach_argument_dependent_lookup_hack tag
Definition: foreach_fwd.hpp:31
bool get_file_node(xml_node<> *node_file, int *file_id, std::string *file_name)
Represents data loaded from a file.
std::size_t name_size() const
Definition: rapidxml.hpp:681
std::unordered_map< int, std::string > _fw_logs_thread_names_list
GLsizei const GLchar *const * string
void parse(Ch *text)
Definition: rapidxml.hpp:1381
std::string name
xml_node< Ch > * first_node(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:936
GLuint64 key
Definition: glext.h:8966
std::unordered_map< int, fw_log_event > _fw_logs_event_list
bool get_enum_value_node(xml_node<> *node_file, int *thread_id, std::string *enum_name)
std::string value
Ch * name() const
Definition: rapidxml.hpp:673
xml_attribute< Ch > * first_attribute(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1025
std::unordered_map< int, std::string > _fw_logs_file_names_list
struct _cl_event * event
Definition: glext.h:2991
xml_node< Ch > * next_sibling(const Ch *name=0, std::size_t name_size=0, bool case_sensitive=true) const
Definition: rapidxml.hpp:1004
bool get_thread_node(xml_node<> *node_file, int *thread_id, std::string *thread_name)
bool build_log_meta_data(fw_logs_formating_options *logs_formating_options)
bool get_enum_name_node(xml_node<> *node_file, int *thread_id, std::string *thread_name)
bool build_meta_data_structure(xml_node<> *xml_node_list_of_events, fw_logs_formating_options *logs_formating_options)
node_type get_next_node(xml_node<> *xml_node_list_of_events, int *id, int *num_of_params, std::string *line)
std::pair< int, std::string > kvp
int stoi(const std::string &value)
Definition: enums.py:1
GLuint res
Definition: glext.h:8856
bool get_event_node(xml_node<> *node_event, int *event_id, int *num_of_params, std::string *line)


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