blob: e6abfbebc7f6a12c9caeeffe595b552f26479e3e [file] [log] [blame]
Shawn McCarney76c14c32020-07-15 10:10:20 -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
Shawn McCarney1df59542020-09-24 18:43:18 -050018#include "ffdc_file.hpp"
19#include "journal.hpp"
Shawn McCarney5dab5d32021-08-28 11:02:14 -050020#include "phase_fault.hpp"
Shawn McCarney1df59542020-09-24 18:43:18 -050021#include "xyz/openbmc_project/Logging/Create/server.hpp"
22#include "xyz/openbmc_project/Logging/Entry/server.hpp"
23
Shawn McCarney76c14c32020-07-15 10:10:20 -050024#include <sdbusplus/bus.hpp>
25
Shawn McCarney1df59542020-09-24 18:43:18 -050026#include <cstdint>
27#include <map>
28#include <string>
29#include <tuple>
30#include <vector>
31
Shawn McCarney76c14c32020-07-15 10:10:20 -050032namespace phosphor::power::regulators
33{
34
Shawn McCarney1df59542020-09-24 18:43:18 -050035using namespace sdbusplus::xyz::openbmc_project::Logging::server;
36using FFDCTuple =
37 std::tuple<FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
38
Shawn McCarney76c14c32020-07-15 10:10:20 -050039/**
40 * @class ErrorLogging
41 *
42 * Abstract base class that provides an error logging interface.
43 *
44 * The interface is used to create error logs.
45 */
46class ErrorLogging
47{
48 public:
49 // Specify which compiler-generated methods we want
50 ErrorLogging() = default;
51 ErrorLogging(const ErrorLogging&) = delete;
52 ErrorLogging(ErrorLogging&&) = delete;
53 ErrorLogging& operator=(const ErrorLogging&) = delete;
54 ErrorLogging& operator=(ErrorLogging&&) = delete;
55 virtual ~ErrorLogging() = default;
56
Shawn McCarney1df59542020-09-24 18:43:18 -050057 /**
58 * Log a regulators configuration file error.
59 *
60 * This error is logged when the regulators configuration file could not be
61 * found, could not be read, or had invalid contents.
62 *
63 * @param severity severity level
64 * @param journal system journal
65 */
66 virtual void logConfigFileError(Entry::Level severity,
67 Journal& journal) = 0;
68
69 /**
70 * Log a D-Bus error.
71 *
72 * This error is logged when D-Bus communication fails.
73 *
74 * @param severity severity level
75 * @param journal system journal
76 */
77 virtual void logDBusError(Entry::Level severity, Journal& journal) = 0;
78
79 /**
80 * Log an I2C communication error.
81 *
82 * @param severity severity level
83 * @param journal system journal
84 * @param bus I2C bus in the form "/dev/i2c-X", where X is the 0-based bus
85 * number
86 * @param addr 7 bit I2C address
87 * @param errorNumber errno value from the failed I2C operation
88 */
89 virtual void logI2CError(Entry::Level severity, Journal& journal,
90 const std::string& bus, uint8_t addr,
91 int errorNumber) = 0;
92
93 /**
94 * Log an internal firmware error.
95 *
96 * @param severity severity level
97 * @param journal system journal
98 */
99 virtual void logInternalError(Entry::Level severity, Journal& journal) = 0;
100
101 /**
Shawn McCarney5dab5d32021-08-28 11:02:14 -0500102 * Log a phase fault error.
103 *
104 * This error is logged when a regulator has lost a redundant phase.
105 *
106 * @param severity severity level
107 * @param journal system journal
108 * @param type phase fault type
109 * @param inventoryPath D-Bus inventory path of the device where the error
110 * occurred
111 * @param additionalData additional error data (if any)
112 */
113 virtual void
114 logPhaseFault(Entry::Level severity, Journal& journal,
115 PhaseFaultType type, const std::string& inventoryPath,
116 std::map<std::string, std::string> additionalData) = 0;
117
118 /**
Shawn McCarney1df59542020-09-24 18:43:18 -0500119 * Log a PMBus error.
120 *
121 * This error is logged when the I2C communication was successful, but the
122 * PMBus value read is invalid or unsupported.
123 *
124 * @param severity severity level
125 * @param journal system journal
126 * @param inventoryPath D-Bus inventory path of the device where the error
127 * occurred
128 */
129 virtual void logPMBusError(Entry::Level severity, Journal& journal,
130 const std::string& inventoryPath) = 0;
131
132 /**
133 * Log a write verification error.
134 *
135 * This error is logged when a device register is written, read back, and
136 * the two values do not match. This is also called a read-back error.
137 *
138 * @param severity severity level
139 * @param journal system journal
140 * @param inventoryPath D-Bus inventory path of the device where the error
141 * occurred
142 */
143 virtual void
144 logWriteVerificationError(Entry::Level severity, Journal& journal,
145 const std::string& inventoryPath) = 0;
Shawn McCarney76c14c32020-07-15 10:10:20 -0500146};
147
148/**
149 * @class DBusErrorLogging
150 *
151 * Implementation of the ErrorLogging interface using D-Bus method calls.
152 */
153class DBusErrorLogging : public ErrorLogging
154{
155 public:
156 // Specify which compiler-generated methods we want
157 DBusErrorLogging() = delete;
158 DBusErrorLogging(const DBusErrorLogging&) = delete;
159 DBusErrorLogging(DBusErrorLogging&&) = delete;
160 DBusErrorLogging& operator=(const DBusErrorLogging&) = delete;
161 DBusErrorLogging& operator=(DBusErrorLogging&&) = delete;
162 virtual ~DBusErrorLogging() = default;
163
164 /**
165 * Constructor.
166 *
167 * @param bus D-Bus bus object
168 */
169 explicit DBusErrorLogging(sdbusplus::bus::bus& bus) : bus{bus}
170 {
171 }
172
Shawn McCarney1df59542020-09-24 18:43:18 -0500173 /** @copydoc ErrorLogging::logConfigFileError() */
174 virtual void logConfigFileError(Entry::Level severity,
175 Journal& journal) override;
176
177 /** @copydoc ErrorLogging::logDBusError() */
178 virtual void logDBusError(Entry::Level severity, Journal& journal) override;
179
180 /** @copydoc ErrorLogging::logI2CError() */
181 virtual void logI2CError(Entry::Level severity, Journal& journal,
182 const std::string& bus, uint8_t addr,
183 int errorNumber) override;
184
185 /** @copydoc ErrorLogging::logInternalError() */
186 virtual void logInternalError(Entry::Level severity,
187 Journal& journal) override;
188
Shawn McCarney5dab5d32021-08-28 11:02:14 -0500189 /** @copydoc ErrorLogging::logPhaseFault() */
190 virtual void logPhaseFault(
191 Entry::Level severity, Journal& journal, PhaseFaultType type,
192 const std::string& inventoryPath,
193 std::map<std::string, std::string> additionalData) override;
194
Shawn McCarney1df59542020-09-24 18:43:18 -0500195 /** @copydoc ErrorLogging::logPMBusError() */
196 virtual void logPMBusError(Entry::Level severity, Journal& journal,
197 const std::string& inventoryPath) override;
198
199 /** @copydoc ErrorLogging::logWriteVerificationError() */
200 virtual void
201 logWriteVerificationError(Entry::Level severity, Journal& journal,
202 const std::string& inventoryPath) override;
Shawn McCarney76c14c32020-07-15 10:10:20 -0500203
204 private:
205 /**
Shawn McCarney1df59542020-09-24 18:43:18 -0500206 * Create an FFDCFile object containing the specified lines of text data.
207 *
208 * Throws an exception if an error occurs.
209 *
210 * @param lines lines of text data to write to file
211 * @return FFDCFile object
212 */
213 FFDCFile createFFDCFile(const std::vector<std::string>& lines);
214
215 /**
216 * Create FFDCFile objects containing debug data to store in the error log.
217 *
218 * If an error occurs, the error is written to the journal but an exception
219 * is not thrown.
220 *
221 * @param journal system journal
222 * @return vector of FFDCFile objects
223 */
224 std::vector<FFDCFile> createFFDCFiles(Journal& journal);
225
226 /**
227 * Create FFDCTuple objects corresponding to the specified FFDC files.
228 *
229 * The D-Bus method to create an error log requires a vector of tuples to
230 * pass in the FFDC file information.
231 *
232 * @param files FFDC files
233 * @return vector of FFDCTuple objects
234 */
235 std::vector<FFDCTuple> createFFDCTuples(std::vector<FFDCFile>& files);
236
237 /**
Shawn McCarney1df59542020-09-24 18:43:18 -0500238 * Logs an error using the D-Bus CreateWithFFDCFiles method.
239 *
240 * If logging fails, a message is written to the journal but an exception is
241 * not thrown.
242 *
243 * @param message Message property of the error log entry
244 * @param severity Severity property of the error log entry
245 * @param additionalData AdditionalData property of the error log entry
246 * @param journal system journal
247 */
248 void logError(const std::string& message, Entry::Level severity,
249 std::map<std::string, std::string>& additionalData,
250 Journal& journal);
251
252 /**
253 * Removes the specified FFDC files from the file system.
254 *
255 * Also clears the specified vector, removing the FFDCFile objects.
256 *
257 * If an error occurs, the error is written to the journal but an exception
258 * is not thrown.
259 *
260 * @param files FFDC files to remove
261 * @param journal system journal
262 */
263 void removeFFDCFiles(std::vector<FFDCFile>& files, Journal& journal);
264
265 /**
Shawn McCarney76c14c32020-07-15 10:10:20 -0500266 * D-Bus bus object.
267 */
268 sdbusplus::bus::bus& bus;
269};
270
271} // namespace phosphor::power::regulators