Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 3 | #include "types.hpp" |
| 4 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 5 | #include <iostream> |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 6 | #include <memory> |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 7 | #include <source_location> |
| 8 | #include <string_view> |
| 9 | |
| 10 | namespace vpd |
| 11 | { |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 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 | { |
Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 21 | DEFAULT, /* logs to the journal */ |
| 22 | PEL, /* Creates a PEL */ |
| 23 | COLLECTION, /* Logs collection messages */ |
| 24 | VPD_WRITE /* Logs VPD write details */ |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 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 | */ |
Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 32 | class ILogFileHandler |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 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 | */ |
| 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 Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 58 | ILogFileHandler() {} |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 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 | */ |
Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 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 |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 76 | |
| 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 Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 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 | */ |
| 121 | void terminateVpdCollectionLogging() noexcept |
| 122 | { |
| 123 | // TODO: reset VPD collection logger |
| 124 | } |
| 125 | |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 126 | private: |
| 127 | /** |
| 128 | * @brief Constructor |
| 129 | */ |
Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 130 | Logger() : m_vpdWriteLogger(nullptr), m_collectionLogger(nullptr) |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 131 | { |
Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 132 | // TODO: initiate synchronous logger for VPD write logs |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Instance to the logger class. |
| 136 | static std::shared_ptr<Logger> m_loggerInstance; |
| 137 | |
Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame^] | 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; |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 143 | }; |
| 144 | |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 145 | /** |
| 146 | * @brief The namespace defines logging related methods for VPD. |
Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 147 | * Only for backward compatibility till new logger class comes up. |
Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 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 |