From bb1f92fa31c4db86642b461c2ad2ec7dd1c361fe Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Thu, 12 Feb 2026 17:53:05 +0300 Subject: [PATCH] - minimize logging --- src/engine/agent/Agent_Impl.cpp | 8 +- src/engine/media/MT_AudioReceiver.cpp | 2 +- src/engine/media/MT_NativeRtpSender.cpp | 2 +- src/libs/ice/ICELog.cpp | 150 +++++++++++++++++++++++- src/libs/ice/ICELog.h | 5 +- 5 files changed, 161 insertions(+), 6 deletions(-) diff --git a/src/engine/agent/Agent_Impl.cpp b/src/engine/agent/Agent_Impl.cpp index 7cc26a95..411058d7 100644 --- a/src/engine/agent/Agent_Impl.cpp +++ b/src/engine/agent/Agent_Impl.cpp @@ -76,7 +76,7 @@ std::string AgentImpl::command(const std::string& command) return ""; std::string cmd = d["command"].asString(); - if (cmd != "wait_for_event") + if (cmd != "wait_for_event" && cmd != "agent_add_root_cert") { ICELogInfo(<< command); } @@ -98,8 +98,12 @@ std::string AgentImpl::command(const std::string& command) if (cmd == "account_setuserinfo") processSetUserInfoToAccount(d, answer); else - if (cmd == "session_create") + if (cmd == "session_create") { + // For Bugsnag test + // int* v = nullptr; + // *v = 0; processCreateSession(d, answer); + } else if (cmd == "session_start") processStartSession(d, answer); diff --git a/src/engine/media/MT_AudioReceiver.cpp b/src/engine/media/MT_AudioReceiver.cpp index e7bf44e4..48f82157 100644 --- a/src/engine/media/MT_AudioReceiver.cpp +++ b/src/engine/media/MT_AudioReceiver.cpp @@ -423,7 +423,7 @@ bool AudioReceiver::add(const std::shared_ptr& p, Codec** de payloadLength = p->GetPayloadLength(), ptype = p->GetPayloadType(); - ICELogInfo(<< "Adding packet No " << p->GetSequenceNumber()); + ICELogMedia(<< "Adding packet No " << p->GetSequenceNumber()); // Increase codec counter mStat.mCodecCount[ptype]++; diff --git a/src/engine/media/MT_NativeRtpSender.cpp b/src/engine/media/MT_NativeRtpSender.cpp index 0b5db0bc..c2f05f58 100644 --- a/src/engine/media/MT_NativeRtpSender.cpp +++ b/src/engine/media/MT_NativeRtpSender.cpp @@ -43,7 +43,7 @@ bool NativeRtpSender::SendRTP(const void *data, size_t len) } } - ICELogInfo(<< "Sending " << sendLength <<" bytes to " << mTarget.mRtp.toBriefStdString()); + ICELogMedia(<< "Sending " << sendLength <<" bytes to " << mTarget.mRtp.toBriefStdString()); mSocket.mRtp->sendDatagram(mTarget.mRtp, mSendBuffer, sendLength); mStat.mSentRtp++; mStat.mSent += len; diff --git a/src/libs/ice/ICELog.cpp b/src/libs/ice/ICELog.cpp index 56522303..9c72e95c 100644 --- a/src/libs/ice/ICELog.cpp +++ b/src/libs/ice/ICELog.cpp @@ -18,6 +18,106 @@ using namespace ice; +#include +#include +#include +#include + +#define KEEP_SIZE (128 * 1024) // 128 KB = 131,072 bytes + +/** + * Truncates a file to its last 128 KB of content. + * + * @param filename Path to the file to truncate + * @return 0 on success, -1 on error (check errno for details) + * + * Behavior: + * - Files ≤128 KB remain unchanged + * - Files >128 KB are replaced with their last 128 KB + * - Uses binary mode to avoid newline translation issues + * - Preserves file existence but NOT original inode/permissions (reopens in write mode) + */ +int truncate_file_to_last_128k(const char *filename) { + if (!filename || *filename == '\0') { + errno = EINVAL; + return -1; + } + + // Step 1: Open file for reading (binary mode for exact byte handling) + FILE *fp = fopen(filename, "rb"); + if (!fp) { + return -1; // errno set by fopen + } + + // Step 2: Get file size + if (fseek(fp, 0, SEEK_END) != 0) { + int err = errno; + fclose(fp); + errno = err; + return -1; + } + + long size = ftell(fp); + if (size < 0) { + int err = errno; + fclose(fp); + errno = err; + return -1; + } + + // Step 3: Nothing to do if file is small enough + if (size <= KEEP_SIZE) { + fclose(fp); + return 0; + } + + // Step 4: Position at start of region to keep (last 128 KB) + long start_pos = size - KEEP_SIZE; + if (fseek(fp, start_pos, SEEK_SET) != 0) { + int err = errno; + fclose(fp); + errno = err; + return -1; + } + + // Step 5: Read the last 128 KB into buffer + char *buffer = (char*)malloc(KEEP_SIZE); + if (!buffer) { + fclose(fp); + errno = ENOMEM; + return -1; + } + + size_t read_bytes = fread(buffer, 1, KEEP_SIZE, fp); + int read_err = ferror(fp); + fclose(fp); + + if (read_bytes != KEEP_SIZE || read_err) { + free(buffer); + errno = read_err ? ferror(fp) : EIO; + return -1; + } + + // Step 6: Reopen file in write mode (truncates existing content) + fp = fopen(filename, "wb"); + if (!fp) { + free(buffer); + return -1; // errno set by fopen + } + + // Step 7: Write preserved content back to file + size_t written = fwrite(buffer, 1, KEEP_SIZE, fp); + int write_err = ferror(fp); + fclose(fp); + free(buffer); + + if (written != KEEP_SIZE || write_err) { + errno = write_err ? ferror(fp) : EIO; + return -1; + } + + return 0; +} LogLevel LogLevelHelper::parse(const std::string& t) { @@ -103,6 +203,9 @@ Logger::useFile(const char* filepath) if (mLogPath.empty()) return; + // Keep only last 128Kb + truncate_file_to_last_128k(filepath); + FILE* f = fopen(filepath, "at"); if (f) { @@ -118,6 +221,10 @@ Logger::useFile(const char* filepath) } } +const std::string& Logger::getLogPath() const { + return mLogPath; +} + void Logger::useNull() { @@ -151,10 +258,51 @@ void Logger::openFile() if (mLogPath.empty()) return; - remove(mLogPath.c_str()); + // remove(mLogPath.c_str()); useFile(mLogPath.c_str()); } +std::string Logger::getLastContent(size_t bytes) { + LogGuard l(mGuard); + std::string r; + + bool fileOpenNeeded = false; + if (mFile) + { + fclose(mFile); mFile = nullptr; + fileOpenNeeded = true; + } + + mFile = fopen(mLogPath.c_str(), "rt"); + + if (mFile) { + fseek(mFile, 0, SEEK_END); + auto size = ftell(mFile); + + if (size < bytes) + fseek(mFile, 0, SEEK_SET); + else + fseek(mFile, size - (long)bytes, SEEK_SET); + + auto contentPos = ftell(mFile); + size_t contentLength = size - contentPos; + if (bytes < contentLength) + contentLength = bytes; + + r.resize(contentLength, ' '); + auto readCount = fread(r.data(), 1, contentLength, mFile); + if (readCount >= 0) + r.resize(readCount); + fclose(mFile); mFile = nullptr; + } + + if (fileOpenNeeded) + { + mFile = fopen(mLogPath.c_str(), "at"); + } + + return r; +} void Logger::useDelegate(LogHandler* delegate_) { diff --git a/src/libs/ice/ICELog.h b/src/libs/ice/ICELog.h index bd419a47..e2b2dff3 100644 --- a/src/libs/ice/ICELog.h +++ b/src/libs/ice/ICELog.h @@ -110,7 +110,10 @@ public: void useNull(); void closeFile(); void openFile(); - + + std::string getLastContent(size_t bytes); + const std::string& getLogPath() const; + LogGuard& mutex(); LogLevel level(); void setLevel(LogLevel level);