Limit the maximum size of log file 43/235043/3
authorInHong Han <inhong1.han@samsung.com>
Tue, 2 Jun 2020 02:20:58 +0000 (11:20 +0900)
committerInHong Han <inhong1.han@samsung.com>
Tue, 2 Jun 2020 02:57:39 +0000 (11:57 +0900)
Change-Id: Ic1f3f34837cf57fbb74c6f90269a44c2d45f44e4

receiver/inc/config.h
receiver/inc/receiver_preference.h
receiver/src/sticker_log.cpp

index bafee30..f1ca4b9 100644 (file)
@@ -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__ */
index 140b171..02fc7e3 100644 (file)
@@ -18,5 +18,6 @@
 #define __RECEIVER_PREFERENCE_H__
 
 #define LAST_SYNC_TIME "LastSyncTime"
+#define LAST_LOG_FILE_INDEX "LastLogFileIndex"
 
 #endif /* __RECEIVER_PREFERENCE_H__ */
index 8954a28..1faffd5 100644 (file)
@@ -9,8 +9,11 @@
 #include <linux/limits.h>
 #include <time.h>
 #include <app_common.h>
+#include <app_preference.h>
 
 #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;