regulators: Add getMessages method
Add getMessages method. It gets the journal messages that have the
specified field set to the specified value.
Tested:
* Get field "_PID" with value "1".
* Get field "_SYSTEMD_UNIT" with value "init.scope".
* Get a field that does not exist and return vector size is 0.
* max parameter is 0 and return all messages.
* max parameter is less than number of messages in journal
and return max number of messages.
* max parameter is more than number of messages in journal
and return all messages
Signed-off-by: Bob King <Bob_King@wistron.com>
Change-Id: I6a9410b5509b3044c1de103e71276e8e7c241fa6
diff --git a/phosphor-regulators/src/journal.hpp b/phosphor-regulators/src/journal.hpp
index 47dc347..4499aa6 100644
--- a/phosphor-regulators/src/journal.hpp
+++ b/phosphor-regulators/src/journal.hpp
@@ -15,6 +15,8 @@
*/
#pragma once
+#include <systemd/sd-journal.h>
+
#include <phosphor-logging/log.hpp>
#include <string>
@@ -82,6 +84,21 @@
* @param messages messages to log
*/
virtual void logInfo(const std::vector<std::string>& messages) = 0;
+
+ /**
+ * Gets the journal messages that have the specified field set to the
+ * specified value.
+ *
+ * @param field journal field to use during search
+ * @param fieldValue expected field value
+ * @param max Maximum number of messages to return.
+ * Specify 0 to return all matching messages.
+ *
+ * @return matching messages from the journal
+ */
+ virtual std::vector<std::string> getMessages(const std::string& field,
+ const std::string& fieldValue,
+ unsigned int max = 0) = 0;
};
/**
@@ -100,6 +117,11 @@
SystemdJournal& operator=(SystemdJournal&&) = delete;
virtual ~SystemdJournal() = default;
+ /** @copydoc Journal::getMessages() */
+ virtual std::vector<std::string> getMessages(const std::string& field,
+ const std::string& fieldValue,
+ unsigned int max) override;
+
/** @copydoc Journal::logDebug(const std::string&) */
virtual void logDebug(const std::string& message) override
{
@@ -147,6 +169,16 @@
logInfo(message);
}
}
+
+ private:
+ /**
+ * Gets the data object associated with a specific field from the
+ * current journal entry and return the data as string.
+ *
+ * @param journal the current journal entry
+ * @param field journal field to use during search
+ */
+ std::string getFieldValue(sd_journal* journal, const char* field) const;
};
} // namespace phosphor::power::regulators