Enhacement internal log system 88/138888/3 accepted/tizen/4.0/unified/20170816.013659 accepted/tizen/4.0/unified/20170829.020211 accepted/tizen/unified/20170721.032329 submit/tizen/20170720.005702 submit/tizen_4.0/20170811.094300 submit/tizen_4.0/20170828.100004 submit/tizen_4.0/20170828.110004
authorSunmin Lee <sunm.lee@samsung.com>
Tue, 4 Jul 2017 01:38:05 +0000 (10:38 +0900)
committerKunhoon Baik <knhoon.baik@samsung.com>
Fri, 14 Jul 2017 07:28:44 +0000 (07:28 +0000)
 - Considering log-size management for efficent storage usage

Change-Id: I762288c5ffb51717a3d5ee4b9b2b6d3e1aebf26f
Signed-off-by: Sunmin Lee <sunm.lee@samsung.com>
ss_engine/fota_common.h
ss_engine/fota_log.c
ss_engine/fota_log.h

index f9beb06..91af6fa 100755 (executable)
@@ -41,6 +41,8 @@ typedef unsigned long ul32;
 typedef signed long long s64;
 typedef unsigned long long u64;
 
+#define MAX_FILE_PATH           512
+
 #ifndef TIME_PROFILING
        //#define TIME_PROFILING
 #endif
index 7d7c44e..8421eac 100755 (executable)
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <stdarg.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#define LOG_SIZE_OPT_PATH              "/opt/data/recovery/.ua_log_size"
+#define DEF_MAX_LOG_SIZE               (2*1024*1024)
+#define MAX_FILE_PATH                  512
 
 long curr_offset = 0;
 long next_offset = 0;
-long cut_offset = 0;
-long max_logfile_size = (2*1024*1024);
+long backup_offset = 0;
+long max_logfile_size = DEF_MAX_LOG_SIZE;
+
+/*-----------------------------------------------------------------------------
+  __check_existence
+ ----------------------------------------------------------------------------*/
+static long __check_existence(const char *file_path)
+{
+       struct stat statbuf;
+       char filename[MAX_FILE_PATH];
+
+       if (strncpy(filename, file_path, strlen(file_path) + 1) == NULL) {
+               return 0;
+       }
+       if (stat(filename, &statbuf)) {
+               if (ENOENT == errno) {
+                       return 0;
+               }
+       }
+       return statbuf.st_size;
+}
+
+/*-----------------------------------------------------------------------------
+  __read_from_file
+ ----------------------------------------------------------------------------*/
+static int __read_from_file(const char *path, char *buf, size_t size)
+{
+       int fd;
+       ssize_t count;
+
+       if (!path)
+               return -1;
+
+       if (size == 0) {
+               return 0;
+       }
+
+       fd = open(path, O_RDONLY, 0);
+       if (fd == -1) {
+               return -1;
+       }
+
+       count = read(fd, buf, size);
+       if (count > 0) {
+               count = (count < (ssize_t)size) ? count : ((ssize_t)size - 1);
+               while (count > 0 && buf[count - 1] == '\n')
+                       count--;
+               buf[count] = '\0';
+       } else {
+               buf[0] = '\0';
+       }
+
+       close(fd);
+
+       return (int)count;
+}
+
+/*-----------------------------------------------------------------------------
+  get_opt_logfile_size
+ ----------------------------------------------------------------------------*/
+static int get_opt_logfile_size(void)
+{
+       /*
+           if status file does not exist, status = UP_START_NONE.
+           if status file exist, read status from file.
+       */
+       char buf[256];
+
+       if (__check_existence(LOG_SIZE_OPT_PATH) == 0) {
+               return -1;
+       }
+
+       if (__read_from_file(LOG_SIZE_OPT_PATH, buf, sizeof(buf)) < 0) {
+               return -1;
+       }
+
+       return atoi(buf);
+}
+
+/*-----------------------------------------------------------------------------
+  set_max_logfile_size
+ ----------------------------------------------------------------------------*/
+void set_max_logfile_size(void)
+{
+       int size = get_opt_logfile_size();
+
+       if (size <= 0) {
+               size = DEF_MAX_LOG_SIZE;
+       }
+
+       max_logfile_size = size;
+}
 
 /*-----------------------------------------------------------------------------
   log_printf
@@ -31,8 +132,8 @@ int log_printf(FILE* log_fp, char* format_str, ...)
 {
        int ret = 0;
        char log_str[4096];
+       char backup_ch;
        int len;
-       int wlen;
        va_list list;
 
        va_start(list, format_str);
@@ -43,8 +144,7 @@ int log_printf(FILE* log_fp, char* format_str, ...)
        next_offset = curr_offset + len;
 
        if (next_offset <= max_logfile_size) {
-               wlen = len;
-               if (fwrite(log_str, 1, wlen, log_fp) != wlen) {
+               if (fprintf(log_fp, "%s", log_str) < 0) {
                        ret = -1;
                        goto exit;
                }
@@ -54,15 +154,16 @@ int log_printf(FILE* log_fp, char* format_str, ...)
                        curr_offset = 0;
                }
        } else {
-               cut_offset = max_logfile_size - curr_offset;
-               wlen = cut_offset;
-               if (fwrite(log_str, 1, wlen, log_fp) != wlen) {
+               backup_offset = max_logfile_size - curr_offset;
+               backup_ch = log_str[backup_offset];
+               log_str[backup_offset] = 0x00;
+               if (fprintf(log_fp, "%s", log_str) < 0) {
                        ret = -1;
                        goto exit;
                }
                rewind(log_fp);
-               wlen = next_offset - max_logfile_size;
-               if (fwrite(log_str+cut_offset, 1, wlen, log_fp) != wlen) {
+               log_str[backup_offset] = backup_ch;
+               if (fprintf(log_fp, "%s", log_str+backup_offset) < 0) {
                        ret = -1;
                        goto exit;
                }
@@ -80,6 +181,11 @@ void truncate_log_file(char *log_path, int size_kb)
 {
        FILE *log_fp;
 
+       if (max_logfile_size != DEF_MAX_LOG_SIZE) {
+               /* This means someone wants to see log file, so not truncate. */
+               return;
+       }
+
        if (size_kb == 0) {
                log_fp = fopen(log_path, "w");
                if (log_fp == NULL) {
index e03beed..6c9aca5 100755 (executable)
@@ -29,6 +29,7 @@ extern unsigned int __log_level__;
 extern FILE *__log_out_file__;
 extern int log_printf(FILE* log_fp, char* format_str, ...);
 extern void truncate_log_file(char *log_path, int size_kb);
+extern void set_max_logfile_size(void);
 
 #define LOG_INFO       (1<<8)
 #define LOG_ENGINE     (1<<7)
@@ -37,7 +38,6 @@ extern void truncate_log_file(char *log_path, int size_kb);
 #define LOG_DEBUG      (1<<4)
 #define LOG_FILE       (1<<3)
 #define LOG_FLASH      (1<<2)
-
 #define LOG_SSENGINE  LOG_ENGINE
 
 //#define DEBUG_STDOUT