Add aaa-12 增加ulog_api配套测试工具,并修正日志模块最大长度为16

RCA:
SOL:
修改人:liangxia
检视人:
This commit is contained in:
liangxia 2019-08-28 16:54:10 +08:00
parent 0f3c0bb217
commit fa0a7fa5c7
8 changed files with 196 additions and 13 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}