mirror of
https://github.com/mudita/MuditaOS.git
synced 2026-01-01 02:19:00 -05:00
Fixes and optimizations in logger: * fixed possible buffer overflow when logging logs over line buffer size; * reduced max log line length to 2048; * moved pubsetbuf before file opening; * log file stream buffer created once in logger ctor; * updatet UTs; * additional minor cleanup.
42 lines
1.0 KiB
C++
42 lines
1.0 KiB
C++
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
|
|
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
|
|
|
|
#include "LoggerBuffer.hpp"
|
|
|
|
std::optional<std::string> LoggerBuffer::get()
|
|
{
|
|
auto logMsg = StringCircularBuffer::get();
|
|
if (!logMsg.has_value()) {
|
|
return std::nullopt;
|
|
}
|
|
if (numOfLostBytes > 0) {
|
|
logMsg = std::to_string(numOfLostBytes) + " " + lostBytesMessage + "\n" + logMsg.value();
|
|
numOfLostBytes = 0;
|
|
}
|
|
return logMsg;
|
|
}
|
|
|
|
void LoggerBuffer::put(const std::string &logMsg)
|
|
{
|
|
updateNumOfLostBytes();
|
|
StringCircularBuffer::put(logMsg);
|
|
}
|
|
|
|
void LoggerBuffer::put(std::string &&logMsg)
|
|
{
|
|
updateNumOfLostBytes();
|
|
StringCircularBuffer::put(std::move(logMsg));
|
|
}
|
|
|
|
void LoggerBuffer::updateNumOfLostBytes()
|
|
{
|
|
if (!StringCircularBuffer::isFull()) {
|
|
return;
|
|
}
|
|
|
|
const auto lostMsg = StringCircularBuffer::get();
|
|
if (lostMsg.has_value()) {
|
|
numOfLostBytes += lostMsg.value().length();
|
|
}
|
|
}
|