LCOV - code coverage report
Current view: top level - src/core/log - log_entry.cpp (source / functions) Hit Total Coverage
Test: libopenmodelviewer coverage Lines: 36 37 97.3 %
Date: 2025-07-03 20:26:48 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //
       2             : // Copyright 2024 OpenModelViewer Authors
       3             : // 
       4             : // Licensed under the Apache License, Version 2.0 (the "License");
       5             : // you may not use this file except in compliance with the License.
       6             : // You may obtain a copy of the License at
       7             : // 
       8             : //     http://www.apache.org/licenses/LICENSE-2.0
       9             : // 
      10             : // Unless required by applicable law or agreed to in writing, software
      11             : // distributed under the License is distributed on an "AS IS" BASIS,
      12             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13             : // See the License for the specific language governing permissions and
      14             : // limitations under the License.
      15             : //
      16             : 
      17             : #include "openmodelviewer/core/log/log_entry.hpp"
      18             : 
      19             : #include <iomanip>
      20             : #include <sstream>
      21             : 
      22             : namespace openmodelviewer::core::log
      23             : {
      24      237185 :         LogEntry::LogEntry(LogLevel level, std::string message) :
      25      237185 :                 m_level(level), m_message(message)
      26             :         {
      27      233363 :                 m_timePoint = std::chrono::system_clock::now();
      28      236046 :                 m_threadId = std::this_thread::get_id();
      29      235102 :         }
      30             : 
      31          18 :         LogLevel LogEntry::getLevel() const noexcept
      32             :         {
      33          18 :                 return m_level;
      34             :         }
      35             : 
      36           1 :         const std::chrono::system_clock::time_point& LogEntry::getTimePoint() const noexcept
      37             :         {
      38           1 :                 return m_timePoint;
      39             :         }
      40             : 
      41           1 :         const std::thread::id& LogEntry::getThreadId() const noexcept
      42             :         {
      43           1 :                 return m_threadId;
      44             :         }
      45             : 
      46          21 :         const std::string& LogEntry::getMessage() const noexcept
      47             :         {
      48          21 :                 return m_message;
      49             :         }
      50             : 
      51          15 :         std::string LogEntry::getFormattedTimePoint(std::string_view timeFormat) const
      52             :         {
      53             :                 using namespace std::chrono;
      54             : 
      55          15 :                 std::time_t timeT = system_clock::to_time_t(m_timePoint);
      56             :                 std::tm tm;
      57             : 
      58             : #ifdef _WIN32
      59             :                 localtime_s(&tm, &timeT);
      60             : #else
      61          15 :                 localtime_r(&timeT, &tm);
      62             : #endif
      63             : 
      64          15 :                 auto duration = m_timePoint.time_since_epoch();
      65          15 :                 auto millis = duration_cast<milliseconds>(duration) % 1000;
      66             : 
      67          15 :                 std::ostringstream oss;
      68          15 :                 oss << std::put_time(&tm, timeFormat.data());
      69          15 :                 oss << '.' << std::setw(3) << std::setfill('0') << millis.count();
      70             : 
      71          30 :                 return oss.str();
      72          15 :         }
      73             : 
      74          14 :         std::string LogEntry::format(std::string_view logFormat) const
      75             :         {
      76          14 :                 std::string output{logFormat};
      77             : 
      78          14 :                 replaceAll(output, "%time%", getFormattedTimePoint());
      79          14 :                 replaceAll(output, "%level%", logLevelToString(m_level));
      80          14 :                 replaceAll(output, "%thread%", std::to_string(std::hash<std::thread::id>{}(m_threadId)));
      81          14 :                 replaceAll(output, "%message%", m_message);
      82             : 
      83          14 :                 return output;
      84           0 :         }
      85             : 
      86          56 :         void LogEntry::replaceAll(std::string& str, std::string_view from, std::string_view to) noexcept
      87             :         {
      88          56 :                 size_t start_pos = 0;
      89          81 :                 while ((start_pos = str.find(from, start_pos)) != std::string::npos)
      90             :                 {
      91          25 :                         str.replace(start_pos, from.length(), to);
      92          25 :                         start_pos += to.length();
      93             :                 }
      94          56 :         }
      95             : }

Generated by: LCOV version 1.14