PEL: Save invalid host PELs to a file

To aid in debug of when the host sends down an invalid PEL, save the PEL
to the file 'badPEL' in the PEL repo path.  This only saves a single one
at a time to keep things simple.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I3b630433712d63cadd971aa87a02f76a9a366849
diff --git a/extensions/openpower-pels/manager.cpp b/extensions/openpower-pels/manager.cpp
index fb0efb3..66f5ee5 100644
--- a/extensions/openpower-pels/manager.cpp
+++ b/extensions/openpower-pels/manager.cpp
@@ -149,6 +149,13 @@
 
         _eventLogger.log("org.open_power.Logging.Error.BadHostPEL",
                          Entry::Level::Error, ad);
+
+        // Save it to a file for debug in the lab.  Just keep the latest.
+        // Not adding it to the PEL because it could already be max size
+        // and don't want to truncate an already invalid PEL.
+        std::ofstream pelFile{getPELRepoPath() / "badPEL"};
+        pelFile.write(reinterpret_cast<const char*>(pelData.data()),
+                      pelData.size());
     }
 }
 
diff --git a/test/openpower-pels/pel_manager_test.cpp b/test/openpower-pels/pel_manager_test.cpp
index 530dfd5..00427c2 100644
--- a/test/openpower-pels/pel_manager_test.cpp
+++ b/test/openpower-pels/pel_manager_test.cpp
@@ -176,6 +176,10 @@
     EXPECT_EQ(logger.ad["SRC"], (*invalidPEL.primarySRC())->asciiString());
     EXPECT_EQ(logger.ad["PEL_SIZE"], std::to_string(data.size()));
 
+    // Check that the bad PEL data was saved to a file.
+    auto badPELData = readPELFile(getPELRepoPath() / "badPEL");
+    EXPECT_EQ(*badPELData, data);
+
     fs::remove_all(pelFilename.parent_path());
 }