From a38f3048c585823111fa410be1f4e6799ab3072b Mon Sep 17 00:00:00 2001 From: Evgeni Raikhel Date: Thu, 6 Feb 2020 18:30:09 +0200 Subject: [PATCH] Refactor xml parser to parse key:value pairs. Print user warning in case of protocol error is identified --- tools/fw-logger/fw-logs-formating-options.cpp | 2 +- tools/fw-logger/fw-logs-formating-options.h | 4 ++-- tools/fw-logger/fw-logs-xml-helper.cpp | 19 +++++++++++++++---- tools/fw-logger/string-formatter.cpp | 17 ++++++++++++----- tools/fw-logger/string-formatter.h | 4 ++-- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/tools/fw-logger/fw-logs-formating-options.cpp b/tools/fw-logger/fw-logs-formating-options.cpp index 399e15bbf71..24a19ba4b3c 100644 --- a/tools/fw-logger/fw-logs-formating-options.cpp +++ b/tools/fw-logger/fw-logs-formating-options.cpp @@ -79,7 +79,7 @@ namespace fw_logger } } - std::unordered_map> fw_logs_formating_options::get_enums() const + std::unordered_map>> fw_logs_formating_options::get_enums() const { return _fw_logs_enum_names_list; } diff --git a/tools/fw-logger/fw-logs-formating-options.h b/tools/fw-logger/fw-logs-formating-options.h index 7dc68e07713..62c39532526 100644 --- a/tools/fw-logger/fw-logs-formating-options.h +++ b/tools/fw-logger/fw-logs-formating-options.h @@ -33,7 +33,7 @@ namespace fw_logger bool get_event_data(int id, fw_log_event* log_event_data) const; bool get_file_name(int id, std::string* file_name) const; bool get_thread_name(uint32_t thread_id, std::string* thread_name) const; - std::unordered_map> get_enums() const; + std::unordered_map>> get_enums() const; bool initialize_from_xml(); private: @@ -41,7 +41,7 @@ namespace fw_logger std::unordered_map _fw_logs_event_list; std::unordered_map _fw_logs_file_names_list; std::unordered_map _fw_logs_thread_names_list; - std::unordered_map> _fw_logs_enum_names_list; + std::unordered_map>> _fw_logs_enum_names_list; std::string _xml_full_file_path; }; diff --git a/tools/fw-logger/fw-logs-xml-helper.cpp b/tools/fw-logger/fw-logs-xml-helper.cpp index 3575da965a5..6bf931780ed 100644 --- a/tools/fw-logger/fw-logs-xml-helper.cpp +++ b/tools/fw-logger/fw-logs-xml-helper.cpp @@ -127,21 +127,32 @@ namespace fw_logger if (attr.compare("Name") == 0) { string name_attr_str(attribute->value(), attribute->value() + attribute->value_size()); - vector values; + vector> values; for (xml_node<>* enum_value_node = enum_node->first_node(); enum_value_node; enum_value_node = enum_value_node->next_sibling()) { + int key = 0; + string value_str; for (xml_attribute<>* attribute = enum_value_node->first_attribute(); attribute; attribute = attribute->next_attribute()) { string attr(attribute->name(), attribute->name() + attribute->name_size()); if (attr.compare("Value") == 0) { - string value_str(attribute->value(), attribute->value() + attribute->value_size()); - values.push_back(value_str); + value_str = std::string(attribute->value(), attribute->value() + attribute->value_size()); + } + if (attr.compare("Key") == 0) + { + try + { + auto key_str = std::string(attribute->value()); + key = std::stoi(key_str, nullptr); + } + catch (...) {} } } + values.push_back(std::make_pair(key, value_str)); } - logs_formating_options->_fw_logs_enum_names_list.insert(pair>(name_attr_str, values)); + logs_formating_options->_fw_logs_enum_names_list.insert(pair>>(name_attr_str, values)); } } } diff --git a/tools/fw-logger/string-formatter.cpp b/tools/fw-logger/string-formatter.cpp index 414d1c2d270..3f7d86c0edf 100644 --- a/tools/fw-logger/string-formatter.cpp +++ b/tools/fw-logger/string-formatter.cpp @@ -10,7 +10,7 @@ using namespace std; namespace fw_logger { - string_formatter::string_formatter(std::unordered_map> enums) + string_formatter::string_formatter(std::unordered_map>> enums) :_enums(enums) { } @@ -100,13 +100,20 @@ namespace fw_logger { auto vec = _enums[enum_name]; regex e3 = e; + int val = exp_replace_it->second; // Validate user's input is within the enumerated values - if (exp_replace_it->second >=0 && exp_replace_it->second second]); + auto it = std::find_if(vec.begin(), vec.end(), [val](auto& pair){ return pair.first == val; }); + if (it != vec.end()) + { + regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, it->second/*vec[exp_replace_it->second]*/); + } else { - std::cout << "Protocol Error recognized!\nImproper log message received: " << source_temp - << ", invalid parameter: " << exp_replace_it->second << std::endl; + stringstream s; + s << "Protocol Error recognized!\nImproper log message received: " << source_temp + << ", invalid parameter: " << exp_replace_it->second << ".\n The range of supported values is \n"; + for_each(vec.begin(), vec.end(), [&s](auto& pair) { s << pair.first << ":" << pair.second << " ,"; }); + std::cout << s.str().c_str() << std::endl;; } source_temp = destTemp; } diff --git a/tools/fw-logger/string-formatter.h b/tools/fw-logger/string-formatter.h index 8b9a5c9cd83..ef677e7f32b 100644 --- a/tools/fw-logger/string-formatter.h +++ b/tools/fw-logger/string-formatter.h @@ -12,7 +12,7 @@ namespace fw_logger class string_formatter { public: - string_formatter(std::unordered_map> enums); + string_formatter(std::unordered_map>> enums); ~string_formatter(void); bool generate_message(const std::string& source, int num_of_params, const uint32_t* params, std::string* dest); @@ -20,6 +20,6 @@ namespace fw_logger private: bool replace_params(const std::string& source, const std::map& exp_replace_map, const std::map& enum_replace_map, std::string* dest); - std::unordered_map> _enums; + std::unordered_map>> _enums; }; }