xref: /openbmc/openpower-vpd-parser/vpd-manager/include/logger.hpp (revision 5779d978aecdaab2c985193cab47d5fe9a7c1c77)
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 };
25 
26 /**
27  * @brief Class to handle file operations w.r.t logging.
28  * Based on the placeholder the class will handle different file operations to
29  * log error messages.
30  */
31 class LogFileHandler
32 {
33     // should hold fd's for files required as per placeholder.
34   public:
35     /**
36      * @brief API exposed to write a log message to a file.
37      *
38      * The API can be called by logger class in case log message needs to be
39      * redirected to a file. The endpoint can be decided based on the
40      * placeholder passed to the API.
41      *
42      * @param[in] i_placeHolder - Information about the endpoint.
43      */
writeLogToFile(const PlaceHolder & i_placeHolder)44     void writeLogToFile([[maybe_unused]] const PlaceHolder& i_placeHolder)
45     {
46         // Handle the file operations.
47     }
48 
49     // Frined class Logger.
50     friend class Logger;
51 
52   private:
53     /**
54      * @brief Constructor
55      * Private so that can't be initialized by class(es) other than friends.
56      */
LogFileHandler()57     LogFileHandler() {}
58 
59     /* Define APIs to handle file operation as per the placeholder. */
60 };
61 
62 /**
63  * @brief Singleton class to handle error logging for the repository.
64  */
65 class Logger
66 {
67   public:
68     /**
69      * @brief Deleted Methods
70      */
71     Logger(const Logger&) = delete;  // Copy constructor
72     Logger(const Logger&&) = delete; // Move constructor
73 
74     /**
75      * @brief Method to get instance of Logger class.
76      */
getLoggerInstance()77     static std::shared_ptr<Logger> getLoggerInstance()
78     {
79         if (!m_loggerInstance)
80         {
81             m_loggerInstance = std::shared_ptr<Logger>(new Logger());
82         }
83         return m_loggerInstance;
84     }
85 
86     /**
87      * @brief API to log a given error message.
88      *
89      * @param[in] i_message - Message to be logged.
90      * @param[in] i_placeHolder - States where the message needs to be logged.
91      * Default is journal.
92      * @param[in] i_pelTuple - A structure only required in case message needs
93      * to be logged as PEL.
94      * @param[in] i_location - Locatuon from where message needs to be logged.
95      */
96     void logMessage(std::string_view i_message,
97                     const PlaceHolder& i_placeHolder = PlaceHolder::DEFAULT,
98                     const types::PelInfoTuple* i_pelTuple = nullptr,
99                     const std::source_location& i_location =
100                         std::source_location::current());
101 
102   private:
103     /**
104      * @brief Constructor
105      */
Logger()106     Logger() : m_logFileHandler(nullptr)
107     {
108         m_logFileHandler =
109             std::shared_ptr<LogFileHandler>(new LogFileHandler());
110     }
111 
112     // Instance to the logger class.
113     static std::shared_ptr<Logger> m_loggerInstance;
114 
115     // Instance to LogFileHandler class.
116     std::shared_ptr<LogFileHandler> m_logFileHandler;
117 };
118 
119 /**
120  * @brief The namespace defines logging related methods for VPD.
121  * Only for backward compatibility till new logger class comes up.
122  */
123 namespace logging
124 {
125 
126 /**
127  * @brief An api to log message.
128  * This API should be called to log message. It will auto append information
129  * like file name, line and function name to the message being logged.
130  *
131  * @param[in] message - Information that we want  to log.
132  * @param[in] location - Object of source_location class.
133  */
134 void logMessage(std::string_view message, const std::source_location& location =
135                                               std::source_location::current());
136 } // namespace logging
137 } // namespace vpd
138