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_level.hpp" 18 : #include "openmodelviewer/core/log/log_entry.hpp" 19 : 20 : #include <iomanip> 21 : #include <sstream> 22 : 23 : namespace openmodelviewer::core::log 24 : { 25 236817 : LogEntry::LogEntry(LogLevel level, std::string message) : 26 236817 : m_level(level), m_message(message) 27 : { 28 233021 : m_timePoint = std::chrono::system_clock::now(); 29 235883 : m_threadId = std::this_thread::get_id(); 30 235685 : } 31 : 32 18 : LogLevel LogEntry::getLevel() const noexcept 33 : { 34 18 : return m_level; 35 : } 36 : 37 1 : const std::chrono::system_clock::time_point& LogEntry::getTimePoint() const noexcept 38 : { 39 1 : return m_timePoint; 40 : } 41 : 42 1 : const std::thread::id& LogEntry::getThreadId() const noexcept 43 : { 44 1 : return m_threadId; 45 : } 46 : 47 21 : const std::string& LogEntry::getMessage() const noexcept 48 : { 49 21 : return m_message; 50 : } 51 : 52 15 : std::string LogEntry::getFormattedTimePoint(std::string_view timeFormat) const 53 : { 54 : using namespace std::chrono; 55 : 56 15 : std::time_t timeT = system_clock::to_time_t(m_timePoint); 57 : std::tm tm; 58 : 59 : #ifdef _WIN32 60 : localtime_s(&tm, &timeT); 61 : #else 62 15 : localtime_r(&timeT, &tm); 63 : #endif 64 : 65 15 : auto duration = m_timePoint.time_since_epoch(); 66 15 : auto millis = duration_cast<milliseconds>(duration) % 1000; 67 : 68 15 : std::ostringstream oss; 69 15 : oss << std::put_time(&tm, timeFormat.data()); 70 15 : oss << '.' << std::setw(3) << std::setfill('0') << millis.count(); 71 : 72 30 : return oss.str(); 73 15 : } 74 : 75 14 : std::string LogEntry::format(std::string_view logFormat) const 76 : { 77 14 : std::string output{logFormat}; 78 : 79 14 : replaceAll(output, "%time%", getFormattedTimePoint()); 80 14 : replaceAll(output, "%level%", to_string(m_level)); 81 14 : replaceAll(output, "%thread%", std::to_string(std::hash<std::thread::id>{}(m_threadId))); 82 14 : replaceAll(output, "%message%", m_message); 83 : 84 14 : return output; 85 0 : } 86 : 87 56 : void LogEntry::replaceAll(std::string& str, std::string_view from, std::string_view to) noexcept 88 : { 89 56 : size_t start_pos = 0; 90 81 : while ((start_pos = str.find(from, start_pos)) != std::string::npos) 91 : { 92 25 : str.replace(start_pos, from.length(), to); 93 25 : start_pos += to.length(); 94 : } 95 56 : } 96 : }