PEL: peltool: Add const strings for section header

Added "Section Version", "Sub-section type" and "Created by" constant strings.

Updated all getJSON() functions to use the new constants.

Signed-off-by: Harisuddin Mohamed Isa <harisuddin@gmail.com>
Change-Id: I434192afed929972845c3cfb8876cc5ef784eec2
diff --git a/extensions/openpower-pels/extended_user_header.cpp b/extensions/openpower-pels/extended_user_header.cpp
index 282bb41..7b381e8 100644
--- a/extensions/openpower-pels/extended_user_header.cpp
+++ b/extensions/openpower-pels/extended_user_header.cpp
@@ -17,6 +17,7 @@
 
 #include "json_utils.hpp"
 #include "pel_types.hpp"
+#include "pel_values.hpp"
 
 #include <phosphor-logging/log.hpp>
 
@@ -25,6 +26,7 @@
 namespace pels
 {
 
+namespace pv = openpower::pels::pel_values;
 using namespace phosphor::logging;
 const size_t defaultSymptomIDWord = 3;
 const size_t symptomIDMaxSize = 80;
@@ -179,12 +181,10 @@
 std::optional<std::string> ExtendedUserHeader::getJSON() const
 {
     std::string json;
-    jsonInsert(json, "Section Version", getNumberString("%d", _header.version),
-               1);
-    jsonInsert(json, "Sub-section type", getNumberString("%d", _header.subType),
-               1);
-    jsonInsert(json, "Created by", getNumberString("0x%X", _header.componentID),
-               1);
+    jsonInsert(json, pv::sectionVer, getNumberString("%d", _header.version), 1);
+    jsonInsert(json, pv::subSection, getNumberString("%d", _header.subType), 1);
+    jsonInsert(json, pv::createdBy,
+               getNumberString("0x%X", _header.componentID), 1);
     jsonInsert(json, "Reporting Machine Type", machineTypeModel(), 1);
     jsonInsert(json, "Reporting Serial Number", trimEnd(machineSerialNumber()),
                1);
diff --git a/extensions/openpower-pels/failing_mtms.cpp b/extensions/openpower-pels/failing_mtms.cpp
index b45c05d..386f496 100644
--- a/extensions/openpower-pels/failing_mtms.cpp
+++ b/extensions/openpower-pels/failing_mtms.cpp
@@ -17,6 +17,7 @@
 
 #include "json_utils.hpp"
 #include "pel_types.hpp"
+#include "pel_values.hpp"
 
 #include <phosphor-logging/log.hpp>
 
@@ -25,6 +26,7 @@
 namespace pels
 {
 
+namespace pv = openpower::pels::pel_values;
 using namespace phosphor::logging;
 static constexpr uint8_t failingMTMSVersion = 0x01;
 
@@ -89,12 +91,10 @@
 std::optional<std::string> FailingMTMS::getJSON() const
 {
     std::string json;
-    jsonInsert(json, "Section Version", getNumberString("%d", _header.version),
-               1);
-    jsonInsert(json, "Sub-section type", getNumberString("%d", _header.subType),
-               1);
-    jsonInsert(json, "Created by", getNumberString("0x%X", _header.componentID),
-               1);
+    jsonInsert(json, pv::sectionVer, getNumberString("%d", _header.version), 1);
+    jsonInsert(json, pv::subSection, getNumberString("%d", _header.subType), 1);
+    jsonInsert(json, pv::createdBy,
+               getNumberString("0x%X", _header.componentID), 1);
     jsonInsert(json, "Machine Type Model", _mtms.machineTypeAndModel(), 1);
     jsonInsert(json, "Serial Number", trimEnd(_mtms.machineSerialNumber()), 1);
     json.erase(json.size() - 2);
diff --git a/extensions/openpower-pels/pel_values.hpp b/extensions/openpower-pels/pel_values.hpp
index 2424c63..d7d4226 100644
--- a/extensions/openpower-pels/pel_values.hpp
+++ b/extensions/openpower-pels/pel_values.hpp
@@ -26,6 +26,10 @@
 using PELFieldValue = std::tuple<uint32_t, const char*, const char*>;
 using PELValues = std::vector<PELFieldValue>;
 
+const std::string sectionVer = "Section Version";
+const std::string subSection = "Sub-section type";
+const std::string createdBy = "Created by";
+
 /**
  * @brief Helper function to get values from lookup tables.
  * @return std::string - the value
diff --git a/extensions/openpower-pels/private_header.cpp b/extensions/openpower-pels/private_header.cpp
index a0424d8..6555f98 100644
--- a/extensions/openpower-pels/private_header.cpp
+++ b/extensions/openpower-pels/private_header.cpp
@@ -97,35 +97,25 @@
             _commitTimestamp.hour, _commitTimestamp.minutes,
             _commitTimestamp.seconds);
     std::string phCommitTStr(tmpPhVal);
-    sprintf(tmpPhVal, "%c", _creatorID);
-    std::string creator(tmpPhVal);
+    std::string creator = getNumberString("%c", _creatorID);
     creator = pv::creatorIDs.count(creator) ? pv::creatorIDs.at(creator)
                                             : "Unknown CreatorID";
     std::string phCreatorVersionStr =
         std::string(reinterpret_cast<const char*>(_creatorVersion.version));
 
-    sprintf(tmpPhVal, "0x%X", _header.componentID);
-    std::string phCbStr(tmpPhVal);
-    sprintf(tmpPhVal, "%d", _header.subType);
-    std::string phStStr(tmpPhVal);
-    sprintf(tmpPhVal, "%d", privateHeaderVersion);
-    std::string phVerStr(tmpPhVal);
-    sprintf(tmpPhVal, "0x%X", _plid);
-    std::string phPlatformIDStr(tmpPhVal);
-    sprintf(tmpPhVal, "0x%X", _id);
-    std::string phLogEntryIDStr(tmpPhVal);
-    std::string phObmcIDStr = std::to_string(_obmcLogID);
     std::string ph;
-    jsonInsert(ph, "Section Version", phVerStr, 1);
-    jsonInsert(ph, "Sub-section type", phStStr, 1);
-    jsonInsert(ph, "Created by", phCbStr, 1);
+    jsonInsert(ph, pv::sectionVer, getNumberString("%d", privateHeaderVersion),
+               1);
+    jsonInsert(ph, pv::subSection, getNumberString("%d", _header.subType), 1);
+    jsonInsert(ph, pv::createdBy, getNumberString("0x%X", _header.componentID),
+               1);
     jsonInsert(ph, "Created at", phCreateTStr, 1);
     jsonInsert(ph, "Committed at", phCommitTStr, 1);
     jsonInsert(ph, "Creator Subsystem", creator, 1);
     jsonInsert(ph, "CSSVER", phCreatorVersionStr, 1);
-    jsonInsert(ph, "Platform Log Id", phPlatformIDStr, 1);
-    jsonInsert(ph, "Entry Id", phLogEntryIDStr, 1);
-    jsonInsert(ph, "BMC Event Log Id", phObmcIDStr, 1);
+    jsonInsert(ph, "Platform Log Id", getNumberString("0x%X", _plid), 1);
+    jsonInsert(ph, "Entry Id", getNumberString("0x%X", _id), 1);
+    jsonInsert(ph, "BMC Event Log Id", std::to_string(_obmcLogID), 1);
     ph.erase(ph.size() - 2);
 
     return ph;
diff --git a/extensions/openpower-pels/src.cpp b/extensions/openpower-pels/src.cpp
index 858c243..8d2fd0b 100644
--- a/extensions/openpower-pels/src.cpp
+++ b/extensions/openpower-pels/src.cpp
@@ -431,11 +431,9 @@
 std::optional<std::string> SRC::getJSON(message::Registry& registry) const
 {
     std::string ps;
-    jsonInsert(ps, "Section Version", getNumberString("%d", _header.version),
-               1);
-    jsonInsert(ps, "Sub-section type", getNumberString("%d", _header.subType),
-               1);
-    jsonInsert(ps, "Created by", getNumberString("0x%X", _header.componentID),
+    jsonInsert(ps, pv::sectionVer, getNumberString("%d", _header.version), 1);
+    jsonInsert(ps, pv::subSection, getNumberString("%d", _header.subType), 1);
+    jsonInsert(ps, pv::createdBy, getNumberString("0x%X", _header.componentID),
                1);
     jsonInsert(ps, "SRC Version", getNumberString("0x%02X", _version), 1);
     jsonInsert(ps, "SRC Format", getNumberString("0x%02X", _hexData[0] & 0xFF),
diff --git a/extensions/openpower-pels/user_data_json.cpp b/extensions/openpower-pels/user_data_json.cpp
index a86ccb3..f91d5f8 100644
--- a/extensions/openpower-pels/user_data_json.cpp
+++ b/extensions/openpower-pels/user_data_json.cpp
@@ -16,7 +16,9 @@
 
 #include "user_data_json.hpp"
 
+#include "json_utils.hpp"
 #include "pel_types.hpp"
+#include "pel_values.hpp"
 #include "user_data_formats.hpp"
 
 #include <fifo_map.hpp>
@@ -27,7 +29,7 @@
 
 namespace openpower::pels::user_data
 {
-
+namespace pv = openpower::pels::pel_values;
 using namespace phosphor::logging;
 
 // Use fifo_map as nlohmann::json's map. We are just ignoring the 'less'
@@ -51,12 +53,9 @@
                        const fifoJSON& json)
 {
     fifoJSON output;
-    output["Section Version"] = std::to_string(version);
-    output["Sub-section type"] = std::to_string(subType);
-
-    char value[10];
-    sprintf(value, "0x%04X", componentID);
-    output["Created by"] = std::string{value};
+    output[pv::sectionVer] = std::to_string(version);
+    output[pv::subSection] = std::to_string(subType);
+    output[pv::createdBy] = getNumberString("0x%04X", componentID);
 
     if (!json.is_object())
     {
diff --git a/extensions/openpower-pels/user_header.cpp b/extensions/openpower-pels/user_header.cpp
index e29ffdf..8984dc7 100644
--- a/extensions/openpower-pels/user_header.cpp
+++ b/extensions/openpower-pels/user_header.cpp
@@ -152,18 +152,11 @@
         hostState = iter->second;
     }
 
-    char tmpUhVal[8];
-    sprintf(tmpUhVal, "%d", userHeaderVersion);
-    std::string uhVerStr(tmpUhVal);
-    sprintf(tmpUhVal, "0x%X", _header.componentID);
-    std::string uhCbStr(tmpUhVal);
-    sprintf(tmpUhVal, "%d", _header.subType);
-    std::string uhStStr(tmpUhVal);
-
     std::string uh;
-    jsonInsert(uh, "Section Version", uhVerStr, 1);
-    jsonInsert(uh, "Sub-section type", uhStStr, 1);
-    jsonInsert(uh, "Log Committed by", uhCbStr, 1);
+    jsonInsert(uh, pv::sectionVer, getNumberString("%d", userHeaderVersion), 1);
+    jsonInsert(uh, pv::subSection, getNumberString("%d", _header.subType), 1);
+    jsonInsert(uh, "Log Committed by",
+               getNumberString("0x%X", _header.componentID), 1);
     jsonInsert(uh, "Subsystem", subsystem, 1);
     jsonInsert(uh, "Event Scope", eventScope, 1);
     jsonInsert(uh, "Event Severity", severity, 1);