Limit the maximum size of log file
[platform/core/uifw/capi-ui-sticker.git] / receiver / src / sticker_log.cpp
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;