From d144bc3231092c3d6acf5cb93b1fce128512c85c Mon Sep 17 00:00:00 2001 From: InHong Han Date: Tue, 2 Jun 2020 11:20:58 +0900 Subject: [PATCH] Limit the maximum size of log file Change-Id: Ic1f3f34837cf57fbb74c6f90269a44c2d45f44e4 --- receiver/inc/config.h | 3 +++ receiver/inc/receiver_preference.h | 1 + receiver/src/sticker_log.cpp | 45 +++++++++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/receiver/inc/config.h b/receiver/inc/config.h index bafee30..f1ca4b9 100644 --- a/receiver/inc/config.h +++ b/receiver/inc/config.h @@ -33,4 +33,7 @@ #define STICKER_DIRECTORY "/opt/usr/share/sticker-data" +#define MAX_LOG_SIZE 1024*1024 +#define MAX_LOG_COUNT 7 + #endif /* __CONFIG_H__ */ diff --git a/receiver/inc/receiver_preference.h b/receiver/inc/receiver_preference.h index 140b171..02fc7e3 100644 --- a/receiver/inc/receiver_preference.h +++ b/receiver/inc/receiver_preference.h @@ -18,5 +18,6 @@ #define __RECEIVER_PREFERENCE_H__ #define LAST_SYNC_TIME "LastSyncTime" +#define LAST_LOG_FILE_INDEX "LastLogFileIndex" #endif /* __RECEIVER_PREFERENCE_H__ */ diff --git a/receiver/src/sticker_log.cpp b/receiver/src/sticker_log.cpp index 8954a28..1faffd5 100644 --- a/receiver/src/sticker_log.cpp +++ b/receiver/src/sticker_log.cpp @@ -9,8 +9,11 @@ #include #include #include +#include #include "log.h" +#include "config.h" +#include "receiver_preference.h" using namespace std; @@ -44,7 +47,47 @@ void sticker_save_log(const char *fmt, ...) if (data_path) free(data_path); - snprintf(strLogFile, sizeof(strLogFile), "%s/sticker.log", log_path); + int idx = 0; + if (preference_get_int(LAST_LOG_FILE_INDEX, &idx) != PREFERENCE_ERROR_NONE) { + idx = 1; + snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx); + + if (access(strLogFile, F_OK) == 0) { + if (unlink(strLogFile) == -1) + LOGE("Failed to remove log file"); + } + + if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE) + LOGW("Failed to set last file index"); + } else { + char tmpLogFile[PATH_MAX]; + snprintf(tmpLogFile, sizeof(tmpLogFile), "%s/sticker_%d.log", log_path, idx); + + ifstream log_file(tmpLogFile, ifstream::binary); + if (!log_file || !log_file.is_open()) { + snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile); + } else { + log_file.seekg(0, log_file.end); + int size = log_file.tellg(); + + if (size >= MAX_LOG_SIZE) { + if (idx + 1 > MAX_LOG_COUNT) + idx = 1; + else + idx += 1; + + if (preference_set_int(LAST_LOG_FILE_INDEX, idx) != PREFERENCE_ERROR_NONE) + LOGW("Failed to set last file index"); + + snprintf(strLogFile, sizeof(strLogFile), "%s/sticker_%d.log", log_path, idx); + if (access(strLogFile, F_OK) == 0) { + if (unlink(strLogFile) == -1) + LOGE("Failed to remove log file"); + } + } else + snprintf(strLogFile, sizeof(strLogFile), "%s", tmpLogFile); + } + } std::ofstream sticker_log_file (strLogFile, std::ios::app); sticker_log_file << full_buf << std::endl; -- 2.7.4