Use fillMessageArgs in log services

There's two instances of log service that the method from
fillMessageArgs was copy/pasted to.  This commit moves them to using the
common, unit tested function.

Tested:
 - Verified the GET on Event logs and works as expected.
   curl -k -u $USER:$PASSWORD --noproxy "*"
   https://${BMCIP}/redfish/v1/Systems/system/LogServices/EventLog/Entries -X GET

 - Verified the PostCode fetching and works as expected.
   curl -k -u $USER:$PASSWORD --noproxy "*"
   https://${BMCIP}/redfish/v1/Systems/system/LogServices/PostCodes/Entries -X GET

Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I24eb13b83eff41e7dfd98ea9ecc160d578d2faa6
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index 323c22a..fedf7ad 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -1290,48 +1290,29 @@
     std::vector<std::string> logEntryFields;
     bmcweb::split(logEntryFields, entry, ',');
     // We need at least a MessageId to be valid
-    if (logEntryFields.empty())
+    auto logEntryIter = logEntryFields.begin();
+    if (logEntryIter == logEntryFields.end())
     {
         return LogParseError::parseFailed;
     }
-    std::string& messageID = logEntryFields[0];
-
+    std::string& messageID = *logEntryIter;
     // Get the Message from the MessageRegistry
     const registries::Message* message = registries::getMessage(messageID);
 
+    logEntryIter++;
     if (message == nullptr)
     {
         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
         return LogParseError::messageIdNotInRegistry;
     }
 
-    std::string msg = message->message;
-
-    // Get the MessageArgs from the log if there are any
-    std::span<std::string> messageArgs;
-    if (logEntryFields.size() > 1)
+    std::vector<std::string_view> messageArgs(logEntryIter,
+                                              logEntryFields.end());
+    std::string msg = redfish::registries::fillMessageArgs(messageArgs,
+                                                           message->message);
+    if (msg.empty())
     {
-        std::string& messageArgsStart = logEntryFields[1];
-        // If the first string is empty, assume there are no MessageArgs
-        std::size_t messageArgsSize = 0;
-        if (!messageArgsStart.empty())
-        {
-            messageArgsSize = logEntryFields.size() - 1;
-        }
-
-        messageArgs = {&messageArgsStart, messageArgsSize};
-
-        // Fill the MessageArgs into the Message
-        int i = 0;
-        for (const std::string& messageArg : messageArgs)
-        {
-            std::string argStr = "%" + std::to_string(++i);
-            size_t argPos = msg.find(argStr);
-            if (argPos != std::string::npos)
-            {
-                msg.replace(argPos, argStr.length(), messageArg);
-            }
-        }
+        return LogParseError::parseFailed;
     }
 
     // Get the Created time from the timestamp. The log timestamp is in RFC3339
@@ -3756,26 +3737,20 @@
         timeOffsetStr << std::setprecision(4);
         // Add double to stream
         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
-        std::vector<std::string> messageArgs = {
-            std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
 
-        // Get MessageArgs template from message registry
-        std::string msg;
-        if (message != nullptr)
+        std::string bootIndexStr = std::to_string(bootIndex);
+        std::string timeOffsetString = timeOffsetStr.str();
+        std::string hexCodeStr = hexCode.str();
+
+        std::array<std::string_view, 3> messageArgs = {
+            bootIndexStr, timeOffsetString, hexCodeStr};
+
+        std::string msg =
+            redfish::registries::fillMessageArgs(messageArgs, message->message);
+        if (msg.empty())
         {
-            msg = message->message;
-
-            // fill in this post code value
-            int i = 0;
-            for (const std::string& messageArg : messageArgs)
-            {
-                std::string argStr = "%" + std::to_string(++i);
-                size_t argPos = msg.find(argStr);
-                if (argPos != std::string::npos)
-                {
-                    msg.replace(argPos, argStr.length(), messageArg);
-                }
-            }
+            messages::internalError(asyncResp->res);
+            return false;
         }
 
         // Get Severity template from message registry
@@ -3795,7 +3770,7 @@
         bmcLogEntry["Id"] = postcodeEntryID;
         bmcLogEntry["Message"] = std::move(msg);
         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
-        bmcLogEntry["MessageArgs"] = std::move(messageArgs);
+        bmcLogEntry["MessageArgs"] = messageArgs;
         bmcLogEntry["EntryType"] = "Event";
         bmcLogEntry["Severity"] = std::move(severity);
         bmcLogEntry["Created"] = entryTimeStr;