From 16f7977131d07ce6a9cd27c4a54722cbb1bf1541 Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Tue, 4 Jul 2017 10:38:05 +0900 Subject: [PATCH] Enhacement internal log system - Considering log-size management for efficent storage usage Change-Id: I762288c5ffb51717a3d5ee4b9b2b6d3e1aebf26f Signed-off-by: Sunmin Lee --- ss_engine/fota_common.h | 2 + ss_engine/fota_log.c | 126 ++++++++++++++++++++++++++++++++++++++++++++---- ss_engine/fota_log.h | 2 +- 3 files changed, 119 insertions(+), 11 deletions(-) diff --git a/ss_engine/fota_common.h b/ss_engine/fota_common.h index f9beb06..91af6fa 100755 --- a/ss_engine/fota_common.h +++ b/ss_engine/fota_common.h @@ -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 diff --git a/ss_engine/fota_log.c b/ss_engine/fota_log.c index 7d7c44e..8421eac 100755 --- a/ss_engine/fota_log.c +++ b/ss_engine/fota_log.c @@ -17,12 +17,113 @@ */ #include +#include #include +#include +#include +#include +#include +#include +#include + +#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) { diff --git a/ss_engine/fota_log.h b/ss_engine/fota_log.h index e03beed..6c9aca5 100755 --- a/ss_engine/fota_log.h +++ b/ss_engine/fota_log.h @@ -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 -- 2.7.4