xref: /openbmc/openpower-vpd-parser/vpd-manager/include/logger.hpp (revision a5e18b8af0ca2c2119a75aef68e5252af340883b)
1 #pragma once
2 
3 #include "types.hpp"
4 
5 #include <iostream>
6 #include <memory>
7 #include <source_location>
8 #include <string_view>
9 
10 namespace vpd
11 {
12 
13 /**
14  * @brief Enum class defining placeholder tags.
15  *
16  * The tag will be used by APIs to identify the endpoint for a given log
17  * message.
18  */
19 enum class PlaceHolder
20 {
21     DEFAULT,    /* logs to the journal */
22     PEL,        /* Creates a PEL */
23     COLLECTION, /* Logs collection messages */
24     VPD_WRITE   /* Logs VPD write details */
25 };
26 
27 /**
28  * @brief Class to handle file operations w.r.t logging.
29  * Based on the placeholder the class will handle different file operations to
30  * log error messages.
31  */
32 class ILogFileHandler
33 {
34     // should hold fd's for files required as per placeholder.
35   public:
36     /**
37      * @brief API exposed to write a log message to a file.
38      *
39      * The API can be called by logger class in case log message needs to be
40      * redirected to a file. The endpoint can be decided based on the
41      * placeholder passed to the API.
42      *
43      * @param[in] i_placeHolder - Information about the endpoint.
44      */
writeLogToFile(const PlaceHolder & i_placeHolder)45     void writeLogToFile([[maybe_unused]] const PlaceHolder& i_placeHolder)
46     {
47         // Handle the file operations.
48     }
49 
50     // Frined class Logger.
51     friend class Logger;
52 
53   private:
54     /**
55      * @brief Constructor
56      * Private so that can't be initialized by class(es) other than friends.
57      */
ILogFileHandler()58     ILogFileHandler() {}
59 
60     /* Define APIs to handle file operation as per the placeholder. */
61 };
62 
63 /**
64  * @brief Singleton class to handle error logging for the repository.
65  */
66 class Logger
67 {
68   public:
69     /**
70      * @brief Deleted Methods
71      */
72     Logger(const Logger&) = delete;            // Copy constructor
73     Logger(const Logger&&) = delete;           // Move constructor
74     Logger operator=(const Logger&) = delete;  // Copy assignment operator
75     Logger operator=(const Logger&&) = delete; // Move assignment operator
76 
77     /**
78      * @brief Method to get instance of Logger class.
79      */
getLoggerInstance()80     static std::shared_ptr<Logger> getLoggerInstance()
81     {
82         if (!m_loggerInstance)
83         {
84             m_loggerInstance = std::shared_ptr<Logger>(new Logger());
85         }
86         return m_loggerInstance;
87     }
88 
89     /**
90      * @brief API to log a given error message.
91      *
92      * @param[in] i_message - Message to be logged.
93      * @param[in] i_placeHolder - States where the message needs to be logged.
94      * Default is journal.
95      * @param[in] i_pelTuple - A structure only required in case message needs
96      * to be logged as PEL.
97      * @param[in] i_location - Locatuon from where message needs to be logged.
98      */
99     void logMessage(std::string_view i_message,
100                     const PlaceHolder& i_placeHolder = PlaceHolder::DEFAULT,
101                     const types::PelInfoTuple* i_pelTuple = nullptr,
102                     const std::source_location& i_location =
103                         std::source_location::current());
104 
105     /**
106      * @brief API to initiate VPD collection logging.
107      *
108      * This API initiates VPD collection logging. It checks for existing
109      * collection log files and if 3 such files are found, it deletes the oldest
110      * file and initiates a VPD collection logger object, so that every new VPD
111      * collection flow always gets logged into a new file.
112      */
113     void initiateVpdCollectionLogging() noexcept;
114 
115     /**
116      * @brief API to terminate VPD collection logging.
117      *
118      * This API terminates the VPD collection logging by destroying the
119      * associated VPD collection logger object.
120      */
terminateVpdCollectionLogging()121     void terminateVpdCollectionLogging() noexcept
122     {
123         // TODO: reset VPD collection logger
124     }
125 
126   private:
127     /**
128      * @brief Constructor
129      */
Logger()130     Logger() : m_vpdWriteLogger(nullptr), m_collectionLogger(nullptr)
131     {
132         // TODO: initiate synchronous logger for VPD write logs
133     }
134 
135     // Instance to the logger class.
136     static std::shared_ptr<Logger> m_loggerInstance;
137 
138     // logger object to handle VPD write logs
139     std::unique_ptr<ILogFileHandler> m_vpdWriteLogger;
140 
141     // logger object to handle VPD collection logs
142     std::unique_ptr<ILogFileHandler> m_collectionLogger;
143 };
144 
145 /**
146  * @brief The namespace defines logging related methods for VPD.
147  * Only for backward compatibility till new logger class comes up.
148  */
149 namespace logging
150 {
151 
152 /**
153  * @brief An api to log message.
154  * This API should be called to log message. It will auto append information
155  * like file name, line and function name to the message being logged.
156  *
157  * @param[in] message - Information that we want  to log.
158  * @param[in] location - Object of source_location class.
159  */
160 void logMessage(std::string_view message, const std::source_location& location =
161                                               std::source_location::current());
162 } // namespace logging
163 } // namespace vpd
164