*/
#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
{
int ret = 0;
char log_str[4096];
+ char backup_ch;
int len;
- int wlen;
va_list list;
va_start(list, 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;
}
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;
}
{
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) {