blob: 300ef6fed516425057c4d246a10fe1490fe6763b [file] [log] [blame]
Shawn McCarney7c5d7b22020-04-03 18:50:13 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
Bob Kingca08a792020-09-30 14:30:13 +080018#include <systemd/sd-journal.h>
19
Anwaar Hadi72e584c2025-05-20 22:02:14 +000020#include <phosphor-logging/lg2.hpp>
Shawn McCarney5248b8a2020-07-15 22:59:36 -050021
Shawn McCarney7c5d7b22020-04-03 18:50:13 -050022#include <string>
Shawn McCarney5248b8a2020-07-15 22:59:36 -050023#include <vector>
24
25namespace phosphor::power::regulators
26{
27
28/**
29 * @class Journal
30 *
31 * Abstract base class that provides a journal interface.
32 *
33 * The interface is used to write messages/log entries to the system journal.
34 */
35class Journal
36{
37 public:
38 // Specify which compiler-generated methods we want
39 Journal() = default;
40 Journal(const Journal&) = delete;
41 Journal(Journal&&) = delete;
42 Journal& operator=(const Journal&) = delete;
43 Journal& operator=(Journal&&) = delete;
44 virtual ~Journal() = default;
45
46 /**
Shawn McCarneya528a282021-02-15 18:07:07 -060047 * Gets the journal messages that have the specified field set to the
48 * specified value.
49 *
50 * The messages in the returned vector are ordered from oldest to newest.
51 *
52 * @param field journal field name
53 * @param fieldValue expected field value
54 * @param max Maximum number of messages to return. Specify 0 to return all
55 * matching messages.
56 * @return matching messages from the journal
57 */
Patrick Williams92261f82025-02-01 08:22:34 -050058 virtual std::vector<std::string> getMessages(const std::string& field,
59 const std::string& fieldValue,
60 unsigned int max = 0) = 0;
Shawn McCarneya528a282021-02-15 18:07:07 -060061
62 /**
Shawn McCarney5248b8a2020-07-15 22:59:36 -050063 * Logs a debug message in the system journal.
64 *
65 * @param message message to log
66 */
67 virtual void logDebug(const std::string& message) = 0;
68
69 /**
70 * Logs debug messages in the system journal.
71 *
72 * @param messages messages to log
73 */
74 virtual void logDebug(const std::vector<std::string>& messages) = 0;
75
76 /**
77 * Logs an error message in the system journal.
78 *
79 * @param message message to log
80 */
81 virtual void logError(const std::string& message) = 0;
82
83 /**
84 * Logs error messages in the system journal.
85 *
86 * @param messages messages to log
87 */
88 virtual void logError(const std::vector<std::string>& messages) = 0;
89
90 /**
91 * Logs an informational message in the system journal.
92 *
93 * @param message message to log
94 */
95 virtual void logInfo(const std::string& message) = 0;
96
97 /**
98 * Logs informational messages in the system journal.
99 *
100 * @param messages messages to log
101 */
102 virtual void logInfo(const std::vector<std::string>& messages) = 0;
103};
104
105/**
106 * @class SystemdJournal
107 *
108 * Implementation of the Journal interface that writes to the systemd journal.
109 */
110class SystemdJournal : public Journal
111{
112 public:
113 // Specify which compiler-generated methods we want
114 SystemdJournal() = default;
115 SystemdJournal(const SystemdJournal&) = delete;
116 SystemdJournal(SystemdJournal&&) = delete;
117 SystemdJournal& operator=(const SystemdJournal&) = delete;
118 SystemdJournal& operator=(SystemdJournal&&) = delete;
119 virtual ~SystemdJournal() = default;
120
Bob Kingca08a792020-09-30 14:30:13 +0800121 /** @copydoc Journal::getMessages() */
Patrick Williams92261f82025-02-01 08:22:34 -0500122 virtual std::vector<std::string> getMessages(const std::string& field,
123 const std::string& fieldValue,
124 unsigned int max) override;
Bob Kingca08a792020-09-30 14:30:13 +0800125
Shawn McCarney5248b8a2020-07-15 22:59:36 -0500126 /** @copydoc Journal::logDebug(const std::string&) */
127 virtual void logDebug(const std::string& message) override
128 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000129 lg2::debug(message.c_str());
Shawn McCarney5248b8a2020-07-15 22:59:36 -0500130 }
131
132 /** @copydoc Journal::logDebug(const std::vector<std::string>&) */
133 virtual void logDebug(const std::vector<std::string>& messages) override
134 {
135 for (const std::string& message : messages)
136 {
137 logDebug(message);
138 }
139 }
140
141 /** @copydoc Journal::logError(const std::string&) */
142 virtual void logError(const std::string& message) override
143 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000144 lg2::error(message.c_str());
Shawn McCarney5248b8a2020-07-15 22:59:36 -0500145 }
146
147 /** @copydoc Journal::logError(const std::vector<std::string>&) */
148 virtual void logError(const std::vector<std::string>& messages) override
149 {
150 for (const std::string& message : messages)
151 {
152 logError(message);
153 }
154 }
155
156 /** @copydoc Journal::logInfo(const std::string&) */
157 virtual void logInfo(const std::string& message) override
158 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000159 lg2::info(message.c_str());
Shawn McCarney5248b8a2020-07-15 22:59:36 -0500160 }
161
162 /** @copydoc Journal::logInfo(const std::vector<std::string>&) */
163 virtual void logInfo(const std::vector<std::string>& messages) override
164 {
165 for (const std::string& message : messages)
166 {
167 logInfo(message);
168 }
169 }
Bob Kingca08a792020-09-30 14:30:13 +0800170
171 private:
172 /**
Shawn McCarneya528a282021-02-15 18:07:07 -0600173 * Gets the value of the specified field for the current journal entry.
Bob Kingca08a792020-09-30 14:30:13 +0800174 *
Shawn McCarneya528a282021-02-15 18:07:07 -0600175 * Returns an empty string if the current journal entry does not have the
176 * specified field.
177 *
178 * @param journal current journal entry
179 * @param field journal field name
180 * @return field value
Bob Kingca08a792020-09-30 14:30:13 +0800181 */
Shawn McCarneya528a282021-02-15 18:07:07 -0600182 std::string getFieldValue(sd_journal* journal, const std::string& field);
183
184 /**
185 * Gets the realtime (wallclock) timestamp for the current journal entry.
186 *
187 * @param journal current journal entry
188 * @return timestamp as a date/time string
189 */
190 std::string getTimeStamp(sd_journal* journal);
Shawn McCarney5248b8a2020-07-15 22:59:36 -0500191};
192
193} // namespace phosphor::power::regulators