| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 1 | #include "logger.hpp" |
| 2 | |
| Souvik Roy | 8042bbf | 2025-10-06 09:16:50 +0000 | [diff] [blame] | 3 | #include <regex> |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 4 | #include <sstream> |
| 5 | |
| 6 | namespace vpd |
| 7 | { |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 8 | std::shared_ptr<Logger> Logger::m_loggerInstance; |
| 9 | |
| 10 | void Logger::logMessage(std::string_view i_message, |
| 11 | const PlaceHolder& i_placeHolder, |
| 12 | const types::PelInfoTuple* i_pelTuple, |
| 13 | const std::source_location& i_location) |
| 14 | { |
| 15 | std::ostringstream l_log; |
| 16 | l_log << "FileName: " << i_location.file_name() << "," |
| 17 | << " Line: " << i_location.line() << " " << i_message; |
| 18 | |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 19 | if ((i_placeHolder == PlaceHolder::COLLECTION) && m_collectionLogger) |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 20 | { |
| 21 | // Log it to a specific place. |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 22 | m_collectionLogger->logMessage(l_log.str()); |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 23 | } |
| 24 | else if (i_placeHolder == PlaceHolder::PEL) |
| 25 | { |
| 26 | if (i_pelTuple) |
| 27 | { |
| 28 | // LOG PEL |
| 29 | // This should call create PEL API from the event logger. |
| 30 | return; |
| 31 | } |
| 32 | std::cout << "Pel info tuple required to log PEL for message <" + |
| 33 | l_log.str() + ">" |
| 34 | << std::endl; |
| 35 | } |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 36 | else if ((i_placeHolder == PlaceHolder::VPD_WRITE) && m_vpdWriteLogger) |
| 37 | { |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 38 | m_vpdWriteLogger->logMessage(l_log.str()); |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 39 | } |
| Sunny Srivastava | 5779d97 | 2025-08-08 01:45:23 -0500 | [diff] [blame] | 40 | else |
| 41 | { |
| 42 | // Default case, let it go to journal. |
| 43 | std::cout << l_log.str() << std::endl; |
| 44 | } |
| 45 | } |
| 46 | |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 47 | void Logger::initiateVpdCollectionLogging() noexcept |
| 48 | { |
| 49 | try |
| 50 | { |
| Souvik Roy | 8042bbf | 2025-10-06 09:16:50 +0000 | [diff] [blame] | 51 | // collection log file directory |
| 52 | const std::filesystem::path l_collectionLogDirectory{"/var/lib/vpd"}; |
| 53 | |
| 54 | std::error_code l_ec; |
| 55 | if (!std::filesystem::exists(l_collectionLogDirectory, l_ec)) |
| 56 | { |
| 57 | if (l_ec) |
| 58 | { |
| 59 | throw std::runtime_error( |
| 60 | "File system call to exist failed with error = " + |
| 61 | l_ec.message()); |
| 62 | } |
| 63 | throw std::runtime_error( |
| 64 | "Directory " + l_collectionLogDirectory.string() + |
| 65 | " does not exist"); |
| 66 | } |
| 67 | |
| 68 | // base name of collection log file |
| 69 | std::filesystem::path l_collectionLogFilePath{l_collectionLogDirectory}; |
| 70 | l_collectionLogFilePath /= "collection"; |
| 71 | |
| 72 | unsigned l_collectionLogFileCount{0}; |
| 73 | |
| 74 | std::filesystem::file_time_type l_oldestFileTime; |
| 75 | std::filesystem::path l_oldestFilePath{l_collectionLogFilePath}; |
| 76 | |
| 77 | // iterate through all entries in the log directory |
| 78 | for (const auto& l_dirEntry : |
| 79 | std::filesystem::directory_iterator(l_collectionLogDirectory)) |
| 80 | { |
| 81 | // check /var/lib/vpd for number "collection.*" log file |
| 82 | const std::regex l_collectionLogFileRegex{"collection.*\\.log"}; |
| 83 | |
| 84 | if (std::filesystem::is_regular_file(l_dirEntry.path()) && |
| 85 | std::regex_match(l_dirEntry.path().filename().string(), |
| 86 | l_collectionLogFileRegex)) |
| 87 | { |
| 88 | // check the write time of this file |
| 89 | const auto l_fileWriteTime = |
| 90 | std::filesystem::last_write_time(l_dirEntry.path()); |
| 91 | |
| 92 | // update oldest file path if required |
| 93 | if (l_fileWriteTime < l_oldestFileTime) |
| 94 | { |
| 95 | l_oldestFileTime = l_fileWriteTime; |
| 96 | l_oldestFilePath = l_dirEntry.path(); |
| 97 | } |
| 98 | |
| 99 | l_collectionLogFileCount++; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // maximum number of collection log files to maintain |
| 104 | constexpr auto l_maxCollectionLogFiles{3}; |
| 105 | |
| 106 | if (l_collectionLogFileCount >= l_maxCollectionLogFiles) |
| 107 | { |
| 108 | // delete oldest collection log file |
| 109 | l_collectionLogFilePath = l_oldestFilePath; |
| 110 | |
| 111 | logMessage("Deleting collection log file " + |
| 112 | l_collectionLogFilePath.string()); |
| 113 | |
| 114 | std::error_code l_ec; |
| 115 | if (!std::filesystem::remove(l_collectionLogFilePath, l_ec)) |
| 116 | { |
| 117 | logMessage("Failed to delete existing collection log file " + |
| 118 | l_collectionLogFilePath.string() + |
| 119 | " Error: " + l_ec.message()); |
| 120 | } |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | l_collectionLogFilePath += |
| 125 | "_" + std::to_string(l_collectionLogFileCount) + ".log"; |
| 126 | } |
| 127 | |
| 128 | // create collection logger object with collection_(n+1).log |
| Souvik Roy | 0fb5c3b | 2025-09-15 08:18:25 +0000 | [diff] [blame] | 129 | m_collectionLogger.reset( |
| Souvik Roy | 8042bbf | 2025-10-06 09:16:50 +0000 | [diff] [blame] | 130 | new AsyncFileLogger(l_collectionLogFilePath, 4096)); |
| Souvik Roy | a5e18b8 | 2025-09-25 05:59:56 +0000 | [diff] [blame] | 131 | } |
| 132 | catch (const std::exception& l_ex) |
| 133 | { |
| 134 | logMessage("Failed to initialize collection logger. Error: " + |
| 135 | std::string(l_ex.what())); |
| 136 | } |
| 137 | } |
| 138 | |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 139 | void SyncFileLogger::logMessage(const std::string_view& i_message) |
| 140 | { |
| 141 | try |
| 142 | { |
| 143 | if (++m_currentNumEntries > m_maxEntries) |
| 144 | { |
| 145 | rotateFile(); |
| 146 | } |
| 147 | m_fileStream << timestamp() << " : " << i_message << std::endl; |
| 148 | } |
| 149 | catch (const std::exception& l_ex) |
| 150 | { |
| 151 | // log message to journal if we fail to log to file |
| 152 | auto l_logger = Logger::getLoggerInstance(); |
| 153 | l_logger->logMessage(i_message); |
| 154 | } |
| 155 | } |
| 156 | |
| Souvik Roy | f858736 | 2025-09-25 09:56:20 +0000 | [diff] [blame] | 157 | void AsyncFileLogger::logMessage(const std::string_view& i_message) |
| Souvik Roy | 0fb5c3b | 2025-09-15 08:18:25 +0000 | [diff] [blame] | 158 | { |
| 159 | try |
| 160 | { |
| Souvik Roy | f858736 | 2025-09-25 09:56:20 +0000 | [diff] [blame] | 161 | // acquire lock on queue |
| 162 | std::unique_lock<std::mutex> l_lock(m_mutex); |
| 163 | |
| 164 | // push message to queue |
| 165 | m_messageQueue.emplace(timestamp() + " : " + std::string(i_message)); |
| 166 | |
| 167 | // notify log worker thread |
| 168 | m_cv.notify_one(); |
| Souvik Roy | 0fb5c3b | 2025-09-15 08:18:25 +0000 | [diff] [blame] | 169 | } |
| 170 | catch (const std::exception& l_ex) |
| 171 | { |
| Souvik Roy | f858736 | 2025-09-25 09:56:20 +0000 | [diff] [blame] | 172 | // log message to journal if we fail to push message to queue |
| 173 | Logger::getLoggerInstance()->logMessage(i_message); |
| Souvik Roy | 0fb5c3b | 2025-09-15 08:18:25 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | void AsyncFileLogger::fileWorker() noexcept |
| 178 | { |
| Souvik Roy | f858736 | 2025-09-25 09:56:20 +0000 | [diff] [blame] | 179 | // create lock object on mutex |
| 180 | std::unique_lock<std::mutex> l_lock(m_mutex); |
| 181 | |
| 182 | // infinite loop |
| 183 | while (true) |
| 184 | { |
| 185 | // check for exit conditions |
| 186 | if (!m_fileStream.is_open() || m_stopLogging) |
| 187 | { |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | // wait for notification from log producer |
| 192 | m_cv.wait(l_lock, |
| 193 | [this] { return m_stopLogging || !m_messageQueue.empty(); }); |
| 194 | |
| 195 | // flush the queue |
| 196 | while (!m_messageQueue.empty()) |
| 197 | { |
| 198 | // read the first message in queue |
| 199 | const auto l_logMessage = m_messageQueue.front(); |
| 200 | try |
| 201 | { |
| 202 | // pop the message from queue |
| 203 | m_messageQueue.pop(); |
| 204 | |
| 205 | // unlock mutex on queue |
| 206 | l_lock.unlock(); |
| 207 | |
| Souvik Roy | f858736 | 2025-09-25 09:56:20 +0000 | [diff] [blame] | 208 | // flush the message to file |
| 209 | m_fileStream << l_logMessage << std::endl; |
| 210 | |
| 211 | // lock mutex on queue |
| 212 | l_lock.lock(); |
| 213 | } |
| 214 | catch (const std::exception& l_ex) |
| 215 | { |
| 216 | // log message to journal if we fail to push message to queue |
| 217 | Logger::getLoggerInstance()->logMessage(l_logMessage); |
| 218 | |
| 219 | // check if we need to reacquire lock before continuing to flush |
| 220 | // queue |
| 221 | if (!l_lock.owns_lock()) |
| 222 | { |
| 223 | l_lock.lock(); |
| 224 | } |
| 225 | } |
| 226 | } // queue flush loop |
| 227 | } // thread loop |
| Souvik Roy | 0fb5c3b | 2025-09-15 08:18:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| Souvik Roy | bac8ba6 | 2025-10-27 09:07:06 +0000 | [diff] [blame^] | 230 | ILogFileHandler::ILogFileHandler(const std::filesystem::path& i_filePath, |
| 231 | const size_t i_maxEntries) : |
| 232 | m_filePath{i_filePath}, m_maxEntries{i_maxEntries} |
| 233 | { |
| 234 | // check if log file already exists |
| 235 | std::error_code l_ec; |
| 236 | const bool l_logFileExists = std::filesystem::exists(m_filePath, l_ec); |
| 237 | if (l_ec) |
| 238 | { |
| 239 | Logger::getLoggerInstance()->logMessage( |
| 240 | "Failed to check if log file already exists. Error: " + |
| 241 | l_ec.message()); |
| 242 | } |
| 243 | |
| 244 | // open the file in append mode |
| 245 | m_fileStream.open(m_filePath, std::ios::out | std::ios::app); |
| 246 | // enable exception mask to throw on badbit and failbit |
| 247 | m_fileStream.exceptions(std::ios_base::badbit | std::ios_base::failbit); |
| 248 | |
| 249 | if (l_logFileExists) |
| 250 | { |
| 251 | // log file already exists, check and update the number of entries |
| 252 | std::ifstream l_readFileStream{m_filePath}; |
| 253 | for (std::string l_line; std::getline(l_readFileStream, l_line); |
| 254 | ++m_currentNumEntries) |
| 255 | {} |
| 256 | |
| 257 | l_readFileStream.close(); |
| 258 | } |
| 259 | } |
| 260 | |
| Souvik Roy | a8c3c09 | 2025-09-11 10:49:29 +0000 | [diff] [blame] | 261 | void ILogFileHandler::rotateFile( |
| 262 | [[maybe_unused]] const unsigned i_numEntriesToDelete) |
| Souvik Roy | 6bd74ad | 2025-09-12 06:24:06 +0000 | [diff] [blame] | 263 | { |
| 264 | /* TODO: |
| 265 | - delete specified number of oldest entries from beginning of file |
| 266 | - rewrite file to move existing logs to beginning of file |
| 267 | */ |
| 268 | m_currentNumEntries = m_maxEntries - i_numEntriesToDelete; |
| 269 | } |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 270 | namespace logging |
| 271 | { |
| 272 | void logMessage(std::string_view message, const std::source_location& location) |
| 273 | { |
| 274 | std::ostringstream log; |
| 275 | log << "FileName: " << location.file_name() << "," |
| 276 | << " Line: " << location.line() << " " << message; |
| 277 | |
| Sunny Srivastava | fa5e4d3 | 2023-03-12 11:59:49 -0500 | [diff] [blame] | 278 | std::cout << log.str() << std::endl; |
| 279 | } |
| 280 | } // namespace logging |
| 281 | } // namespace vpd |