Matt Spinler | d96fa60 | 2022-12-15 11:11:26 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <systemd/sd-journal.h> |
| 4 | |
| 5 | #include <string> |
| 6 | #include <vector> |
| 7 | |
| 8 | namespace openpower::pels |
| 9 | { |
| 10 | |
| 11 | /** |
| 12 | * @class JournalBase |
| 13 | * Abstract class to read messages from the journal. |
| 14 | */ |
| 15 | class JournalBase |
| 16 | { |
| 17 | public: |
| 18 | JournalBase() = default; |
| 19 | virtual ~JournalBase() = default; |
| 20 | JournalBase(const JournalBase&) = default; |
| 21 | JournalBase& operator=(const JournalBase&) = default; |
| 22 | JournalBase(JournalBase&&) = default; |
| 23 | JournalBase& operator=(JournalBase&&) = default; |
| 24 | |
| 25 | /** |
| 26 | * @brief Get messages from the journal |
| 27 | * |
| 28 | * @param syslogID - The SYSLOG_IDENTIFIER field value |
| 29 | * @param maxMessages - Max number of messages to get |
| 30 | * |
| 31 | * @return The messages |
| 32 | */ |
Patrick Williams | 075c792 | 2024-08-16 15:19:49 -0400 | [diff] [blame] | 33 | virtual std::vector<std::string> |
| 34 | getMessages(const std::string& syslogID, size_t maxMessages) const = 0; |
Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @brief Call journalctl --sync to write unwritten journal data to disk |
| 38 | */ |
| 39 | virtual void sync() const = 0; |
Matt Spinler | d96fa60 | 2022-12-15 11:11:26 -0600 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * @class Journal |
| 44 | * |
| 45 | * Reads from the journal. |
| 46 | */ |
| 47 | class Journal : public JournalBase |
| 48 | { |
| 49 | public: |
| 50 | Journal() = default; |
| 51 | ~Journal() = default; |
| 52 | Journal(const Journal&) = default; |
| 53 | Journal& operator=(const Journal&) = default; |
| 54 | Journal(Journal&&) = default; |
| 55 | Journal& operator=(Journal&&) = default; |
| 56 | |
| 57 | /** |
| 58 | * @brief Get messages from the journal |
| 59 | * |
| 60 | * @param syslogID - The SYSLOG_IDENTIFIER field value |
| 61 | * @param maxMessages - Max number of messages to get |
| 62 | * |
| 63 | * @return The messages |
| 64 | */ |
| 65 | std::vector<std::string> getMessages(const std::string& syslogID, |
| 66 | size_t maxMessages) const override; |
| 67 | |
Matt Spinler | 9d92109 | 2022-12-15 11:54:49 -0600 | [diff] [blame] | 68 | /** |
| 69 | * @brief Call journalctl --sync to write unwritten journal data to disk |
| 70 | */ |
| 71 | void sync() const override; |
| 72 | |
Matt Spinler | d96fa60 | 2022-12-15 11:11:26 -0600 | [diff] [blame] | 73 | private: |
| 74 | /** |
| 75 | * @brief Gets a field from the current journal entry |
| 76 | * |
| 77 | * @param journal - pointer to current journal entry |
| 78 | * @param field - The field name whose value to get |
| 79 | * |
| 80 | * @return std::string - The field value |
| 81 | */ |
| 82 | std::string getFieldValue(sd_journal* journal, |
| 83 | const std::string& field) const; |
| 84 | |
| 85 | /** |
| 86 | * @brief Gets a readable timestamp from the journal entry |
| 87 | * |
| 88 | * @param journal - pointer to current journal entry |
| 89 | * |
| 90 | * @return std::string - A timestamp string |
| 91 | */ |
| 92 | std::string getTimeStamp(sd_journal* journal) const; |
| 93 | }; |
| 94 | } // namespace openpower::pels |