xref: /openbmc/phosphor-logging/extensions/openpower-pels/journal.hpp (revision 25291157927273a4ac459c6479c1d3090ddc6a16)
1d96fa60dSMatt Spinler #pragma once
2d96fa60dSMatt Spinler 
3d96fa60dSMatt Spinler #include <systemd/sd-journal.h>
4d96fa60dSMatt Spinler 
5d96fa60dSMatt Spinler #include <string>
6d96fa60dSMatt Spinler #include <vector>
7d96fa60dSMatt Spinler 
8d96fa60dSMatt Spinler namespace openpower::pels
9d96fa60dSMatt Spinler {
10d96fa60dSMatt Spinler 
11d96fa60dSMatt Spinler /**
12d96fa60dSMatt Spinler  * @class JournalBase
13d96fa60dSMatt Spinler  * Abstract class to read messages from the journal.
14d96fa60dSMatt Spinler  */
15d96fa60dSMatt Spinler class JournalBase
16d96fa60dSMatt Spinler {
17d96fa60dSMatt Spinler   public:
18d96fa60dSMatt Spinler     JournalBase() = default;
19d96fa60dSMatt Spinler     virtual ~JournalBase() = default;
20d96fa60dSMatt Spinler     JournalBase(const JournalBase&) = default;
21d96fa60dSMatt Spinler     JournalBase& operator=(const JournalBase&) = default;
22d96fa60dSMatt Spinler     JournalBase(JournalBase&&) = default;
23d96fa60dSMatt Spinler     JournalBase& operator=(JournalBase&&) = default;
24d96fa60dSMatt Spinler 
25d96fa60dSMatt Spinler     /**
26d96fa60dSMatt Spinler      * @brief Get messages from the journal
27d96fa60dSMatt Spinler      *
28d96fa60dSMatt Spinler      * @param syslogID - The SYSLOG_IDENTIFIER field value
29d96fa60dSMatt Spinler      * @param maxMessages - Max number of messages to get
30d96fa60dSMatt Spinler      *
31d96fa60dSMatt Spinler      * @return The messages
32d96fa60dSMatt Spinler      */
33*25291157SPatrick Williams     virtual std::vector<std::string> getMessages(const std::string& syslogID,
34*25291157SPatrick Williams                                                  size_t maxMessages) const = 0;
359d921096SMatt Spinler 
369d921096SMatt Spinler     /**
379d921096SMatt Spinler      * @brief Call journalctl --sync to write unwritten journal data to disk
389d921096SMatt Spinler      */
399d921096SMatt Spinler     virtual void sync() const = 0;
40d96fa60dSMatt Spinler };
41d96fa60dSMatt Spinler 
42d96fa60dSMatt Spinler /**
43d96fa60dSMatt Spinler  * @class Journal
44d96fa60dSMatt Spinler  *
45d96fa60dSMatt Spinler  * Reads from the journal.
46d96fa60dSMatt Spinler  */
47d96fa60dSMatt Spinler class Journal : public JournalBase
48d96fa60dSMatt Spinler {
49d96fa60dSMatt Spinler   public:
50d96fa60dSMatt Spinler     Journal() = default;
51d96fa60dSMatt Spinler     ~Journal() = default;
52d96fa60dSMatt Spinler     Journal(const Journal&) = default;
53d96fa60dSMatt Spinler     Journal& operator=(const Journal&) = default;
54d96fa60dSMatt Spinler     Journal(Journal&&) = default;
55d96fa60dSMatt Spinler     Journal& operator=(Journal&&) = default;
56d96fa60dSMatt Spinler 
57d96fa60dSMatt Spinler     /**
58d96fa60dSMatt Spinler      * @brief Get messages from the journal
59d96fa60dSMatt Spinler      *
60d96fa60dSMatt Spinler      * @param syslogID - The SYSLOG_IDENTIFIER field value
61d96fa60dSMatt Spinler      * @param maxMessages - Max number of messages to get
62d96fa60dSMatt Spinler      *
63d96fa60dSMatt Spinler      * @return The messages
64d96fa60dSMatt Spinler      */
65d96fa60dSMatt Spinler     std::vector<std::string> getMessages(const std::string& syslogID,
66d96fa60dSMatt Spinler                                          size_t maxMessages) const override;
67d96fa60dSMatt Spinler 
689d921096SMatt Spinler     /**
699d921096SMatt Spinler      * @brief Call journalctl --sync to write unwritten journal data to disk
709d921096SMatt Spinler      */
719d921096SMatt Spinler     void sync() const override;
729d921096SMatt Spinler 
73d96fa60dSMatt Spinler   private:
74d96fa60dSMatt Spinler     /**
75d96fa60dSMatt Spinler      * @brief Gets a field from the current journal entry
76d96fa60dSMatt Spinler      *
77d96fa60dSMatt Spinler      * @param journal - pointer to current journal entry
78d96fa60dSMatt Spinler      * @param field - The field name whose value to get
79d96fa60dSMatt Spinler      *
80d96fa60dSMatt Spinler      * @return std::string - The field value
81d96fa60dSMatt Spinler      */
82d96fa60dSMatt Spinler     std::string getFieldValue(sd_journal* journal,
83d96fa60dSMatt Spinler                               const std::string& field) const;
84d96fa60dSMatt Spinler 
85d96fa60dSMatt Spinler     /**
86d96fa60dSMatt Spinler      * @brief Gets a readable timestamp from the journal entry
87d96fa60dSMatt Spinler      *
88d96fa60dSMatt Spinler      * @param journal - pointer to current journal entry
89d96fa60dSMatt Spinler      *
90d96fa60dSMatt Spinler      * @return std::string - A timestamp string
91d96fa60dSMatt Spinler      */
92d96fa60dSMatt Spinler     std::string getTimeStamp(sd_journal* journal) const;
93d96fa60dSMatt Spinler };
94d96fa60dSMatt Spinler } // namespace openpower::pels
95