aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/log_buffer.cpp
diff options
context:
space:
mode:
authorandrew-rykov <arykov@ydb.tech>2022-09-13 12:49:05 +0300
committerandrew-rykov <arykov@ydb.tech>2022-09-13 12:49:05 +0300
commitcee4a99ba93f21e3d30b5f1d58c84ab6ca41981b (patch)
treed75cbc3a1ad20a211c550d8f29634eff47f5e48d /library/cpp/actors/core/log_buffer.cpp
parent8c1af103661148f5377e712cb2ac5623672522a1 (diff)
downloadydb-cee4a99ba93f21e3d30b5f1d58c84ab6ca41981b.tar.gz
add-log-buffer 2
add log buffer changed names added move changed details fixed released buffer procedure returned condition IgnoredCount > 0 default bufferSizeLimitBytes = 0 returned passedCount declaration returned Y_VERIFY removed passedcount changed buffer reducing no new line at the end of file added srcs in ya.make add log buffer
Diffstat (limited to 'library/cpp/actors/core/log_buffer.cpp')
-rw-r--r--library/cpp/actors/core/log_buffer.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/library/cpp/actors/core/log_buffer.cpp b/library/cpp/actors/core/log_buffer.cpp
new file mode 100644
index 00000000000..4a05f9a8c25
--- /dev/null
+++ b/library/cpp/actors/core/log_buffer.cpp
@@ -0,0 +1,98 @@
+#include "log_buffer.h"
+
+#include <util/system/yassert.h>
+#include <algorithm>
+
+using namespace NActors::NLog;
+
+namespace NActors {
+TLogBufferMessage::TLogBufferMessage(NLog::TEvLog::TPtr& ev)
+ : Formatted(ev->Get()->Line)
+ , Time(ev->Get()->Stamp)
+ , Component(ev->Get()->Component)
+ , Priority(ev->Get()->Level.ToPrio())
+{}
+
+TLogBuffer::TLogBuffer(ILoggerMetrics *metrics)
+ : Metrics(metrics)
+{}
+
+bool TLogBuffer::TryAddMessage(TLogBufferMessage message) {
+ Buffer.push_back(std::move(message));
+ BufferSize += sizeof(message);
+ PrioStats[message.Priority]++;
+
+ return true;
+}
+
+TLogBufferMessage TLogBuffer::GetMessage() {
+ auto message = Buffer.front();
+
+ ui64 messageSize = sizeof(message);
+ BufferSize -= messageSize;
+ Buffer.pop_front();
+ PrioStats[message.Priority]--;
+
+ return message;
+}
+
+bool TLogBuffer::IsEmpty() const {
+ return Buffer.empty();
+}
+
+size_t TLogBuffer::GetLogsNumber() const {
+ return Buffer.size();
+}
+
+ui64 TLogBuffer::GetSizeBytes() const {
+ return BufferSize;
+}
+
+void TLogBuffer::FilterByLogPriority(NLog::EPrio prio) {
+ bool isFirstRemoving = true;
+ auto it = Buffer.begin();
+ while (it != Buffer.end())
+ {
+ if (it->Priority >= prio) {
+ ui64 messageSize = sizeof(*it);
+ BufferSize -= messageSize;
+ Metrics->IncIgnoredMsgs();
+ PrioStats[it->Priority]--;
+
+ if (isFirstRemoving && prio > NLog::EPrio::Error) {
+ it->Priority = NLog::EPrio::Error;
+ it->Formatted = Sprintf("Ignored log records due to log buffer overflow! IgnoredCount# %" PRIu32 " ", PrioStats[prio]);
+
+ PrioStats[NLog::EPrio::Error]++;
+ BufferSize += sizeof(*it);
+ it++;
+ isFirstRemoving = false;
+ }
+ else {
+ it = Buffer.erase(it);
+ }
+ }
+ else {
+ it++;
+ }
+ }
+}
+
+bool inline TLogBuffer::CheckMessagesNumberEnoughForClearing(ui32 number) {
+ return number * 10 > Buffer.size();
+}
+
+bool TLogBuffer::TryReduceLogsNumber() {
+ ui32 removeLogsNumber = 0;
+ for (ui16 p = ui16(NLog::EPrio::Trace); p > ui16(NLog::EPrio::Alert); p--)
+ {
+ NLog::EPrio prio = static_cast<NLog::EPrio>(p);
+ removeLogsNumber += PrioStats[prio];
+ if (CheckMessagesNumberEnoughForClearing(removeLogsNumber)) {
+ FilterByLogPriority(prio);
+ return true;
+ }
+ }
+ return false;
+}
+}