blob: 575aa19ad522f709b6ee21491a1bf65f9ccc9ef4 [file] [log] [blame]
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05001#pragma once
2
Sunny Srivastava5779d972025-08-08 01:45:23 -05003#include "types.hpp"
4
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05005#include <iostream>
Sunny Srivastava5779d972025-08-08 01:45:23 -05006#include <memory>
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -05007#include <source_location>
8#include <string_view>
9
10namespace vpd
11{
Sunny Srivastava5779d972025-08-08 01:45:23 -050012
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 */
19enum class PlaceHolder
20{
Souvik Roya5e18b82025-09-25 05:59:56 +000021 DEFAULT, /* logs to the journal */
22 PEL, /* Creates a PEL */
23 COLLECTION, /* Logs collection messages */
24 VPD_WRITE /* Logs VPD write details */
Sunny Srivastava5779d972025-08-08 01:45:23 -050025};
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 */
Souvik Roya5e18b82025-09-25 05:59:56 +000032class ILogFileHandler
Sunny Srivastava5779d972025-08-08 01:45:23 -050033{
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 */
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 */
Souvik Roya5e18b82025-09-25 05:59:56 +000058 ILogFileHandler() {}
Sunny Srivastava5779d972025-08-08 01:45:23 -050059
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 */
66class Logger
67{
68 public:
69 /**
70 * @brief Deleted Methods
71 */
Souvik Roya5e18b82025-09-25 05:59:56 +000072 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
Sunny Srivastava5779d972025-08-08 01:45:23 -050076
77 /**
78 * @brief Method to get instance of Logger class.
79 */
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
Souvik Roya5e18b82025-09-25 05:59:56 +0000105 /**
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 */
121 void terminateVpdCollectionLogging() noexcept
122 {
123 // TODO: reset VPD collection logger
124 }
125
Sunny Srivastava5779d972025-08-08 01:45:23 -0500126 private:
127 /**
128 * @brief Constructor
129 */
Souvik Roya5e18b82025-09-25 05:59:56 +0000130 Logger() : m_vpdWriteLogger(nullptr), m_collectionLogger(nullptr)
Sunny Srivastava5779d972025-08-08 01:45:23 -0500131 {
Souvik Roya5e18b82025-09-25 05:59:56 +0000132 // TODO: initiate synchronous logger for VPD write logs
Sunny Srivastava5779d972025-08-08 01:45:23 -0500133 }
134
135 // Instance to the logger class.
136 static std::shared_ptr<Logger> m_loggerInstance;
137
Souvik Roya5e18b82025-09-25 05:59:56 +0000138 // 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;
Sunny Srivastava5779d972025-08-08 01:45:23 -0500143};
144
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500145/**
146 * @brief The namespace defines logging related methods for VPD.
Sunny Srivastava5779d972025-08-08 01:45:23 -0500147 * Only for backward compatibility till new logger class comes up.
Sunny Srivastavafa5e4d32023-03-12 11:59:49 -0500148 */
149namespace 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 */
160void logMessage(std::string_view message, const std::source_location& location =
161 std::source_location::current());
162} // namespace logging
163} // namespace vpd