Audioservice: Add log rotate policy. [2/2]

PD#SWPL-139460

Problem:
Log file would be too large if use file to record log.

Solution:
Add log rotate policy to limit log file size.

Verify:
AV400

Change-Id: Id6742134990696ef5d30a4191383d1127869e619
Signed-off-by: hanliang.xiong <hanliang.xiong@amlogic.com>
diff --git a/aml_log/aml_log.c b/aml_log/aml_log.c
index 38302b6..ce3bb5e 100644
--- a/aml_log/aml_log.c
+++ b/aml_log/aml_log.c
@@ -353,6 +353,53 @@
 static FILE *log_fp = NULL;
 void aml_log_set_output_file(FILE *fp) { log_fp = fp; }
 pthread_t aml_log_pthread_id = 0;
+static const char *log_rotate_file = NULL;
+static const char *log_current_file = NULL;
+unsigned int log_rotate_bytes = 1024*1024;
+
+void aml_log_set_rotate_policy(const char *current_file, const char *rotate_file, unsigned int rotate_bytes)
+{
+  log_fp = fopen(current_file, "a+");
+  if (!log_fp) {
+    printf("open current log file fail, the reason is: \n");
+    perror("open log file fail reason");
+    return;
+  }
+  log_current_file = current_file;
+  log_rotate_file = rotate_file;
+  log_rotate_bytes = rotate_bytes;
+}
+
+/**
+ * @brief Try to rotate file.
+ *
+ * @param len new added length in bytes
+ */
+void aml_log_do_rotate(int len)
+{
+  static unsigned int total_len = 0;
+  total_len += len;
+  if (total_len >= log_rotate_bytes) {
+    if ((log_fp == NULL) || \
+      (log_rotate_file == NULL) || \
+      (log_current_file == NULL)
+    ) {
+      printf("log_fp:%p, log_rotate_file:%s,log_current_file:%s should not be null if you want to use rotate\n",
+        log_fp, log_rotate_file, log_current_file);
+      return;
+    }
+    // now current file is big enough
+    // try to rotate
+    char cmd[256] = {0};
+    snprintf(cmd,255, "cp -f %s %s", log_current_file, log_rotate_file);
+    system(cmd);
+    // empty current file
+    ftruncate(fileno(log_fp), 0);
+    fseek(log_fp, 0, SEEK_END);
+    total_len = 0;// reset this counter
+    sync();
+  }
+}
 
 void aml_log_msg(struct AmlLogCat *cat, int level, const char *file, const char *function,
                      int lineno, const char *fmt, ...) {
@@ -378,7 +425,10 @@
     va_start(ap, fmt);
     vsnprintf(&buf[len], sizeof(buf) - len, fmt, ap);
     va_end(ap);
-    fprintf(log_fp ?: stdout, "%s", buf);
+    len = fprintf(log_fp ?: stdout, "%s", buf);
+    if (log_fp) {
+      aml_log_do_rotate(len);
+    }
 }
 
 static FILE *trace_json_fp = NULL;
diff --git a/aml_log/aml_log.h b/aml_log/aml_log.h
index 4bb8809..c67e355 100644
--- a/aml_log/aml_log.h
+++ b/aml_log/aml_log.h
@@ -204,6 +204,14 @@
 void aml_trace_set_from_string(const char *str);
 
 void aml_log_set_output_file(FILE *fp);
+/**
+ * @brief Rotate the log file to save space.
+ *
+ * @param current_file current log file path. Like /tmp/app.log
+ * @param rotate_file the file to be saved. Like /tmp/app.log.1
+ * @param rotate_bytes Rotate the current_file when its size > rotate_bytes, like 1024*1024
+ */
+void aml_log_set_rotate_policy(const char *current_file, const char *rotate_file, unsigned int rotate_bytes);
 
 /**
  * @brief write json format trace information to stream fp