blob: 1261e32cd28306711304943e81322ee6e88b01e1 [file] [log] [blame]
Matt Spinlerd96fa602022-12-15 11:11:26 -06001#pragma once
2
3#include <systemd/sd-journal.h>
4
5#include <string>
6#include <vector>
7
8namespace openpower::pels
9{
10
11/**
12 * @class JournalBase
13 * Abstract class to read messages from the journal.
14 */
15class 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 */
33 virtual std::vector<std::string> getMessages(const std::string& syslogID,
34 size_t maxMessages) const = 0;
35};
36
37/**
38 * @class Journal
39 *
40 * Reads from the journal.
41 */
42class Journal : public JournalBase
43{
44 public:
45 Journal() = default;
46 ~Journal() = default;
47 Journal(const Journal&) = default;
48 Journal& operator=(const Journal&) = default;
49 Journal(Journal&&) = default;
50 Journal& operator=(Journal&&) = default;
51
52 /**
53 * @brief Get messages from the journal
54 *
55 * @param syslogID - The SYSLOG_IDENTIFIER field value
56 * @param maxMessages - Max number of messages to get
57 *
58 * @return The messages
59 */
60 std::vector<std::string> getMessages(const std::string& syslogID,
61 size_t maxMessages) const override;
62
63 private:
64 /**
65 * @brief Gets a field from the current journal entry
66 *
67 * @param journal - pointer to current journal entry
68 * @param field - The field name whose value to get
69 *
70 * @return std::string - The field value
71 */
72 std::string getFieldValue(sd_journal* journal,
73 const std::string& field) const;
74
75 /**
76 * @brief Gets a readable timestamp from the journal entry
77 *
78 * @param journal - pointer to current journal entry
79 *
80 * @return std::string - A timestamp string
81 */
82 std::string getTimeStamp(sd_journal* journal) const;
83};
84} // namespace openpower::pels