PEL: Print the JSON formatted UserData

Override the getJSON() function in the  UserData section so it will
support peltool pretty printing when the data has been stored as JSON
(component phosphor-logging, subtype 1 as defined in
user_data_formats.hpp).

It first converts the original JSON string to a nlohmann::json object to
validate it is valid JSON and then uses nlohmann::json's stream <<
operator to convert it into a pretty string.

This also sets up a framework where other UserData parsing functions can
be called from, and it is all only compiled into peltool, and not into
phosphor-log-manager.

The UserData section created out of the OpenBMC event log's
AdditionalData property already makes use of this format.

Here are some example outputs.  Note that the 'Data' key is added by
this code for JSON that isn't already an object (dict) to make it one.

"User Data": {
    "OPERATION": "something",
    "REGISTER_FFDC": "REG1=0x8|REG2=0x9|REG3=0x55",
    "RETURN_VALUE": "-12",
    "TIMEOUT_IN_MSEC": "100",
    "_PID": "993"
},

"User Data": {
    "Data": [
        "OPERATION",
        "REGISTER_FFDC",
        "RETURN_VALUE",
        "TIMEOUT_IN_MSEC",
        "_PID"
    ]
},

"User Data": {
    "Data": "This is a string"
}

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I7b034ac24339b5b019db3b57d8e71cb03559363e
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index 74d46bd..262865f 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -214,7 +214,8 @@
 
 } // namespace util
 
-void PEL::printSectionInJSON(const Section& section, std::string& buf) const
+void PEL::printSectionInJSON(const Section& section, std::string& buf,
+                             std::map<uint16_t, size_t>& pluralSections) const
 {
     char tmpB[5];
     uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8),
@@ -224,6 +225,15 @@
     std::string sectionName = pv::sectionTitles.count(sectionID)
                                   ? pv::sectionTitles.at(sectionID)
                                   : "Unknown Section";
+
+    // Add a count if there are multiple of this type of section
+    auto count = pluralSections.find(section.header().id);
+    if (count != pluralSections.end())
+    {
+        sectionName += " " + std::to_string(count->second);
+        count->second++;
+    }
+
     if (section.valid())
     {
         auto json = section.getJSON();
@@ -248,14 +258,46 @@
     }
 }
 
+std::map<uint16_t, size_t> PEL::getPluralSections() const
+{
+    std::map<uint16_t, size_t> sectionCounts;
+
+    for (const auto& section : optionalSections())
+    {
+        if (sectionCounts.find(section->header().id) == sectionCounts.end())
+        {
+            sectionCounts[section->header().id] = 1;
+        }
+        else
+        {
+            sectionCounts[section->header().id]++;
+        }
+    }
+
+    std::map<uint16_t, size_t> sections;
+    for (const auto& [id, count] : sectionCounts)
+    {
+        if (count > 1)
+        {
+            // Start with 0 here and printSectionInJSON()
+            // will increment it as it goes.
+            sections.emplace(id, 0);
+        }
+    }
+
+    return sections;
+}
+
 void PEL::toJSON() const
 {
+    auto sections = getPluralSections();
+
     std::string buf = "{";
-    printSectionInJSON(*(_ph.get()), buf);
-    printSectionInJSON(*(_uh.get()), buf);
+    printSectionInJSON(*(_ph.get()), buf, sections);
+    printSectionInJSON(*(_uh.get()), buf, sections);
     for (auto& section : this->optionalSections())
     {
-        printSectionInJSON(*(section.get()), buf);
+        printSectionInJSON(*(section.get()), buf, sections);
     }
     buf += "}";
     std::size_t found = buf.rfind(",");