regulators: Enhance SystemdJournal::getMessages()
Enhance the SystemdJournal::getMessages() method that obtains systemd
journal entries with a specified field value:
* Handle journal entries that do not have expected fields. All journal
fields are optional, so if a field is missing this is not an error
condition.
* Use built-in filtering support provided by the systemd journal API.
This will likely provide better performance.
* Use C++ std::string methods to perform field data parsing.
* Add short sleep before acquiring journal messages. Otherwise recently
added messages are sometimes not available.
Tested:
* Tested where journal entry is missing expected fields.
* Tested that built-in filtering support results in the desired set of
journal entries.
* Tested field data parsing. Tested where value has expected format and
when it does not.
* Verified all journal entries, including the most recently added ones,
are obtained.
* Tested error handling when each sd_journal_*() function fails.
* For complete test plan, see
https://gist.github.com/smccarney/bd4e546a76ee05e4df939eb69e23daee
Signed-off-by: Shawn McCarney <shawnmm@us.ibm.com>
Change-Id: I066492edc05691b240d768c5b99cfa6f07d83ddf
diff --git a/phosphor-regulators/src/journal.hpp b/phosphor-regulators/src/journal.hpp
index 4499aa6..2199c8c 100644
--- a/phosphor-regulators/src/journal.hpp
+++ b/phosphor-regulators/src/journal.hpp
@@ -44,6 +44,22 @@
virtual ~Journal() = default;
/**
+ * Gets the journal messages that have the specified field set to the
+ * specified value.
+ *
+ * The messages in the returned vector are ordered from oldest to newest.
+ *
+ * @param field journal field name
+ * @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;
+
+ /**
* Logs a debug message in the system journal.
*
* @param message message to log
@@ -84,21 +100,6 @@
* @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;
};
/**
@@ -172,13 +173,24 @@
private:
/**
- * Gets the data object associated with a specific field from the
- * current journal entry and return the data as string.
+ * Gets the value of the specified field for the current journal entry.
*
- * @param journal the current journal entry
- * @param field journal field to use during search
+ * Returns an empty string if the current journal entry does not have the
+ * specified field.
+ *
+ * @param journal current journal entry
+ * @param field journal field name
+ * @return field value
*/
- std::string getFieldValue(sd_journal* journal, const char* field) const;
+ std::string getFieldValue(sd_journal* journal, const std::string& field);
+
+ /**
+ * Gets the realtime (wallclock) timestamp for the current journal entry.
+ *
+ * @param journal current journal entry
+ * @return timestamp as a date/time string
+ */
+ std::string getTimeStamp(sd_journal* journal);
};
} // namespace phosphor::power::regulators