Line data Source code
1 : /*! 2 : * \file esys/repo/tui/logbuffer.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2026 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/tui/logbuffer.h" 20 : 21 : namespace esys::repo::tui 22 : { 23 : 24 0 : LogBuffer::LogBuffer(std::size_t capacity) 25 0 : : m_capacity(capacity == 0 ? 1 : capacity) 26 : { 27 0 : } 28 : 29 0 : void LogBuffer::push(std::string line) 30 : { 31 0 : std::lock_guard<std::mutex> lock(m_mutex); 32 0 : while (m_lines.size() >= m_capacity) m_lines.pop_front(); 33 0 : m_lines.push_back(std::move(line)); 34 0 : } 35 : 36 0 : std::vector<std::string> LogBuffer::snapshot() const 37 : { 38 0 : std::lock_guard<std::mutex> lock(m_mutex); 39 0 : return std::vector<std::string>(m_lines.begin(), m_lines.end()); 40 0 : } 41 : 42 0 : std::size_t LogBuffer::size() const 43 : { 44 0 : std::lock_guard<std::mutex> lock(m_mutex); 45 0 : return m_lines.size(); 46 0 : } 47 : 48 : } // namespace esys::repo::tui