Update handling of skipped log entries

We now skip log entries where the MessageId is not in the Message
Registry. However, since a blank entry is added to the array before the
MessageId is checked, it was leaving an empty {} in that location and
incrementing the Members@odata.count.

This change moves the MessageId check to before the entry count is
incremented to fix the Members@odata.count and moves the push_back()
into the array to the end, so an entry is only added if it's valid.

Tested:
Added three entries to the log as follows: good, bad, good.
The entries array showed only the two good entries with
Members@odata.count=2.
Using top=2, still showed only the two good entries with
Members@odata.count=2.
Skip did not work but appears to already be broken, so it was not
impacted by this change.
Confirmed that getting a single good entry still works.

Change-Id: I08ffc56af14004e2221acdb4a091bbfeb9c21c70
Signed-off-by: Jason M. Bills <jason.m.bills@intel.com>
diff --git a/redfish-core/lib/log_services.hpp b/redfish-core/lib/log_services.hpp
index e484649..050e7a7 100644
--- a/redfish-core/lib/log_services.hpp
+++ b/redfish-core/lib/log_services.hpp
@@ -1050,7 +1050,7 @@
 
 static int fillEventLogEntryJson(const std::string& logEntryID,
                                  const std::string& logEntry,
-                                 nlohmann::json& logEntryJson)
+                                 nlohmann::json::object_t& logEntryJson)
 {
     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
     // First get the Timestamp
@@ -1196,16 +1196,6 @@
             bool firstEntry = true;
             while (std::getline(logStream, logEntry))
             {
-                entryCount++;
-                // Handle paging using skip (number of entries to skip
-                // from the start) and top (number of entries to
-                // display)
-                if (entryCount <= delegatedQuery.skip ||
-                    entryCount > delegatedQuery.skip + delegatedQuery.top)
-                {
-                    continue;
-                }
-
                 std::string idStr;
                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
                 {
@@ -1213,13 +1203,28 @@
                 }
                 firstEntry = false;
 
-                logEntryArray.push_back({});
-                nlohmann::json& bmcLogEntry = logEntryArray.back();
+                nlohmann::json::object_t bmcLogEntry;
                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
                 {
                     messages::internalError(asyncResp->res);
                     return;
                 }
+
+                if (bmcLogEntry.empty())
+                {
+                    continue;
+                }
+
+                entryCount++;
+                // Handle paging using skip (number of entries to skip from the
+                // start) and top (number of entries to display)
+                if (entryCount <= delegatedQuery.skip ||
+                    entryCount > delegatedQuery.skip + delegatedQuery.top)
+                {
+                    continue;
+                }
+
+                logEntryArray.push_back(std::move(bmcLogEntry));
             }
         }
         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
@@ -1277,12 +1282,14 @@
 
                 if (idStr == targetID)
                 {
-                    if (fillEventLogEntryJson(idStr, logEntry,
-                                              asyncResp->res.jsonValue) != 0)
+                    nlohmann::json::object_t bmcLogEntry;
+                    if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) !=
+                        0)
                     {
                         messages::internalError(asyncResp->res);
                         return;
                     }
+                    asyncResp->res.jsonValue = std::move(bmcLogEntry);
                     return;
                 }
             }