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/sinks/buffer_sink.hpp" 18 : 19 : namespace openmodelviewer::core::log::sinks 20 : { 21 9 : BufferSink::BufferSink(size_t bufferSize) 22 9 : : m_maxBufferSize(bufferSize) 23 : { 24 9 : } 25 : 26 40026 : void BufferSink::log(const LogEntry& entry) 27 : { 28 40026 : if (m_logBuffer.size() >= m_maxBufferSize) 29 : { 30 1 : m_logBuffer.pop_front(); 31 : } 32 : 33 40026 : m_logBuffer.push_back(entry); 34 40026 : } 35 : 36 1 : void BufferSink::flush() 37 : { 38 1 : m_logBuffer.clear(); 39 1 : } 40 : 41 1 : void BufferSink::resize(size_t newMaxBufferSize) 42 : { 43 1 : m_maxBufferSize = newMaxBufferSize; 44 : 45 3 : while (m_logBuffer.size() > m_maxBufferSize) 46 : { 47 2 : m_logBuffer.pop_front(); 48 : } 49 1 : } 50 : 51 2 : bool BufferSink::empty() const noexcept 52 : { 53 2 : return m_logBuffer.empty(); 54 : } 55 : 56 1 : size_t BufferSink::size() const noexcept 57 : { 58 1 : return m_logBuffer.size(); 59 : } 60 : 61 5 : const std::vector<LogEntry> BufferSink::getEntries() const noexcept 62 : { 63 5 : std::vector<LogEntry> entries; 64 5 : entries.reserve(m_logBuffer.size()); 65 : 66 40015 : for (const LogEntry& entry : m_logBuffer) 67 : { 68 40010 : entries.push_back(entry); 69 : } 70 : 71 5 : return entries; 72 : } 73 : 74 1 : const std::vector<LogEntry> BufferSink::getEntries(LogLevel level) const noexcept 75 : { 76 1 : std::vector<LogEntry> filtered; 77 : 78 5 : for (const LogEntry& entry : m_logBuffer) 79 : { 80 4 : if (entry.getLevel() == level) 81 : { 82 2 : filtered.push_back(entry); 83 : } 84 : } 85 : 86 1 : return filtered; 87 : } 88 : 89 1 : const std::vector<LogEntry> BufferSink::getLastEntries(size_t count) const 90 : { 91 1 : std::vector<LogEntry> result; 92 : 93 1 : if (m_logBuffer.empty()) 94 : { 95 0 : return result; 96 : } 97 : 98 1 : const size_t start = m_logBuffer.size() > count ? m_logBuffer.size() - count : 0; 99 : 100 1 : result.reserve(m_logBuffer.size() - start); 101 : 102 4 : for (size_t i = start; i < m_logBuffer.size(); ++i) 103 : { 104 3 : result.push_back(m_logBuffer[i]); 105 : } 106 : 107 1 : return result; 108 0 : } 109 : 110 1 : const std::vector<std::string> BufferSink::getFormattedEntries(std::string_view logFormat) const noexcept 111 : { 112 1 : std::vector<std::string> formatted; 113 1 : formatted.reserve(m_logBuffer.size()); 114 : 115 2 : for (LogEntry entry : m_logBuffer) 116 : { 117 1 : formatted.push_back(entry.format(logFormat)); 118 1 : } 119 : 120 1 : return formatted; 121 : } 122 : }