avsync-lib: CF1 support syslog and logcat [1/1]

PD#TV-61176

Problem:
Some platforms is not stdout/stderr friendly. Any log can cause
scheuding issue for AVsync.

Solution:
Add compile options for syslog and logcat support

Verify:
T5D + Roxton

Change-Id: I723b4fd0560e4d71adee64fe7961580506bf694a
Signed-off-by: Song Zhao <song.zhao@amlogic.com>
diff --git a/Makefile b/Makefile
index 920849e..d32ecf5 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 
 #export CC BUILD_DIR STAGING_DIR TARGET_DIR
 all:
-	-$(MAKE) -C src all
+	-$(MAKE) -C LOG=$(LOG) src all
 install:
 	-$(MAKE) -C src install
 clean:
diff --git a/src/Makefile b/src/Makefile
index 5074c79..596f582 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -14,18 +14,29 @@
 all: $(TARGET)
 endif
 
+CC_FLAG = -Wall
 LD_FLAG = -lm -lpthread
 
+ifeq ($(LOG), LOGCAT)
+$(info use logcat)
+LD_FALG += -llog
+CC_FLAG += -DENABLE_LOGCAT
+endif
+ifeq ($(LOG), SYSLOG)
+$(info use syslog)
+CC_FLAG += -DENABLE_SYSLOG
+endif
+
 $(TARGET): $(OBJ)
-	$(CC) $(TARGET_CFLAGS)  -g -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib $(LD_FLAG) $(OBJ) -shared -fPIC -o $@
+	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib $(LD_FLAG) $(OBJ) -shared -fPIC -o $@
 
 $(TEST): $(TARGET) test.c
 	cp $(TARGET) $(STAGING_DIR)/usr/lib/
-	$(CC) $(TARGET_CFLAGS)  -g -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lamlavsync -lpthread test.c -o $@
+	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lamlavsync test.c -o $@
 
 $(PCR_TEST): $(TARGET) pcr_test.c
 	cp $(TARGET) $(STAGING_DIR)/usr/lib/
-	$(CC) $(TARGET_CFLAGS)  -g -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lpthread -lamlavsync pcr_test.c -o $@
+	$(CC) $(TARGET_CFLAGS) $(CC_FLAG) -D_FILE_OFFSET_BITS=64 -Wall -I$(STAGING_DIR)/usr/include/ -L$(STAGING_DIR)/usr/lib -lpthread -lamlavsync pcr_test.c -o $@
 
 .PHONY: clean
 
diff --git a/src/avsync.c b/src/avsync.c
index a195c5c..5f210a0 100644
--- a/src/avsync.c
+++ b/src/avsync.c
@@ -485,7 +485,8 @@
     if (!avsync || !avsync->fd)
         return -1;
 
-    log_info("[%d]policy %u --> %u, timeout %d --> %d", avsync->start_policy, st_policy->policy, avsync->timeout, st_policy->timeout);
+    log_info("[%d]policy %u --> %u, timeout %d --> %d", avsync->session_id,
+        avsync->start_policy, st_policy->policy, avsync->timeout, st_policy->timeout);
     if (LIVE_MODE(avsync->mode) &&
         st_policy->policy != AV_SYNC_START_ASAP) {
         log_error("policy %d not supported in live mode", st_policy->policy);
diff --git a/src/log.c b/src/log.c
index 155d6ce..892dda5 100644
--- a/src/log.c
+++ b/src/log.c
@@ -26,6 +26,12 @@
 #include <string.h>
 #include <time.h>
 #include <sys/time.h>
+#ifdef ENABLE_SYSLOG
+#include <syslog.h>
+#elif ENABLE_LOGCAT
+#define LOG_TAG "avs"
+#include <cutils/log.h>
+#endif
 
 #include "aml_avsync_log.h"
 
@@ -103,6 +109,76 @@
    clock_gettime( CLOCK_MONOTONIC_RAW, &tm );
    second = tm.tv_sec;
    usec = tm.tv_nsec/1000LL;
+
+#if defined(ENABLE_SYSLOG)
+  {
+    va_list args;
+    int l;
+    char content[512];
+    switch (level) {
+      case AVS_LOG_FATAL:
+        l = LOG_CRIT;
+        break;
+      case AVS_LOG_ERROR:
+        l = LOG_ERR;
+        break;
+      case AVS_LOG_WARN:
+        l = LOG_WARNING;
+        break;
+      case AVS_LOG_INFO:
+        l = LOG_NOTICE;
+        break;
+      case AVS_LOG_DEBUG:
+        l = LOG_INFO;
+        break;
+      case AVS_LOG_TRACE:
+      default:
+        l = LOG_DEBUG;
+        break;
+    }
+    va_start(args, fmt);
+    vsnprintf(content, 512, fmt, args);
+    va_end(args);
+    syslog(l, "[%ld.%06ld]: %-5s %s:%d: %s", second, usec, level_names[level], file, line, content);
+
+    unlock();
+    return;
+  }
+#elif defined(ENABLE_LOGCAT)
+  {
+    va_list args;
+    int l;
+    char content[512];
+    switch (level) {
+      case AVS_LOG_FATAL:
+        l = ANDROID_LOG_ERROR;
+        break;
+      case AVS_LOG_ERROR:
+        l = ANDROID_LOG_ERROR;
+        break;
+      case AVS_LOG_WARN:
+        l = ANDROID_LOG_WARN;
+        break;
+      case AVS_LOG_INFO:
+        l = ANDROID_LOG_INFO;
+        break;
+      case AVS_LOG_DEBUG:
+        l = ANDROID_LOG_DEBUG;
+        break;
+      case AVS_LOG_TRACE:
+      default:
+        l = ANDROID_LOG_VERBOSE;
+        break;
+    }
+    va_start(args, fmt);
+    vsnprintf(content, 512, fmt, args);
+    va_end(args);
+    LOG_PRI(l, LOG_TAG, "[%ld.%06ld]: %-5s %s:%d: %s", second, usec, level_names[level], file, line, content);
+
+    unlock();
+    return;
+  }
+#endif
   /* Log to stderr */
   if (!L.quiet) {
     va_list args;