From fa0a7fa5c7d88f34b12fe8a70f92ed7b51d5897d Mon Sep 17 00:00:00 2001
From: liangxia <liangxia@cmhi.chinamobile.com>
Date: Wed, 28 Aug 2019 16:54:10 +0800
Subject: [PATCH] =?UTF-8?q?Add=20=20aaa-12=20=E5=A2=9E=E5=8A=A0ulog=5Fapi?=
 =?UTF-8?q?=E9=85=8D=E5=A5=97=E6=B5=8B=E8=AF=95=E5=B7=A5=E5=85=B7=EF=BC=8C?=
 =?UTF-8?q?=E5=B9=B6=E4=BF=AE=E6=AD=A3=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97?=
 =?UTF-8?q?=E6=9C=80=E5=A4=A7=E9=95=BF=E5=BA=A6=E4=B8=BA16=20RCA=EF=BC=9A?=
 =?UTF-8?q?=20SOL=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Aliangxia?=
 =?UTF-8?q?=20=E6=A3=80=E8=A7=86=E4=BA=BA=EF=BC=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Common/klog_api.h                           |   2 +-
 Common/ulog_api.h                           |   2 +-
 Makefile                                    |  13 ++-
 Platform/build/user.ulog.ulog-test.Makefile |  66 +++++++++++++
 Platform/modules/klog_api/api/klog_api.c    |   6 +-
 Platform/user/ulog/log-sched/log_sched.c    |   2 +-
 Platform/user/ulog/ulog-api/ulog_api.c      |  18 +++-
 Platform/user/ulog/ulog-test/ulog_test.c    | 100 ++++++++++++++++++++
 8 files changed, 196 insertions(+), 13 deletions(-)
 create mode 100644 Platform/build/user.ulog.ulog-test.Makefile
 create mode 100644 Platform/user/ulog/ulog-test/ulog_test.c

diff --git a/Common/klog_api.h b/Common/klog_api.h
index 5292a31b9..8a078ae2a 100644
--- a/Common/klog_api.h
+++ b/Common/klog_api.h
@@ -7,7 +7,7 @@
 #define MAX_MODULE_NAME_SZ  16
 
 typedef struct _klog {
-   char module_name[MAX_MODULE_NAME_SZ];
+   char module_name[MAX_MODULE_NAME_SZ+1];
 }klog_t;
 
 
diff --git a/Common/ulog_api.h b/Common/ulog_api.h
index 1d199e2e6..fd2fbb90e 100755
--- a/Common/ulog_api.h
+++ b/Common/ulog_api.h
@@ -8,7 +8,7 @@
 #define MAX_MODULE_NAME_SZ  16
 
 typedef struct _ulog {
-   char module_name[MAX_MODULE_NAME_SZ];
+   char module_name[MAX_MODULE_NAME_SZ+1];
 } ulog_t;
 
 ulog_t *ulog_init(const char *module_name, u8 is_print);
diff --git a/Makefile b/Makefile
index 234e7fdc7..574c8548f 100755
--- a/Makefile
+++ b/Makefile
@@ -28,9 +28,9 @@ MAKE_FLAGS				+= -j$(shell cat /proc/cpuinfo | grep processor | wc -l)
 endif
 endif
 
-.PHONY : demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging
+.PHONY : demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging ulog_test
 
-all: demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging
+all: demo database openrpc ulog klog klog_test conntrack netlink trace redismq usermanager configm webauth matchrule logging ulog_test
 
 ifeq ($(OPT), install)
 #$(shell `find ../release -name "*.zip" -delete`)
@@ -228,4 +228,13 @@ else ifeq ($(OPT), install)
 	$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=test_klog_api
 else
 	$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=test_klog_api
+endif
+
+ulog_test:
+ifeq ($(OPT), clean)
+	$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.ulog-test.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=test_ulog_api
+else ifeq ($(OPT), install)
+	$(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.ulog-test.Makefile install MLOG=$(MLOG) MAKE_TARGET=test_ulog_api
+else
+	$(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.ulog-test.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=test_ulog_api
 endif
\ No newline at end of file
diff --git a/Platform/build/user.ulog.ulog-test.Makefile b/Platform/build/user.ulog.ulog-test.Makefile
new file mode 100644
index 000000000..4986b5f72
--- /dev/null
+++ b/Platform/build/user.ulog.ulog-test.Makefile
@@ -0,0 +1,66 @@
+# target name, the target name must have the same name of c source file
+TARGET_NAME=test_ulog_api
+
+# target
+# for linux module driver: KO
+# for application: EXE
+# for dynamic library: DLL
+TARGET_TYPE = EXE
+
+# target object
+# for application: APP
+# for device driver: DRV
+TARGET_OBJ  = APP
+
+# custom install dir
+TARGET_BOX =
+
+#debug mode or release mode
+DEBUG 			= TRUE
+
+PLAT_LINUX		?= TRUE
+PLAT_ARM64		?= TRUE
+
+VPATH = ../user/ulog/ulog-test
+
+# source code
+
+# set the source file, don't used .o because of ...
+
+COMMON_SRCS = ulog_test.c
+
+# MRS Board Source Files
+PLAT_LINUX_SRCS = $(COMMON_SRCS)
+PLAT_ARM64_SRCS = $(COMMON_SRCS)
+
+# gcc CFLAGS
+PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/ulog -I../user/ulog
+PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS)
+
+
+PLAT_ARM64_LDFLAGS  :=
+PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS)
+
+#gcc libs
+ARM64_LIBS		:= -lopenrpc-arm64 -lulogapi-arm64 -lpthread -lm -lev
+LINUX_LIBS		:= -lopenrpc-linux -lulogapi-linux -lpthread  -lm -lev
+
+# this line must be at below of thus, because of...
+include ../../Common/common.Makefile
+
+ifneq ($(MAKECMDGOALS), clean)
+ifneq ($(MAKECMDGOALS), cleanall)
+ifneq ($(notdir $(DEPEND_LIB)), $(wildcard $(DEPEND_LIB)))
+$(shell $(CP) $(DEPEND_LIB) ./)
+endif
+endif
+endif
+
+ifeq ($(MAKECMDGOALS), )
+$(shell find ./ -name "$(TARGET)-*.ko" -delete)
+else
+ifeq ($(MAKECMDGOALS), all)
+$(shell find ./ -name "$(TARGET)-*.ko" -delete)
+endif
+endif
+
diff --git a/Platform/modules/klog_api/api/klog_api.c b/Platform/modules/klog_api/api/klog_api.c
index 3e6237056..a25a9d763 100644
--- a/Platform/modules/klog_api/api/klog_api.c
+++ b/Platform/modules/klog_api/api/klog_api.c
@@ -18,8 +18,8 @@ klog_t *klog_init(const char *module_name)
 	
 	len = strlen(module_name);
     
-    if (len >= MAX_MODULE_NAME_SZ) {
-        printk(KERN_ERR"The length:%d of module_name must be less than %d, but input module_name is %s", len, MAX_MODULE_NAME_SZ, module_name);
+    if (len > MAX_MODULE_NAME_SZ) {
+        printk(KERN_ERR"The length:%d of module_name must be not more than %d, but input module_name is %s", len, MAX_MODULE_NAME_SZ, module_name);
         return NULL;
     }
 
@@ -28,7 +28,7 @@ klog_t *klog_init(const char *module_name)
         printk(KERN_ERR"Allocating log memory is failure");
         return NULL;
     }
-	memset(log->module_name, '\0', MAX_MODULE_NAME_SZ);
+	memset(log->module_name, '\0', MAX_MODULE_NAME_SZ+1);
     strncpy(log->module_name, module_name, len);
 	
     return log;
diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c
index f34fda764..6c80c89e1 100755
--- a/Platform/user/ulog/log-sched/log_sched.c
+++ b/Platform/user/ulog/log-sched/log_sched.c
@@ -47,7 +47,7 @@ int main(int argc, char **argv)
 
     g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon);
     if (g_log == NULL) {
-        fprintf(stderr, "Initiating ulog is failure");
+        fprintf(stderr, "Initiating ulog is failure\n");
         return -1;
     }
 
diff --git a/Platform/user/ulog/ulog-api/ulog_api.c b/Platform/user/ulog/ulog-api/ulog_api.c
index 4cdd75a31..5c84de244 100755
--- a/Platform/user/ulog/ulog-api/ulog_api.c
+++ b/Platform/user/ulog/ulog-api/ulog_api.c
@@ -13,19 +13,27 @@
 ulog_t *ulog_init(const char *module_name, u8 is_print)
 {
     ulog_t *log;
-    u32 len = strlen(module_name);
-    
+    u32 len = 0;
+
+	if (NULL == module_name) {
+		fprintf(stderr, "Bad input: module_name is NULL\n");
+        return NULL;
+	}
+	
+	len = strlen(module_name);
+	
     if (len > MAX_MODULE_NAME_SZ) {
-        fprintf(stderr, "The length:%d of module_name can't more than %d", len, MAX_MODULE_NAME_SZ);
+        fprintf(stderr, "The length:%d of module_name can't more than %d\n", len, MAX_MODULE_NAME_SZ);
         return NULL;
     }
 
     log = (ulog_t *)malloc(sizeof(*log));
     if (log == NULL) {
-        fprintf(stderr, "Allocating log memory is failure");
+        fprintf(stderr, "Allocating log memory is failure\n");
         return NULL;
     }
     strncpy(log->module_name, module_name, len);
+	log->module_name[len] = '\0';
 
     int opt = LOG_PERROR |LOG_PID;
     if (is_print > 0) {
@@ -47,7 +55,7 @@ void ulog_close(ulog_t *log)
 void ulog_record(const ulog_t *log, int level, const char *fmt, ...)
 {
     if (log == NULL) {
-        fprintf(stderr, "Log is null");
+        fprintf(stderr, "Log is null\n");
         return;
     }
 
diff --git a/Platform/user/ulog/ulog-test/ulog_test.c b/Platform/user/ulog/ulog-test/ulog_test.c
new file mode 100644
index 000000000..15cda4b88
--- /dev/null
+++ b/Platform/user/ulog/ulog-test/ulog_test.c
@@ -0,0 +1,100 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+
+#include "ulog_api.h"
+#include "ulog_in.h"
+
+
+ulog_t *g_log = NULL;
+
+
+static void test_ulog_api()
+{
+	ULOG_DEBUG  (g_log, "test for ulog_api %s", "debug");
+	ULOG_INFO   (g_log, "test for ulog_api %s", "info");
+	ULOG_NOTICE (g_log, "test for ulog_api %s", "notice");
+	ULOG_WARNING(g_log, "test for ulog_api %s", "warning");
+	ULOG_ERR    (g_log, "test for ulog_api %s", "err");
+	ULOG_CRIT   (g_log, "test for ulog_api %s", "crit");
+	ULOG_ALERT  (g_log, "test for ulog_api %s", "alert");
+	ULOG_EMERG  (g_log, "test for ulog_api %s", "emerg");
+}
+
+static void ulog_test_usage(const char *pname)
+{
+	if (NULL == pname)
+	{
+		return;
+	}
+	
+	fprintf(stderr, "--------------------------------------------------------\n");
+	fprintf(stderr, "  usage of %s:\n", pname);
+	fprintf(stderr, "    %s [-d] [-a module_name] [-h]\n", pname);
+	fprintf(stderr, "    \n", pname);
+	fprintf(stderr, "    -d: daemon\n", pname);
+	fprintf(stderr, "    -a: set module name, no longer than 16 bytes\n");
+	fprintf(stderr, "    -h: help\n");
+	fprintf(stderr, "--------------------------------------------------------\n");
+}
+
+/* usage:
+	test_ulog_api 
+	test_ulog_api -a test_ulog_api 
+	test_ulog_api -a test2 
+	test_ulog_api -a 123456789012345 
+	test_ulog_api -a 1234567890123456 
+	test_ulog_api -a 12345678901234567 
+
+	test_ulog_api -d
+	test_ulog_api -d -a test_ulog_api
+	test_ulog_api -d -a test2
+	test_ulog_api -d -a 123456789012345
+	test_ulog_api -d -a 1234567890123456
+	test_ulog_api -d -a 12345678901234567
+*/
+int main(int argc, char **argv)
+{
+    char *options = "da:h";
+    int opt; 
+	u8 run_daemon = 0;
+	char module_name[MAX_MODULE_NAME_SZ+4] = "";
+
+	while ((opt = getopt(argc, argv, options)) != -1) {
+        switch (opt) {
+        case 'd':
+            run_daemon = 1;
+            break;
+		case 'a':
+			memset(module_name, 0, MAX_MODULE_NAME_SZ+4);
+			strncpy(module_name, optarg, MAX_MODULE_NAME_SZ+3);
+			break;
+		case 'h':
+			ulog_test_usage(argv[0]);
+			return 0;
+        }
+    }
+
+	g_log = ulog_init(module_name, run_daemon);
+	if (NULL == g_log)
+	{
+		goto END;
+	}
+
+	if (run_daemon) {
+        if (daemon(0, 0) == -1) {
+            ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno));
+            goto END;
+        }
+    }
+
+	test_ulog_api();
+
+END:
+	if (NULL != g_log)
+	{
+		ulog_close(g_log);
+	}
+    return 0;
+}