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 
18 #include <phosphor-logging/log.hpp>
19 
20 #include <string>
21 #include <vector>
22 
23 namespace phosphor::power::regulators
24 {
25 
26 /**
27  * @class Journal
28  *
29  * Abstract base class that provides a journal interface.
30  *
31  * The interface is used to write messages/log entries to the system journal.
32  */
33 class Journal
34 {
35   public:
36     // Specify which compiler-generated methods we want
37     Journal() = default;
38     Journal(const Journal&) = delete;
39     Journal(Journal&&) = delete;
40     Journal& operator=(const Journal&) = delete;
41     Journal& operator=(Journal&&) = delete;
42     virtual ~Journal() = default;
43 
44     /**
45      * Logs a debug message in the system journal.
46      *
47      * @param message message to log
48      */
49     virtual void logDebug(const std::string& message) = 0;
50 
51     /**
52      * Logs debug messages in the system journal.
53      *
54      * @param messages messages to log
55      */
56     virtual void logDebug(const std::vector<std::string>& messages) = 0;
57 
58     /**
59      * Logs an error message in the system journal.
60      *
61      * @param message message to log
62      */
63     virtual void logError(const std::string& message) = 0;
64 
65     /**
66      * Logs error messages in the system journal.
67      *
68      * @param messages messages to log
69      */
70     virtual void logError(const std::vector<std::string>& messages) = 0;
71 
72     /**
73      * Logs an informational message in the system journal.
74      *
75      * @param message message to log
76      */
77     virtual void logInfo(const std::string& message) = 0;
78 
79     /**
80      * Logs informational messages in the system journal.
81      *
82      * @param messages messages to log
83      */
84     virtual void logInfo(const std::vector<std::string>& messages) = 0;
85 };
86 
87 /**
88  * @class SystemdJournal
89  *
90  * Implementation of the Journal interface that writes to the systemd journal.
91  */
92 class SystemdJournal : public Journal
93 {
94   public:
95     // Specify which compiler-generated methods we want
96     SystemdJournal() = default;
97     SystemdJournal(const SystemdJournal&) = delete;
98     SystemdJournal(SystemdJournal&&) = delete;
99     SystemdJournal& operator=(const SystemdJournal&) = delete;
100     SystemdJournal& operator=(SystemdJournal&&) = delete;
101     virtual ~SystemdJournal() = default;
102 
103     /** @copydoc Journal::logDebug(const std::string&) */
104     virtual void logDebug(const std::string& message) override
105     {
106         using namespace phosphor::logging;
107         log<level::DEBUG>(message.c_str());
108     }
109 
110     /** @copydoc Journal::logDebug(const std::vector<std::string>&) */
111     virtual void logDebug(const std::vector<std::string>& messages) override
112     {
113         for (const std::string& message : messages)
114         {
115             logDebug(message);
116         }
117     }
118 
119     /** @copydoc Journal::logError(const std::string&) */
120     virtual void logError(const std::string& message) override
121     {
122         using namespace phosphor::logging;
123         log<level::ERR>(message.c_str());
124     }
125 
126     /** @copydoc Journal::logError(const std::vector<std::string>&) */
127     virtual void logError(const std::vector<std::string>& messages) override
128     {
129         for (const std::string& message : messages)
130         {
131             logError(message);
132         }
133     }
134 
135     /** @copydoc Journal::logInfo(const std::string&) */
136     virtual void logInfo(const std::string& message) override
137     {
138         using namespace phosphor::logging;
139         log<level::INFO>(message.c_str());
140     }
141 
142     /** @copydoc Journal::logInfo(const std::vector<std::string>&) */
143     virtual void logInfo(const std::vector<std::string>& messages) override
144     {
145         for (const std::string& message : messages)
146         {
147             logInfo(message);
148         }
149     }
150 };
151 
152 } // namespace phosphor::power::regulators
153 
154 // TODO: Remove the functional interface below once all the code has switched to
155 // the new Journal interface.  Also delete journal.cpp and remove references to
156 // it in meson files.
157 
158 /**
159  * Systemd journal interface.
160  *
161  * Provides functions to log messages to the systemd journal.
162  *
163  * This interface provides an abstraction layer so that testcases can use a mock
164  * implementation and validate the logged messages.
165  *
166  * This interface does not currently provide the ability to specify key/value
167  * pairs to provide more information in the journal entry.  It will be added
168  * later if needed.
169  */
170 namespace phosphor::power::regulators::journal
171 {
172 
173 /**
174  * Logs a message with a priority value of 'ERR' to the systemd journal.
175  *
176  * @param message message to log
177  */
178 void logErr(const std::string& message);
179 
180 /**
181  * Logs a message with a priority value of 'INFO' to the systemd journal.
182  *
183  * @param message message to log
184  */
185 void logInfo(const std::string& message);
186 
187 /**
188  * Logs a message with a priority value of 'DEBUG' to the systemd journal.
189  *
190  * @param message message to log
191  */
192 void logDebug(const std::string& message);
193 
194 } // namespace phosphor::power::regulators::journal
195