| 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 | |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 5 | #include <filesystem> |
| 6 | #include <fstream> |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 7 | #include <iostream> |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 8 | #include <memory> |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 9 | #include <source_location> |
| 10 | #include <string_view> |
| 11 | |
| 12 | namespace vpd |
| 13 | { |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * @brief Enum class defining placeholder tags. |
| 17 | * |
| 18 | * The tag will be used by APIs to identify the endpoint for a given log |
| 19 | * message. |
| 20 | */ |
| 21 | enum class PlaceHolder |
| 22 | { |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 23 | DEFAULT, /* logs to the journal */ |
| 24 | PEL, /* Creates a PEL */ |
| 25 | COLLECTION, /* Logs collection messages */ |
| 26 | VPD_WRITE /* Logs VPD write details */ |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 27 | }; |
| 28 | |
| 29 | /** |
| 30 | * @brief Class to handle file operations w.r.t logging. |
| 31 | * Based on the placeholder the class will handle different file operations to |
| 32 | * log error messages. |
| 33 | */ |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 34 | class ILogFileHandler |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 35 | { |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 36 | protected: |
| 37 | // absolute file path of log file |
| 38 | std::filesystem::path m_filePath{}; |
| 39 | |
| 40 | // max number of log entries in file |
| 41 | size_t m_maxEntries{256}; |
| 42 | |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 43 | // file stream object to do file operations |
| 44 | std::fstream m_fileStream; |
| 45 | |
| 46 | // current number of log entries in file |
| 47 | size_t m_currentNumEntries{0}; |
| 48 | |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 49 | /** |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 50 | * @brief API to rotate file. |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 51 | * |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 52 | * This API rotates the logs within a file by deleting specified number of |
| 53 | * oldest entries. |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 54 | * |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 55 | * @param[in] i_numEntriesToDelete - Number of entries to delete. |
| 56 | * |
| 57 | * @throw std::runtime_error |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 58 | */ |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 59 | virtual void rotateFile( |
| 60 | [[maybe_unused]] const unsigned i_numEntriesToDelete = 5); |
| 61 | |
| 62 | /** |
| 63 | * @brief Constructor. |
| 64 | * Private so that can't be initialized by class(es) other than friends. |
| 65 | * |
| 66 | * @param[in] i_filePath - Absolute path of the log file. |
| 67 | * @param[in] i_maxEntries - Maximum number of entries in the log file after |
| 68 | * which the file will be rotated. |
| 69 | */ |
| 70 | ILogFileHandler(const std::filesystem::path& i_filePath, |
| 71 | const size_t i_maxEntries) : |
| 72 | m_filePath{i_filePath}, m_maxEntries{i_maxEntries} |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 73 | { |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 74 | // open the file in append mode |
| 75 | m_fileStream.open(m_filePath, std::ios::out | std::ios::app); |
| 76 | |
| 77 | // enable exception mask to throw on badbit and failbit |
| 78 | m_fileStream.exceptions(std::ios_base::badbit | std::ios_base::failbit); |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 79 | } |
| 80 | |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 81 | /** |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 82 | * @brief API to generate timestamp in string format. |
| 83 | * |
| 84 | * @return Returns timestamp in string format on success, otherwise returns |
| 85 | * empty string in case of any error. |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 86 | */ |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 87 | static inline std::string timestamp() noexcept |
| 88 | { |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 89 | try |
| 90 | { |
| 91 | const auto l_now = std::chrono::system_clock::now(); |
| 92 | const auto l_in_time_t = |
| 93 | std::chrono::system_clock::to_time_t(l_now); |
| 94 | const auto l_ms = |
| 95 | std::chrono::duration_cast<std::chrono::milliseconds>( |
| 96 | l_now.time_since_epoch()) % |
| 97 | 1000; |
| 98 | |
| 99 | std::stringstream l_ss; |
| 100 | l_ss << std::put_time(std::localtime(&l_in_time_t), |
| 101 | "%Y-%m-%d %H:%M:%S") |
| 102 | << "." << std::setfill('0') << std::setw(3) << l_ms.count(); |
| 103 | return l_ss.str(); |
| 104 | } |
| 105 | catch (const std::exception& l_ex) |
| 106 | { |
| 107 | return std::string{}; |
| 108 | } |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 109 | } |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 110 | |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 111 | public: |
| 112 | // deleted methods |
| 113 | ILogFileHandler() = delete; |
| 114 | ILogFileHandler(const ILogFileHandler&) = delete; |
| 115 | ILogFileHandler(const ILogFileHandler&&) = delete; |
| 116 | ILogFileHandler operator=(const ILogFileHandler&) = delete; |
| 117 | ILogFileHandler operator=(const ILogFileHandler&&) = delete; |
| 118 | |
| 119 | /** |
| 120 | * @brief API to log a message to file. |
| 121 | * |
| 122 | * @param[in] i_message - Message to log. |
| 123 | * |
| 124 | * @throw std::runtime_error |
| 125 | */ |
| 126 | virtual void logMessage( |
| 127 | [[maybe_unused]] const std::string_view& i_message) = 0; |
| 128 | |
| 129 | // destructor |
| 130 | virtual ~ILogFileHandler() |
| 131 | { |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 132 | if (m_fileStream.is_open()) |
| 133 | { |
| 134 | m_fileStream.close(); |
| 135 | } |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 136 | } |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | /** |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 140 | * @brief A class to handle logging messages to file synchronously |
| 141 | * |
| 142 | * This class handles logging messages to a specific file in a synchronous |
| 143 | * manner. |
| 144 | * Note: The logMessage API of this class is not multi-thread safe. |
| 145 | */ |
| 146 | class SyncFileLogger final : public ILogFileHandler |
| 147 | { |
| 148 | /** |
| 149 | * @brief Parameterized constructor. |
| 150 | * Private so that can't be initialized by class(es) other than friends. |
| 151 | * |
| 152 | * @param[in] i_filePath - Absolute path of the log file. |
| 153 | * @param[in] i_maxEntries - Maximum number of entries in the log file after |
| 154 | * which the file will be rotated. |
| 155 | */ |
| 156 | SyncFileLogger(const std::filesystem::path& i_filePath, |
| 157 | const size_t i_maxEntries) : |
| 158 | ILogFileHandler(i_filePath, i_maxEntries) |
| 159 | {} |
| 160 | |
| 161 | public: |
| 162 | // Friend class Logger. |
| 163 | friend class Logger; |
| 164 | |
| 165 | // deleted methods |
| 166 | SyncFileLogger() = delete; |
| 167 | SyncFileLogger(const SyncFileLogger&) = delete; |
| 168 | SyncFileLogger(const SyncFileLogger&&) = delete; |
| 169 | SyncFileLogger operator=(const SyncFileLogger&) = delete; |
| 170 | SyncFileLogger operator=(const SyncFileLogger&&) = delete; |
| 171 | |
| 172 | /** |
| 173 | * @brief API to log a message to file |
| 174 | * |
| 175 | * This API logs messages to file in a synchronous manner. |
| 176 | * Note: This API is not multi-thread safe. |
| 177 | * |
| 178 | * @param[in] i_message - Message to log |
| 179 | * |
| 180 | * @throw std::runtime_error |
| 181 | */ |
| 182 | void logMessage(const std::string_view& i_message) override; |
| 183 | |
| 184 | // destructor |
| 185 | ~SyncFileLogger() = default; |
| 186 | }; |
| 187 | |
| 188 | /** |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 189 | * @brief Singleton class to handle error logging for the repository. |
| 190 | */ |
| 191 | class Logger |
| 192 | { |
| 193 | public: |
| 194 | /** |
| 195 | * @brief Deleted Methods |
| 196 | */ |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 197 | Logger(const Logger&) = delete; // Copy constructor |
| 198 | Logger(const Logger&&) = delete; // Move constructor |
| 199 | Logger operator=(const Logger&) = delete; // Copy assignment operator |
| 200 | Logger operator=(const Logger&&) = delete; // Move assignment operator |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * @brief Method to get instance of Logger class. |
| 204 | */ |
| 205 | static std::shared_ptr<Logger> getLoggerInstance() |
| 206 | { |
| 207 | if (!m_loggerInstance) |
| 208 | { |
| 209 | m_loggerInstance = std::shared_ptr<Logger>(new Logger()); |
| 210 | } |
| 211 | return m_loggerInstance; |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * @brief API to log a given error message. |
| 216 | * |
| 217 | * @param[in] i_message - Message to be logged. |
| 218 | * @param[in] i_placeHolder - States where the message needs to be logged. |
| 219 | * Default is journal. |
| 220 | * @param[in] i_pelTuple - A structure only required in case message needs |
| 221 | * to be logged as PEL. |
| 222 | * @param[in] i_location - Locatuon from where message needs to be logged. |
| 223 | */ |
| 224 | void logMessage(std::string_view i_message, |
| 225 | const PlaceHolder& i_placeHolder = PlaceHolder::DEFAULT, |
| 226 | const types::PelInfoTuple* i_pelTuple = nullptr, |
| 227 | const std::source_location& i_location = |
| 228 | std::source_location::current()); |
| 229 | |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 230 | /** |
| 231 | * @brief API to initiate VPD collection logging. |
| 232 | * |
| 233 | * This API initiates VPD collection logging. It checks for existing |
| 234 | * collection log files and if 3 such files are found, it deletes the oldest |
| 235 | * file and initiates a VPD collection logger object, so that every new VPD |
| 236 | * collection flow always gets logged into a new file. |
| 237 | */ |
| 238 | void initiateVpdCollectionLogging() noexcept; |
| 239 | |
| 240 | /** |
| 241 | * @brief API to terminate VPD collection logging. |
| 242 | * |
| 243 | * This API terminates the VPD collection logging by destroying the |
| 244 | * associated VPD collection logger object. |
| 245 | */ |
| 246 | void terminateVpdCollectionLogging() noexcept |
| 247 | { |
| 248 | // TODO: reset VPD collection logger |
| 249 | } |
| 250 | |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 251 | private: |
| 252 | /** |
| 253 | * @brief Constructor |
| 254 | */ |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 255 | Logger() : m_vpdWriteLogger(nullptr), m_collectionLogger(nullptr) |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 256 | { |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 257 | m_vpdWriteLogger.reset( |
| 258 | new SyncFileLogger("/var/lib/vpd/vpdWrite.log", 128)); |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // Instance to the logger class. |
| 262 | static std::shared_ptr<Logger> m_loggerInstance; |
| 263 | |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 264 | // logger object to handle VPD write logs |
| 265 | std::unique_ptr<ILogFileHandler> m_vpdWriteLogger; |
| 266 | |
| 267 | // logger object to handle VPD collection logs |
| 268 | std::unique_ptr<ILogFileHandler> m_collectionLogger; |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 269 | }; |
| 270 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 271 | /** |
| 272 | * @brief The namespace defines logging related methods for VPD. |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 273 | * Only for backward compatibility till new logger class comes up. |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 274 | */ |
| 275 | namespace logging |
| 276 | { |
| 277 | |
| 278 | /** |
| 279 | * @brief An api to log message. |
| 280 | * This API should be called to log message. It will auto append information |
| 281 | * like file name, line and function name to the message being logged. |
| 282 | * |
| 283 | * @param[in] message - Information that we want to log. |
| 284 | * @param[in] location - Object of source_location class. |
| 285 | */ |
| 286 | void logMessage(std::string_view message, const std::source_location& location = |
| 287 | std::source_location::current()); |
| 288 | } // namespace logging |
| 289 | } // namespace vpd |