PEL: Create class to read from the journal

Create a Journal class that can extract messages out of the journal and
return them as a vector of strings that look like:

"Dec 14 15:58:17 systemd[1]: systemd-tmpfiles-clean.service: Deactivated
successfully."

It can either grab the previous N entries, or the previous N entries
that match a specific SYSLOG_IDENTIFIER value.

The class follows the same strategy as the DataInterface class where a
base class pointer is passed into the PEL Manager class so that during
unit test it can be mocked.

Future commits will capture the journal into PEL UserData sections.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I9f4bb304c4b213165049fa00de2e62f962ae67f1
diff --git a/extensions/openpower-pels/entry_points.cpp b/extensions/openpower-pels/entry_points.cpp
index 156c666..c904afb 100644
--- a/extensions/openpower-pels/entry_points.cpp
+++ b/extensions/openpower-pels/entry_points.cpp
@@ -17,6 +17,7 @@
 #include "elog_entry.hpp"
 #include "event_logger.hpp"
 #include "extensions.hpp"
+#include "journal.hpp"
 #include "manager.hpp"
 #include "pldm_interface.hpp"
 
@@ -44,16 +45,18 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<DataInterface>(logManager.getBus());
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<Journal>();
+
 #ifndef DONT_SEND_PELS_TO_HOST
     std::unique_ptr<HostInterface> hostIface = std::make_unique<PLDMInterface>(
         logManager.getBus().get_event(), *(dataIface.get()));
 
     manager = std::make_unique<Manager>(logManager, std::move(dataIface),
-                                        std::move(logger),
+                                        std::move(logger), std::move(journal),
                                         std::move(hostIface));
 #else
     manager = std::make_unique<Manager>(logManager, std::move(dataIface),
-                                        std::move(logger));
+                                        std::move(logger), std::move(journal));
 #endif
 
 #ifdef PEL_ENABLE_PHAL
diff --git a/extensions/openpower-pels/journal.cpp b/extensions/openpower-pels/journal.cpp
new file mode 100644
index 0000000..0a728f0
--- /dev/null
+++ b/extensions/openpower-pels/journal.cpp
@@ -0,0 +1,176 @@
+/**
+ * Copyright © 2023 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "journal.hpp"
+
+#include <phosphor-logging/log.hpp>
+
+#include <stdexcept>
+#include <thread>
+
+namespace openpower::pels
+{
+
+using namespace phosphor::logging;
+
+/**
+ * @class JournalCloser
+ *
+ * Closes the journal on destruction
+ */
+class JournalCloser
+{
+  public:
+    JournalCloser() = delete;
+    JournalCloser(const JournalCloser&) = delete;
+    JournalCloser(JournalCloser&&) = delete;
+    JournalCloser& operator=(const JournalCloser&) = delete;
+    JournalCloser& operator=(JournalCloser&&) = delete;
+
+    explicit JournalCloser(sd_journal* journal) : journal{journal} {}
+
+    ~JournalCloser()
+    {
+        sd_journal_close(journal);
+    }
+
+  private:
+    sd_journal* journal{nullptr};
+};
+
+std::vector<std::string> Journal::getMessages(const std::string& syslogID,
+                                              size_t maxMessages) const
+{
+    // The message registry JSON schema will also fail if a zero is in the JSON
+    if (0 == maxMessages)
+    {
+        log<level::ERR>(
+            "maxMessages value of zero passed into Journal::getMessages");
+        return std::vector<std::string>{};
+    }
+
+    sd_journal* journal;
+    int rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
+    if (rc < 0)
+    {
+        throw std::runtime_error{std::string{"Failed to open journal: "} +
+                                 strerror(-rc)};
+    }
+
+    JournalCloser closer{journal};
+
+    if (!syslogID.empty())
+    {
+        std::string match{"SYSLOG_IDENTIFIER=" + syslogID};
+
+        rc = sd_journal_add_match(journal, match.c_str(), 0);
+        if (rc < 0)
+        {
+            throw std::runtime_error{
+                std::string{"Failed to add journal match: "} + strerror(-rc)};
+        }
+    }
+
+    // Loop through matching entries from newest to oldest
+    std::vector<std::string> messages;
+    messages.reserve(maxMessages);
+    std::string sID, pid, message, timeStamp, line;
+
+    SD_JOURNAL_FOREACH_BACKWARDS(journal)
+    {
+        timeStamp = getTimeStamp(journal);
+        sID = getFieldValue(journal, "SYSLOG_IDENTIFIER");
+        pid = getFieldValue(journal, "_PID");
+        message = getFieldValue(journal, "MESSAGE");
+
+        line = timeStamp + " " + sID + "[" + pid + "]: " + message;
+        messages.emplace(messages.begin(), line);
+
+        if (messages.size() >= maxMessages)
+        {
+            break;
+        }
+    }
+
+    return messages;
+}
+
+std::string Journal::getFieldValue(sd_journal* journal,
+                                   const std::string& field) const
+{
+    std::string value{};
+
+    const void* data{nullptr};
+    size_t length{0};
+    int rc = sd_journal_get_data(journal, field.c_str(), &data, &length);
+    if (rc < 0)
+    {
+        if (-rc == ENOENT)
+        {
+            // Current entry does not include this field; return empty value
+            return value;
+        }
+        else
+        {
+            throw std::runtime_error{
+                std::string{"Failed to read journal entry field: "} +
+                strerror(-rc)};
+        }
+    }
+
+    // Get value from field data.  Field data in format "FIELD=value".
+    std::string dataString{static_cast<const char*>(data), length};
+    std::string::size_type pos = dataString.find('=');
+    if ((pos != std::string::npos) && ((pos + 1) < dataString.size()))
+    {
+        // Value is substring after the '='
+        value = dataString.substr(pos + 1);
+    }
+
+    return value;
+}
+
+std::string Journal::getTimeStamp(sd_journal* journal) const
+{
+    // Get realtime (wallclock) timestamp of current journal entry.  The
+    // timestamp is in microseconds since the epoch.
+    uint64_t usec{0};
+    int rc = sd_journal_get_realtime_usec(journal, &usec);
+    if (rc < 0)
+    {
+        throw std::runtime_error{
+            std::string{"Failed to get journal entry timestamp: "} +
+            strerror(-rc)};
+    }
+
+    // Convert to number of seconds since the epoch
+    time_t secs = usec / 1000000;
+
+    // Convert seconds to tm struct required by strftime()
+    struct tm* timeStruct = localtime(&secs);
+    if (timeStruct == nullptr)
+    {
+        throw std::runtime_error{
+            std::string{"Invalid journal entry timestamp: "} + strerror(errno)};
+    }
+
+    // Convert tm struct into a date/time string
+    char timeStamp[80];
+    strftime(timeStamp, sizeof(timeStamp), "%b %d %H:%M:%S", timeStruct);
+
+    return timeStamp;
+}
+
+} // namespace openpower::pels
diff --git a/extensions/openpower-pels/journal.hpp b/extensions/openpower-pels/journal.hpp
new file mode 100644
index 0000000..1261e32
--- /dev/null
+++ b/extensions/openpower-pels/journal.hpp
@@ -0,0 +1,84 @@
+#pragma once
+
+#include <systemd/sd-journal.h>
+
+#include <string>
+#include <vector>
+
+namespace openpower::pels
+{
+
+/**
+ * @class JournalBase
+ * Abstract class to read messages from the journal.
+ */
+class JournalBase
+{
+  public:
+    JournalBase() = default;
+    virtual ~JournalBase() = default;
+    JournalBase(const JournalBase&) = default;
+    JournalBase& operator=(const JournalBase&) = default;
+    JournalBase(JournalBase&&) = default;
+    JournalBase& operator=(JournalBase&&) = default;
+
+    /**
+     * @brief Get messages from the journal
+     *
+     * @param syslogID - The SYSLOG_IDENTIFIER field value
+     * @param maxMessages - Max number of messages to get
+     *
+     * @return The messages
+     */
+    virtual std::vector<std::string> getMessages(const std::string& syslogID,
+                                                 size_t maxMessages) const = 0;
+};
+
+/**
+ * @class Journal
+ *
+ * Reads from the journal.
+ */
+class Journal : public JournalBase
+{
+  public:
+    Journal() = default;
+    ~Journal() = default;
+    Journal(const Journal&) = default;
+    Journal& operator=(const Journal&) = default;
+    Journal(Journal&&) = default;
+    Journal& operator=(Journal&&) = default;
+
+    /**
+     * @brief Get messages from the journal
+     *
+     * @param syslogID - The SYSLOG_IDENTIFIER field value
+     * @param maxMessages - Max number of messages to get
+     *
+     * @return The messages
+     */
+    std::vector<std::string> getMessages(const std::string& syslogID,
+                                         size_t maxMessages) const override;
+
+  private:
+    /**
+     * @brief Gets a field from the current journal entry
+     *
+     * @param journal - pointer to current journal entry
+     * @param field - The field name whose value to get
+     *
+     * @return std::string - The field value
+     */
+    std::string getFieldValue(sd_journal* journal,
+                              const std::string& field) const;
+
+    /**
+     * @brief Gets a readable timestamp from the journal entry
+     *
+     * @param journal - pointer to current journal entry
+     *
+     * @return std::string - A timestamp string
+     */
+    std::string getTimeStamp(sd_journal* journal) const;
+};
+} // namespace openpower::pels
diff --git a/extensions/openpower-pels/manager.hpp b/extensions/openpower-pels/manager.hpp
index 44a9ec2..8f439a5 100644
--- a/extensions/openpower-pels/manager.hpp
+++ b/extensions/openpower-pels/manager.hpp
@@ -5,6 +5,7 @@
 #include "data_interface.hpp"
 #include "event_logger.hpp"
 #include "host_notifier.hpp"
+#include "journal.hpp"
 #include "log_manager.hpp"
 #include "paths.hpp"
 #include "pel.hpp"
@@ -48,13 +49,14 @@
      */
     Manager(phosphor::logging::internal::Manager& logManager,
             std::unique_ptr<DataInterfaceBase> dataIface,
-            EventLogger::LogFunction creatorFunc) :
+            EventLogger::LogFunction creatorFunc,
+            std::unique_ptr<JournalBase> journal) :
         PELInterface(logManager.getBus(), OBJ_LOGGING),
         _logManager(logManager), _eventLogger(std::move(creatorFunc)),
         _repo(getPELRepoPath()),
         _registry(getPELReadOnlyDataPath() / message::registryFileName),
         _event(sdeventplus::Event::get_default()),
-        _dataIface(std::move(dataIface))
+        _dataIface(std::move(dataIface)), _journal(std::move(journal))
     {
         for (const auto& entry : _logManager.entries)
         {
@@ -82,8 +84,10 @@
     Manager(phosphor::logging::internal::Manager& logManager,
             std::unique_ptr<DataInterfaceBase> dataIface,
             EventLogger::LogFunction creatorFunc,
+            std::unique_ptr<JournalBase> journal,
             std::unique_ptr<HostInterface> hostIface) :
-        Manager(logManager, std::move(dataIface), std::move(creatorFunc))
+        Manager(logManager, std::move(dataIface), std::move(creatorFunc),
+                std::move(journal))
     {
         _hostNotifier = std::make_unique<HostNotifier>(
             _repo, *(_dataIface.get()), std::move(hostIface));
@@ -506,6 +510,11 @@
     std::unique_ptr<DataInterfaceBase> _dataIface;
 
     /**
+     * @brief Object used to read from the journal
+     */
+    std::unique_ptr<JournalBase> _journal;
+
+    /**
      * @brief The map used to keep track of PEL entry pointer associated with
      *        event log.
      */
diff --git a/extensions/openpower-pels/meson.build b/extensions/openpower-pels/meson.build
index a29d257..6a07322 100644
--- a/extensions/openpower-pels/meson.build
+++ b/extensions/openpower-pels/meson.build
@@ -62,6 +62,7 @@
     'failing_mtms.cpp',
     'fru_identity.cpp',
     'generic.cpp',
+    'journal.cpp',
     'json_utils.cpp',
     'log_id.cpp',
     'mru.cpp',
diff --git a/test/openpower-pels/mocks.hpp b/test/openpower-pels/mocks.hpp
index de7f757..7a1ee35 100644
--- a/test/openpower-pels/mocks.hpp
+++ b/test/openpower-pels/mocks.hpp
@@ -1,5 +1,6 @@
 #include "extensions/openpower-pels/data_interface.hpp"
 #include "extensions/openpower-pels/host_interface.hpp"
+#include "extensions/openpower-pels/journal.hpp"
 
 #include <fcntl.h>
 
@@ -278,5 +279,14 @@
     size_t _cmdsProcessed = 0;
 };
 
+class MockJournal : public JournalBase
+{
+  public:
+    MockJournal() {}
+
+    MOCK_METHOD(std::vector<std::string>, getMessages,
+                (const std::string&, size_t), (const override));
+};
+
 } // namespace pels
 } // namespace openpower
diff --git a/test/openpower-pels/pel_manager_test.cpp b/test/openpower-pels/pel_manager_test.cpp
index b27ca60..012e42c 100644
--- a/test/openpower-pels/pel_manager_test.cpp
+++ b/test/openpower-pels/pel_manager_test.cpp
@@ -131,10 +131,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     // Create a PEL, write it to a file, and pass that filename into
     // the create function.
@@ -173,10 +176,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     // Create a PEL, write it to a file, and pass that filename into
     // the create function.
@@ -282,10 +288,13 @@
     EXPECT_CALL(*mockIface, checkDumpStatus(dumpType))
         .WillRepeatedly(Return(std::vector<bool>{false, false, false}));
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     std::vector<std::string> additionalData{"FOO=BAR"};
     std::vector<std::string> associations;
@@ -387,10 +396,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     Manager manager{logManager, std::move(dataIface),
                     std::bind(std::mem_fn(&TestLogger::log), &logger,
                               std::placeholders::_1, std::placeholders::_2,
-                              std::placeholders::_3)};
+                              std::placeholders::_3),
+                    std::move(journal)};
 
     // Create a PEL, write it to a file, and pass that filename into
     // the create function so there's one in the repo.
@@ -610,10 +622,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     {
         std::string adItem = "ESEL=" + esel;
@@ -664,10 +679,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     // Create 25 1000B (4096B on disk each, which is what is used for pruning)
     // BMC non-informational PELs in the 100KB repository.  After the 24th one,
@@ -738,10 +756,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     auto data = pelDataFactory(TestPELType::pelSimple);
     auto dir = makeTempDir();
@@ -814,10 +835,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     auto data = pelDataFactory(TestPELType::pelSimple);
     auto dir = makeTempDir();
@@ -890,10 +914,13 @@
     EXPECT_CALL(*mockIface, checkDumpStatus(dumpType))
         .WillRepeatedly(Return(std::vector<bool>{false, false, false}));
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     // Add a PEL with a callout as if hostboot added it
     {
@@ -999,10 +1026,13 @@
     std::unique_ptr<DataInterfaceBase> dataIface =
         std::make_unique<MockDataInterface>();
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     for (int i = 0; i < 2; i++)
     {
@@ -1084,10 +1114,13 @@
     EXPECT_CALL(*mockIface, checkDumpStatus(dumpType))
         .WillRepeatedly(Return(std::vector<bool>{false, false, false}));
 
+    std::unique_ptr<JournalBase> journal = std::make_unique<MockJournal>();
+
     openpower::pels::Manager manager{
         logManager, std::move(dataIface),
         std::bind(std::mem_fn(&TestLogger::log), &logger, std::placeholders::_1,
-                  std::placeholders::_2, std::placeholders::_3)};
+                  std::placeholders::_2, std::placeholders::_3),
+        std::move(journal)};
 
     std::vector<std::string> additionalData{"FOO=BAR"};
     std::vector<std::string> associations;