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
Fix GCC build
  • Loading branch information
ev-mp committed Feb 6, 2020
1 parent 183c87d commit 2d20e54
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 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
23 changes: 15 additions & 8 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 @@ -84,7 +84,7 @@ namespace fw_logger
string st_regular_exp = "[a-zA-Z]+";
regex e1(st_regular_exp);

for(auto exp = 0; exp<m.size(); exp++)
for(size_t exp = 0; exp<m.size(); exp++)
{
string str = m[exp];

Expand All @@ -93,20 +93,27 @@ namespace fw_logger
regex e2 = e1;
std::regex_search(str, m1, std::regex(e2));

for (auto exp = 0; exp < m1.size(); exp++)
for (size_t exp = 0; exp < m1.size(); exp++)
{
enum_name = m1[exp];
if (_enums.size()>0 && _enums.find(enum_name) != _enums.end())
{
auto vec = _enums[enum_name];
regex e3 = e;
// 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]);
// Verify user's input is within the enumerated range
int val = exp_replace_it->second;
auto it = std::find_if(vec.begin(), vec.end(), [val](std::pair<int, std::string>& kvp){ return kvp.first == val; });
if (it != vec.end())
{
regex_replace(back_inserter(destTemp), source_temp.begin(), source_temp.end(), e3, 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](std::pair<int, std::string>& kvp) { s << kvp.first << ":" << kvp.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 2d20e54

Please sign in to comment.