| Shawn McCarney | 7c5d7b2 | 2020-04-03 18:50:13 -0500 | [diff] [blame] | 1 | /** | 
|  | 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 King | ca08a79 | 2020-09-30 14:30:13 +0800 | [diff] [blame] | 18 | #include <systemd/sd-journal.h> | 
|  | 19 |  | 
| Shawn McCarney | 5248b8a | 2020-07-15 22:59:36 -0500 | [diff] [blame] | 20 | #include <phosphor-logging/log.hpp> | 
|  | 21 |  | 
| Shawn McCarney | 7c5d7b2 | 2020-04-03 18:50:13 -0500 | [diff] [blame] | 22 | #include <string> | 
| Shawn McCarney | 5248b8a | 2020-07-15 22:59:36 -0500 | [diff] [blame] | 23 | #include <vector> | 
|  | 24 |  | 
|  | 25 | namespace 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 | */ | 
|  | 35 | class 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 | /** | 
|  | 47 | * Logs a debug message in the system journal. | 
|  | 48 | * | 
|  | 49 | * @param message message to log | 
|  | 50 | */ | 
|  | 51 | virtual void logDebug(const std::string& message) = 0; | 
|  | 52 |  | 
|  | 53 | /** | 
|  | 54 | * Logs debug messages in the system journal. | 
|  | 55 | * | 
|  | 56 | * @param messages messages to log | 
|  | 57 | */ | 
|  | 58 | virtual void logDebug(const std::vector<std::string>& messages) = 0; | 
|  | 59 |  | 
|  | 60 | /** | 
|  | 61 | * Logs an error message in the system journal. | 
|  | 62 | * | 
|  | 63 | * @param message message to log | 
|  | 64 | */ | 
|  | 65 | virtual void logError(const std::string& message) = 0; | 
|  | 66 |  | 
|  | 67 | /** | 
|  | 68 | * Logs error messages in the system journal. | 
|  | 69 | * | 
|  | 70 | * @param messages messages to log | 
|  | 71 | */ | 
|  | 72 | virtual void logError(const std::vector<std::string>& messages) = 0; | 
|  | 73 |  | 
|  | 74 | /** | 
|  | 75 | * Logs an informational message in the system journal. | 
|  | 76 | * | 
|  | 77 | * @param message message to log | 
|  | 78 | */ | 
|  | 79 | virtual void logInfo(const std::string& message) = 0; | 
|  | 80 |  | 
|  | 81 | /** | 
|  | 82 | * Logs informational messages in the system journal. | 
|  | 83 | * | 
|  | 84 | * @param messages messages to log | 
|  | 85 | */ | 
|  | 86 | virtual void logInfo(const std::vector<std::string>& messages) = 0; | 
| Bob King | ca08a79 | 2020-09-30 14:30:13 +0800 | [diff] [blame] | 87 |  | 
|  | 88 | /** | 
|  | 89 | * Gets the journal messages that have the specified field set to the | 
|  | 90 | * specified value. | 
|  | 91 | * | 
|  | 92 | * @param field journal field to use during search | 
|  | 93 | * @param fieldValue expected field value | 
|  | 94 | * @param max Maximum number of messages to return. | 
|  | 95 | *        Specify 0 to return all matching messages. | 
|  | 96 | * | 
|  | 97 | * @return matching messages from the journal | 
|  | 98 | */ | 
|  | 99 | virtual std::vector<std::string> getMessages(const std::string& field, | 
|  | 100 | const std::string& fieldValue, | 
|  | 101 | unsigned int max = 0) = 0; | 
| Shawn McCarney | 5248b8a | 2020-07-15 22:59:36 -0500 | [diff] [blame] | 102 | }; | 
|  | 103 |  | 
|  | 104 | /** | 
|  | 105 | * @class SystemdJournal | 
|  | 106 | * | 
|  | 107 | * Implementation of the Journal interface that writes to the systemd journal. | 
|  | 108 | */ | 
|  | 109 | class SystemdJournal : public Journal | 
|  | 110 | { | 
|  | 111 | public: | 
|  | 112 | // Specify which compiler-generated methods we want | 
|  | 113 | SystemdJournal() = default; | 
|  | 114 | SystemdJournal(const SystemdJournal&) = delete; | 
|  | 115 | SystemdJournal(SystemdJournal&&) = delete; | 
|  | 116 | SystemdJournal& operator=(const SystemdJournal&) = delete; | 
|  | 117 | SystemdJournal& operator=(SystemdJournal&&) = delete; | 
|  | 118 | virtual ~SystemdJournal() = default; | 
|  | 119 |  | 
| Bob King | ca08a79 | 2020-09-30 14:30:13 +0800 | [diff] [blame] | 120 | /** @copydoc Journal::getMessages() */ | 
|  | 121 | virtual std::vector<std::string> getMessages(const std::string& field, | 
|  | 122 | const std::string& fieldValue, | 
|  | 123 | unsigned int max) override; | 
|  | 124 |  | 
| Shawn McCarney | 5248b8a | 2020-07-15 22:59:36 -0500 | [diff] [blame] | 125 | /** @copydoc Journal::logDebug(const std::string&) */ | 
|  | 126 | virtual void logDebug(const std::string& message) override | 
|  | 127 | { | 
|  | 128 | using namespace phosphor::logging; | 
|  | 129 | log<level::DEBUG>(message.c_str()); | 
|  | 130 | } | 
|  | 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 | { | 
|  | 144 | using namespace phosphor::logging; | 
|  | 145 | log<level::ERR>(message.c_str()); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | /** @copydoc Journal::logError(const std::vector<std::string>&) */ | 
|  | 149 | virtual void logError(const std::vector<std::string>& messages) override | 
|  | 150 | { | 
|  | 151 | for (const std::string& message : messages) | 
|  | 152 | { | 
|  | 153 | logError(message); | 
|  | 154 | } | 
|  | 155 | } | 
|  | 156 |  | 
|  | 157 | /** @copydoc Journal::logInfo(const std::string&) */ | 
|  | 158 | virtual void logInfo(const std::string& message) override | 
|  | 159 | { | 
|  | 160 | using namespace phosphor::logging; | 
|  | 161 | log<level::INFO>(message.c_str()); | 
|  | 162 | } | 
|  | 163 |  | 
|  | 164 | /** @copydoc Journal::logInfo(const std::vector<std::string>&) */ | 
|  | 165 | virtual void logInfo(const std::vector<std::string>& messages) override | 
|  | 166 | { | 
|  | 167 | for (const std::string& message : messages) | 
|  | 168 | { | 
|  | 169 | logInfo(message); | 
|  | 170 | } | 
|  | 171 | } | 
| Bob King | ca08a79 | 2020-09-30 14:30:13 +0800 | [diff] [blame] | 172 |  | 
|  | 173 | private: | 
|  | 174 | /** | 
|  | 175 | * Gets the data object associated with a specific field from the | 
|  | 176 | * current journal entry and return the data as string. | 
|  | 177 | * | 
|  | 178 | * @param journal the current journal entry | 
|  | 179 | * @param field journal field to use during search | 
|  | 180 | */ | 
|  | 181 | std::string getFieldValue(sd_journal* journal, const char* field) const; | 
| Shawn McCarney | 5248b8a | 2020-07-15 22:59:36 -0500 | [diff] [blame] | 182 | }; | 
|  | 183 |  | 
|  | 184 | } // namespace phosphor::power::regulators |