diff options
author | Roland Reichwein <mail@reichwein.it> | 2025-01-04 11:28:20 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2025-01-04 11:28:20 +0100 |
commit | b2c35cdf69a9084806ac7930cf4475980d596cf6 (patch) | |
tree | b74b8f2ee2c66c59f7385407cfc34c2a0a16961f /log.cpp | |
parent | aaafcea7e26791acbf5b9612e3fb396edcdfcc8f (diff) |
Separate out implementation from headers
Diffstat (limited to 'log.cpp')
-rw-r--r-- | log.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -2,3 +2,56 @@ log_stream log_cout; +log_stream::log_stream(): m_active(false), m_buffer(), m_log_lines() {} + +void log_stream::log_lines(int n) { + m_log_lines = n; +} + +std::string log_stream::get_log() { + return m_buffer.str(); +} + +// log to buffer +void log_stream::activate() +{ + m_active = true; +} + +// log to plain console +void log_stream::deactivate() +{ + m_active = false; +} + +log_stream& log_stream::operator<<( + std::basic_ostream<char>& (*func) + (std::basic_ostream<char>&) ) { + if (m_active) { + m_buffer << *func; + trim_buffer(); + } + else + { + std::cout << *func; + } + return *this; +} + +void log_stream::trim_buffer() +{ + std::string s = m_buffer.str(); + size_t pos = s.npos; + for (int i = 0; i <= m_log_lines; ++i) { + pos = s.rfind("\n", pos); + if (pos == s.npos) { + // too few lines + return; + } + if (pos > 0) { + --pos; + } + } + + m_buffer.str(s.substr((pos <= (s.size() - 2)) ? pos + 2 : pos)); +} |