LCOV - code coverage report
Current view: top level - src/core - Logger.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 30 30
Test Date: 2026-04-10 19:03:25 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2025 aeml
       3              :  *
       4              :  * This program is free software: you can redistribute it and/or modify
       5              :  * it under the terms of the GNU General Public License as published by
       6              :  * the Free Software Foundation, either version 3 of the License, or
       7              :  * (at your option) any later version.
       8              :  *
       9              :  * This program is distributed in the hope that it will be useful,
      10              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12              :  * GNU General Public License for more details.
      13              :  *
      14              :  * You should have received a copy of the GNU General Public License
      15              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      16              :  */
      17              : 
      18              : #include "core/Logger.hpp"
      19              : 
      20              : #include <chrono>
      21              : #include <ctime>
      22              : #include <fstream>
      23              : #include <iomanip>
      24              : #include <iostream>
      25              : #include <mutex>
      26              : #include <sstream>
      27              : 
      28              : namespace
      29              : {
      30              :     std::mutex g_logMutex;
      31              : 
      32          154 :     std::string CurrentTimeString()
      33              :     {
      34              :         using clock = std::chrono::system_clock;
      35          154 :         const auto now   = clock::now();
      36          154 :         const auto timeT = clock::to_time_t(now);
      37              : 
      38          154 :         std::tm localTm{};
      39              :     #if defined(_WIN32)
      40              :         localtime_s(&localTm, &timeT);
      41              :     #else
      42          154 :         localtime_r(&timeT, &localTm);
      43              :     #endif
      44              : 
      45          154 :         std::ostringstream oss;
      46          154 :         oss << std::put_time(&localTm, "%Y-%m-%d %H:%M:%S");
      47          308 :         return oss.str();
      48          154 :     }
      49              : 
      50          154 :     void LogWithLevel(std::ostream& os, const char* level, const std::string& message)
      51              :     {
      52          154 :         std::lock_guard<std::mutex> lock{g_logMutex};
      53          154 :         os << "[" << CurrentTimeString() << "] "
      54          154 :            << level << ": " << message << '\n';
      55          154 :     }
      56              : }
      57              : 
      58              : namespace core
      59              : {
      60           28 :     Logger::Logger() = default;
      61              : 
      62          140 :     void Logger::Info(const std::string& message) const
      63              :     {
      64          140 :         auto& os = m_stream ? *m_stream : std::cout;
      65          140 :         LogWithLevel(os, "INFO", message);
      66          140 :     }
      67              : 
      68            1 :     void Logger::Warn(const std::string& message) const
      69              :     {
      70            1 :         auto& os = m_stream ? *m_stream : std::cout;
      71            1 :         LogWithLevel(os, "WARN", message);
      72            1 :     }
      73              : 
      74           13 :     void Logger::Error(const std::string& message) const
      75              :     {
      76           13 :         auto& os = m_stream ? *m_stream : std::cout;
      77           13 :         LogWithLevel(os, "ERROR", message);
      78           13 :     }
      79              : 
      80            4 :     void Logger::SetOutput(std::shared_ptr<std::ostream> stream)
      81              :     {
      82            4 :         m_stream = std::move(stream);
      83            4 :     }
      84              : }
        

Generated by: LCOV version 2.0-1