Skip to content

Commit

Permalink
Refactor xml parser to parse key:value pairs.
Browse files Browse the repository at this point in the history
Print user warning in case of protocol error is identified
  • Loading branch information
ev-mp committed Feb 6, 2020
1 parent 183c87d commit a38f304
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tools/fw-logger/fw-logs-formating-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace fw_logger
}
}

std::unordered_map<std::string, std::vector<std::string>> fw_logs_formating_options::get_enums() const
std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> fw_logs_formating_options::get_enums() const
{
return _fw_logs_enum_names_list;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/fw-logger/fw-logs-formating-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ 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<std::string, std::vector<std::string>> get_enums() const;
std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> get_enums() const;
bool initialize_from_xml();

private:
friend fw_logs_xml_helper;
std::unordered_map<int, fw_log_event> _fw_logs_event_list;
std::unordered_map<int, std::string> _fw_logs_file_names_list;
std::unordered_map<int, std::string> _fw_logs_thread_names_list;
std::unordered_map<std::string, std::vector<std::string>> _fw_logs_enum_names_list;
std::unordered_map<std::string, std::vector<std::pair<int,std::string>>> _fw_logs_enum_names_list;

std::string _xml_full_file_path;
};
Expand Down
19 changes: 15 additions & 4 deletions tools/fw-logger/fw-logs-xml-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,32 @@ namespace fw_logger
if (attr.compare("Name") == 0)
{
string name_attr_str(attribute->value(), attribute->value() + attribute->value_size());
vector<string> values;
vector<std::pair<int,string>> 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<string, vector<string>>(name_attr_str, values));
logs_formating_options->_fw_logs_enum_names_list.insert(pair<string, vector<std::pair<int, std::string>>>(name_attr_str, values));
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions tools/fw-logger/string-formatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using namespace std;

namespace fw_logger
{
string_formatter::string_formatter(std::unordered_map<std::string, std::vector<std::string>> enums)
string_formatter::string_formatter(std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> enums)
:_enums(enums)
{
}
Expand Down Expand Up @@ -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 <vec.size())
auto res1 = regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, vec[exp_replace_it->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;
}
Expand Down
4 changes: 2 additions & 2 deletions tools/fw-logger/string-formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace fw_logger
class string_formatter
{
public:
string_formatter(std::unordered_map<std::string, std::vector<std::string>> enums);
string_formatter(std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> enums);
~string_formatter(void);

bool generate_message(const std::string& source, int num_of_params, const uint32_t* params, std::string* dest);

private:
bool replace_params(const std::string& source, const std::map<std::string, std::string>& exp_replace_map, const std::map<std::string, int>& enum_replace_map, std::string* dest);

std::unordered_map<std::string, std::vector<std::string>> _enums;
std::unordered_map<std::string, std::vector<std::pair<int, std::string>>> _enums;
};
}

0 comments on commit a38f304

Please sign in to comment.