From 9f183b4708cba832933228c63cf4ee1b933e6c71 Mon Sep 17 00:00:00 2001 From: zhangtaohz Date: Mon, 19 Aug 2019 18:20:06 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=20aaa-12=20=E5=A2=9E=E5=8A=A0=E5=86=85?= =?UTF-8?q?=E6=A0=B8=E6=97=A5=E5=BF=97=E6=8E=A5=E5=8F=A3=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E6=8F=90=E4=BE=9B=E5=AF=B9=E5=BA=94=E7=9A=84?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BE=8B=E7=A8=8B=20RCA=EF=BC=9A=20SOL?= =?UTF-8?q?=EF=BC=9A=20=E4=BF=AE=E6=94=B9=E4=BA=BA=EF=BC=9Aliangxia=20?= =?UTF-8?q?=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 | 28 ++++++ Makefile | 22 ++++- Platform/build/module.klog_api.api.Makefile | 49 +++++++++++ Platform/build/module.klog_api.test.Makefile | 49 +++++++++++ Platform/modules/klog_api/api/Makefile | 15 ++++ Platform/modules/klog_api/api/klog_api.c | 88 +++++++++++++++++++ Platform/modules/klog_api/api/klog_api_mod.c | 28 ++++++ Platform/modules/klog_api/test/Makefile | 15 ++++ .../modules/klog_api/test/test_klog_api.c | 49 +++++++++++ .../modules/klog_api/test/test_klog_api_mod.c | 28 ++++++ 10 files changed, 367 insertions(+), 4 deletions(-) create mode 100644 Common/klog_api.h create mode 100644 Platform/build/module.klog_api.api.Makefile create mode 100644 Platform/build/module.klog_api.test.Makefile create mode 100644 Platform/modules/klog_api/api/Makefile create mode 100644 Platform/modules/klog_api/api/klog_api.c create mode 100644 Platform/modules/klog_api/api/klog_api_mod.c create mode 100644 Platform/modules/klog_api/test/Makefile create mode 100644 Platform/modules/klog_api/test/test_klog_api.c create mode 100644 Platform/modules/klog_api/test/test_klog_api_mod.c diff --git a/Common/klog_api.h b/Common/klog_api.h new file mode 100644 index 000000000..5292a31b9 --- /dev/null +++ b/Common/klog_api.h @@ -0,0 +1,28 @@ +#ifndef _KLOG_API_H +#define _KLOG_API_H + +#include +#include "common_types.h" + +#define MAX_MODULE_NAME_SZ 16 + +typedef struct _klog { + char module_name[MAX_MODULE_NAME_SZ]; +}klog_t; + + +extern klog_t *klog_init(const char *module_name); +extern void klog_close(klog_t *log); +extern void klog_record(const klog_t *log, const char* level_str, const char *fmt, ...); + +#define KLOG_DEBUG(log, fmt, ...) klog_record(log, KERN_DEBUG, fmt, ##__VA_ARGS__) +#define KLOG_INFO(log, fmt, ...) klog_record(log, KERN_INFO, fmt, ##__VA_ARGS__) +#define KLOG_NOTICE(log, fmt, ...) klog_record(log, KERN_NOTICE, fmt, ##__VA_ARGS__) +#define KLOG_WARNING(log, fmt, ...) klog_record(log, KERN_WARNING, fmt, ##__VA_ARGS__) +#define KLOG_ERR(log, fmt, ...) klog_record(log, KERN_ERR, fmt, ##__VA_ARGS__) +#define KLOG_CRIT(log, fmt, ...) klog_record(log, KERN_CRIT, fmt, ##__VA_ARGS__) +#define KLOG_ALERT(log, fmt, ...) klog_record(log, KERN_ALERT, fmt, ##__VA_ARGS__) +#define KLOG_EMERG(log, fmt, ...) klog_record(log, KERN_EMERG, fmt, ##__VA_ARGS__) + +#endif + diff --git a/Makefile b/Makefile index 5f0fc8dac..234e7fdc7 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 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 -all: demo database openrpc ulog 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 ifeq ($(OPT), install) #$(shell `find ../release -name "*.zip" -delete`) @@ -212,6 +212,20 @@ else $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.logging.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=logging endif +klog: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.api.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=klog_api +else ifeq ($(OPT), install) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.api.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=klog_api +else + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f module.klog_api.api.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=klog_api +endif - - +klog_test: +ifeq ($(OPT), clean) + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f module.klog_api.test.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=test_klog_api +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 \ No newline at end of file diff --git a/Platform/build/module.klog_api.api.Makefile b/Platform/build/module.klog_api.api.Makefile new file mode 100644 index 000000000..2159eac97 --- /dev/null +++ b/Platform/build/module.klog_api.api.Makefile @@ -0,0 +1,49 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=klog_api + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = KO + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = DRV + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../modules/klog_api/api + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = klog_api.c klog_api_mod.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -I../../Common +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +ifeq ($(MAKECMDGOALS), ) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +else +ifeq ($(MAKECMDGOALS), all) +$(shell find ./ -name "$(TARGET)-*.ko" -delete) +endif +endif diff --git a/Platform/build/module.klog_api.test.Makefile b/Platform/build/module.klog_api.test.Makefile new file mode 100644 index 000000000..0d4bfa64b --- /dev/null +++ b/Platform/build/module.klog_api.test.Makefile @@ -0,0 +1,49 @@ +# target name, the target name must have the same name of c source file +TARGET_NAME=test_klog_api + +# target +# for linux module driver: KO +# for application: EXE +# for dynamic library: DLL +TARGET_TYPE = KO + +# target object +# for application: APP +# for device driver: DRV +TARGET_OBJ = DRV + +# custom install dir +TARGET_BOX = + +#debug mode or release mode +DEBUG = TRUE + +PLAT_LINUX ?= TRUE +PLAT_ARM64 ?= TRUE + +VPATH = ../modules/klog_api/test + +# source code + +# set the source file, don't used .o because of ... + +COMMON_SRCS = test_klog_api.c test_klog_api_mod.c + +# MRS Board Source Files +PLAT_LINUX_SRCS = $(COMMON_SRCS) +PLAT_ARM64_SRCS = $(COMMON_SRCS) + +# gcc CFLAGS +PLAT_ARM64_CFLAGS := -I../../Common +PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) + +# this line must be at below of thus, because of... +include ../../Common/common.Makefile + +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/Makefile b/Platform/modules/klog_api/api/Makefile new file mode 100644 index 000000000..248af41fa --- /dev/null +++ b/Platform/modules/klog_api/api/Makefile @@ -0,0 +1,15 @@ +ifneq ($(KERNELRELEASE),) + obj-m += klog_api.o +else + PWD := $(shell pwd) + KVER := $(shell uname -r) +# KDIR := /lib/modules/$(KVER)/build + KDIR := $(HUACHENG_LINUX_KERNEL) +# KBUILD_EXTRA_SYMBOLS += /data/code/modules/netlink_api/app_k/Module.symvers +default: + $(MAKE) -C $(KDIR) M=$(PWD) modules +all: + make -C $(KDIR) M=$(PWD) modules +clean: + rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mks +endif diff --git a/Platform/modules/klog_api/api/klog_api.c b/Platform/modules/klog_api/api/klog_api.c new file mode 100644 index 000000000..3e6237056 --- /dev/null +++ b/Platform/modules/klog_api/api/klog_api.c @@ -0,0 +1,88 @@ +#include "klog_api.h" +#include +#include + +#define LOG_MSG_SZ 1024 +#define MODULE_FMT "[%s]" + +klog_t *g_klog = NULL; + +klog_t *klog_init(const char *module_name) +{ + klog_t *log = NULL; + u32 len = 0; + + if(NULL == module_name) { + return NULL; + } + + 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); + return NULL; + } + + log = (klog_t *)kmalloc(sizeof(*log), GFP_KERNEL); + if (NULL == log) { + printk(KERN_ERR"Allocating log memory is failure"); + return NULL; + } + memset(log->module_name, '\0', MAX_MODULE_NAME_SZ); + strncpy(log->module_name, module_name, len); + + return log; +} + +void klog_close(klog_t *log) +{ + if (log != NULL) { + kfree(log); + } +} + +void klog_record(const klog_t *log, const char* level_str, const char *fmt, ...) +{ + char log_buf[LOG_MSG_SZ]; + va_list args; + + va_start(args, fmt); + vsnprintf(log_buf, sizeof(log_buf), fmt, args); + va_end(args); + + if (NULL != log) { + printk("%s"MODULE_FMT" %s", level_str, log->module_name, log_buf) ; + } + else { + printk("%s %s", level_str, log_buf) ; + } +} + +EXPORT_SYMBOL_GPL(klog_init); +EXPORT_SYMBOL_GPL(klog_close); +EXPORT_SYMBOL_GPL(klog_record); + + +static int __init klog_api_init(void) +{ + g_klog = klog_init("klog_api"); + + KLOG_INFO(g_klog, "Klog is initiated"); + + return 0; +} + +static void __exit klog_api_exit(void) +{ + KLOG_INFO(g_klog, "Klog is exited"); + + klog_close(g_klog); +} + +module_init(klog_api_init); +module_exit(klog_api_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Klog_api process module"); +MODULE_AUTHOR("zhangtao"); + diff --git a/Platform/modules/klog_api/api/klog_api_mod.c b/Platform/modules/klog_api/api/klog_api_mod.c new file mode 100644 index 000000000..d6a10e65b --- /dev/null +++ b/Platform/modules/klog_api/api/klog_api_mod.c @@ -0,0 +1,28 @@ +#include +#include +#include + +MODULE_INFO(vermagic, VERMAGIC_STRING); +MODULE_INFO(name, KBUILD_MODNAME); + +__visible struct module __this_module +__attribute__((section(".gnu.linkonce.this_module"))) = { + .name = KBUILD_MODNAME, + .init = init_module, +#ifdef CONFIG_MODULE_UNLOAD + .exit = cleanup_module, +#endif + .arch = MODULE_ARCH_INIT, +}; + +#ifdef RETPOLINE +MODULE_INFO(retpoline, "Y"); +#endif + +static const char __module_depends[] +__used +__attribute__((section(".modinfo"))) = +"depends="; + + +MODULE_INFO(srcversion, "176959D2D9B79E0BAD85AE2"); diff --git a/Platform/modules/klog_api/test/Makefile b/Platform/modules/klog_api/test/Makefile new file mode 100644 index 000000000..9cf810486 --- /dev/null +++ b/Platform/modules/klog_api/test/Makefile @@ -0,0 +1,15 @@ +ifneq ($(KERNELRELEASE),) + obj-m += test_klog_api.o +else + PWD := $(shell pwd) + KVER := $(shell uname -r) +# KDIR := /lib/modules/$(KVER)/build + KDIR := $(HUACHENG_LINUX_KERNEL) +# KBUILD_EXTRA_SYMBOLS += /data/code/modules/netlink_api/app_k/Module.symvers +default: + $(MAKE) -C $(KDIR) M=$(PWD) modules +all: + make -C $(KDIR) M=$(PWD) modules +clean: + rm -rf *.o *.mod.c *.ko *.symvers *.order *.makers .*.cmd *.cmd .tmp_versions .cache.mks +endif diff --git a/Platform/modules/klog_api/test/test_klog_api.c b/Platform/modules/klog_api/test/test_klog_api.c new file mode 100644 index 000000000..a0d96911b --- /dev/null +++ b/Platform/modules/klog_api/test/test_klog_api.c @@ -0,0 +1,49 @@ +#include "klog_api.h" +#include + +klog_t *g_klog = NULL; + +static void test_klog_api(const char *module_name) +{ + g_klog = klog_init(module_name); + + KLOG_DEBUG (g_klog, "test for klog_api %s", "debug"); + KLOG_INFO (g_klog, "test for klog_api %s", "info"); + KLOG_NOTICE (g_klog, "test for klog_api %s", "notice"); + KLOG_WARNING(g_klog, "test for klog_api %s", "warning"); + KLOG_ERR (g_klog, "test for klog_api %s", "err"); + KLOG_CRIT (g_klog, "test for klog_api %s", "crit"); + KLOG_ALERT (g_klog, "test for klog_api %s", "alert"); + KLOG_EMERG (g_klog, "test for klog_api %s", "emerg"); + + klog_close(g_klog); +} + +static int __init test_klog_api_init(void) +{ + printk(KERN_INFO"Test-klog_api is initiated"); + + test_klog_api("test_klog_api"); + test_klog_api(NULL); + test_klog_api(""); + test_klog_api("a"); + test_klog_api("123456789012345"); + test_klog_api("1234567890123456"); + test_klog_api("12345678901234567"); + + return 0; +} + +static void __exit test_klog_api_exit(void) +{ + printk(KERN_INFO"Test-klog_api is exited"); +} + + +module_init(test_klog_api_init); +module_exit(test_klog_api_exit); + +MODULE_LICENSE("GPL"); +MODULE_DESCRIPTION("Test-klog_api process module"); +MODULE_AUTHOR("zhangtao"); + diff --git a/Platform/modules/klog_api/test/test_klog_api_mod.c b/Platform/modules/klog_api/test/test_klog_api_mod.c new file mode 100644 index 000000000..d6a10e65b --- /dev/null +++ b/Platform/modules/klog_api/test/test_klog_api_mod.c @@ -0,0 +1,28 @@ +#include +#include +#include + +MODULE_INFO(vermagic, VERMAGIC_STRING); +MODULE_INFO(name, KBUILD_MODNAME); + +__visible struct module __this_module +__attribute__((section(".gnu.linkonce.this_module"))) = { + .name = KBUILD_MODNAME, + .init = init_module, +#ifdef CONFIG_MODULE_UNLOAD + .exit = cleanup_module, +#endif + .arch = MODULE_ARCH_INIT, +}; + +#ifdef RETPOLINE +MODULE_INFO(retpoline, "Y"); +#endif + +static const char __module_depends[] +__used +__attribute__((section(".modinfo"))) = +"depends="; + + +MODULE_INFO(srcversion, "176959D2D9B79E0BAD85AE2");