diff --git a/Common/ulog_api.h b/Common/ulog_api.h index f28ef563c..1d199e2e6 100755 --- a/Common/ulog_api.h +++ b/Common/ulog_api.h @@ -1,13 +1,17 @@ #ifndef _ULOG_API_H #define _ULOG_API_H +#include + +#include "common_types.h" + #define MAX_MODULE_NAME_SZ 16 typedef struct _ulog { char module_name[MAX_MODULE_NAME_SZ]; } ulog_t; -ulog_t *ulog_init(const char *module_name); +ulog_t *ulog_init(const char *module_name, u8 is_print); void ulog_close(ulog_t *log); void ulog_record(const ulog_t *log, int level, const char *fmt, ...); diff --git a/ControlPlatform/aaa/aaa-shiro/impl/pom.xml b/ControlPlatform/aaa/aaa-shiro/impl/pom.xml index 83622d57b..71e68b848 100644 --- a/ControlPlatform/aaa/aaa-shiro/impl/pom.xml +++ b/ControlPlatform/aaa/aaa-shiro/impl/pom.xml @@ -251,15 +251,6 @@ and is available at http://www.eclipse.org/legal/epl-v10.html - - org.jacoco - jacoco-maven-plugin - - - **/*UserHandler.* - - - diff --git a/ControlPlatform/aaa/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/idm/UserHandler.java b/ControlPlatform/aaa/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/idm/UserHandler.java index 5e86df2a9..e92978a53 100644 --- a/ControlPlatform/aaa/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/idm/UserHandler.java +++ b/ControlPlatform/aaa/aaa-shiro/impl/src/main/java/org/opendaylight/aaa/shiro/idm/UserHandler.java @@ -14,21 +14,7 @@ package org.opendaylight.aaa.shiro.idm; -import java.util.Collection; -import java.util.Objects; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriInfo; import org.opendaylight.aaa.api.ClaimCache; -import org.opendaylight.aaa.AAAShiroProvider; import org.opendaylight.aaa.api.IDMStoreException; import org.opendaylight.aaa.api.IIDMStore; import org.opendaylight.aaa.api.model.IDMError; @@ -37,7 +23,14 @@ import org.opendaylight.aaa.api.model.Users; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.ws.rs.*; +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; @@ -58,7 +51,7 @@ import java.util.Objects; public class UserHandler { private static final Logger LOG = LoggerFactory.getLogger(UserHandler.class); - private static final String PW_PATTERN = "/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,}$/"; + private static final String PW_PATTERN = "^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{8,}$"; /** * If a user is created through the /auth/v1/users rest * endpoint without a password, the default password is assigned to the @@ -129,10 +122,9 @@ public class UserHandler { * Extracts the user represented by id. The password and salt * fields are redacted for security reasons. * - * @param id - * the unique id of representing the user account + * @param id the unique id of representing the user account * @return A response with the user information, or internal error if one - * occurs + * occurs */ @GET @Path("/{id}") @@ -170,10 +162,8 @@ public class UserHandler { * If a password is not provided, please ensure you change the default * password ASAP for security reasons! * - * @param info - * passed from Jersey - * @param user - * the user defined in the JSON payload + * @param info passed from Jersey + * @param user the user defined in the JSON payload * @return A response stating success or failure of user creation */ @POST @@ -220,7 +210,7 @@ public class UserHandler { String userDescription = user.getDescription(); if (userDescription == null) { user.setDescription(DEFAULT_DESCRIPTION); - } else if (userDescription.length() > IdmLightApplication.MAX_FIELD_LEN) { + } else if (userDescription.length() > IdmLightApplication.MAX_FIELD_LEN) { // The "description" field has a maximum length. return providedFieldTooLong("description", IdmLightApplication.MAX_FIELD_LEN); } @@ -242,7 +232,7 @@ public class UserHandler { return providedFieldTooLong("password", IdmLightApplication.MAX_FIELD_LEN); } - Response response = checkPasswordError(userPassword); + Response response = checkPasswordError(userPassword); if (response.getStatus() != 200) { return response; } @@ -278,15 +268,13 @@ public class UserHandler { private Response providePasswordError(String s) { return new IDMError(407, s).response(); } + /** * REST endpoint to update a user account. * - * @param info - * passed from Jersey - * @param user - * the user defined in the JSON payload - * @param id - * the unique id for the user that will be updated + * @param info passed from Jersey + * @param user the user defined in the JSON payload + * @param id the unique id for the user that will be updated * @return A response stating success or failure of the user update */ @PUT @@ -338,10 +326,8 @@ public class UserHandler { /** * REST endpoint to delete a user account. * - * @param info - * passed from Jersey - * @param id - * the unique id of the user which is being deleted + * @param info passed from Jersey + * @param id the unique id of the user which is being deleted * @return A response stating success or failure of user deletion */ @DELETE @@ -368,10 +354,8 @@ public class UserHandler { /** * Creates a Response related to an internal server error. * - * @param verbal - * such as "creating", "deleting", "updating" - * @param ex - * The exception, which is logged locally + * @param verbal such as "creating", "deleting", "updating" + * @param ex The exception, which is logged locally * @return A response containing internal error with specific reasoning */ private Response internalError(final String verbal, final Exception ex) { @@ -384,8 +368,7 @@ public class UserHandler { * Creates a Response related to the user not providing a * required field. * - * @param fieldName - * the name of the field which is missing + * @param fieldName the name of the field which is missing * @return A response explaining that the request is missing a field */ private Response missingRequiredField(final String fieldName) { @@ -400,10 +383,8 @@ public class UserHandler { * Creates a Response related to the user providing a field * that is too long. * - * @param fieldName - * the name of the field that is too long - * @param maxFieldLength - * the maximum length of fieldName + * @param fieldName the name of the field that is too long + * @param maxFieldLength the maximum length of fieldName * @return A response containing the bad field and the maximum field length */ private Response providedFieldTooLong(final String fieldName, final int maxFieldLength) { @@ -414,10 +395,8 @@ public class UserHandler { * Creates the client-facing message related to the user providing a field * that is too long. * - * @param fieldName - * the name of the field that is too long - * @param maxFieldLength - * the maximum length of fieldName + * @param fieldName the name of the field that is too long + * @param maxFieldLength the maximum length of fieldName * @return a response containing the too long field and its length */ private static String getProvidedFieldTooLongMessage(final String fieldName, final int maxFieldLength) { @@ -430,8 +409,7 @@ public class UserHandler { * Prepares a user account for output by redacting the appropriate fields. * This method side-effects the user parameter. * - * @param user - * the user account which will have fields redacted + * @param user the user account which will have fields redacted */ private static void redactUserPasswordInfo(final User user) { user.setPassword(REDACTED_PASSWORD); @@ -441,8 +419,7 @@ public class UserHandler { /** * Validate the input field length. * - * @param inputField - * the field to check + * @param inputField the field to check * @return true if input field bigger than the MAX_FIELD_LEN */ private boolean checkInputFieldLength(final String inputField) { diff --git a/ControlPlatform/aaa/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/idm/rest/test/UserHandlerTest.java b/ControlPlatform/aaa/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/idm/rest/test/UserHandlerTest.java index 29afa90da..857a83a8c 100644 --- a/ControlPlatform/aaa/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/idm/rest/test/UserHandlerTest.java +++ b/ControlPlatform/aaa/aaa-shiro/impl/src/test/java/org/opendaylight/aaa/shiro/idm/rest/test/UserHandlerTest.java @@ -14,24 +14,24 @@ package org.opendaylight.aaa.shiro.idm.rest.test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import javax.ws.rs.NotFoundException; -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.Response; import org.junit.Ignore; import org.junit.Test; import org.opendaylight.aaa.api.model.IDMError; import org.opendaylight.aaa.api.model.User; import org.opendaylight.aaa.api.model.Users; -@Ignore +import javax.ws.rs.NotFoundException; +import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + public class UserHandlerTest extends HandlerTest { @Test @@ -61,12 +61,14 @@ public class UserHandlerTest extends HandlerTest { // check create user Map usrData = new HashMap<>(); usrData.put("name", "usr1"); - usrData.put("description", "test user"); - usrData.put("enabled", "true"); +// usrData.put("description", "test user"); +// usrData.put("enabled", "true"); usrData.put("email", "user1@usr.org"); usrData.put("password", "ChangeZbadPa$$w0rd"); usrData.put("domainid", "0"); - Response clientResponse = target("/v1/users").request().post(entity(usrData)); + Response clientResponse = target("/v1/users") + .request() + .post(entity(usrData)); assertEquals(201, clientResponse.getStatus()); // check create user missing name data @@ -101,8 +103,8 @@ public class UserHandlerTest extends HandlerTest { // Bug 8382: if a user id is specified, 400 is returned usrData = new HashMap<>(); usrData.put("name", "usr1"); - usrData.put("description", "test user"); - usrData.put("enabled", "true"); +// usrData.put("description", "test user"); +// usrData.put("enabled", "true"); usrData.put("email", "user1@usr.org"); usrData.put("password", "ChangeZbadPa$$w0rd"); usrData.put("userid", "userid"); @@ -113,10 +115,10 @@ public class UserHandlerTest extends HandlerTest { } /** * Revision history - * + *

* ------------------------------------------------------------------------- * Date Author Note - * + *

* ------------------------------------------------------------------------- * 2019/7/3 Dong Xiancun creat */ diff --git a/ControlPlatform/aaa/parent/pom.xml b/ControlPlatform/aaa/parent/pom.xml index 0e504587c..39bbe1074 100644 --- a/ControlPlatform/aaa/parent/pom.xml +++ b/ControlPlatform/aaa/parent/pom.xml @@ -139,11 +139,6 @@ - - - **/*UserHandler.* - - diff --git a/Makefile b/Makefile index 21adffc41..2e109ee7e 100755 --- a/Makefile +++ b/Makefile @@ -162,14 +162,14 @@ endif ulog: ifeq ($(OPT), clean) - $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.syslog_sched.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=syslog-sched $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.api.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=ulog-api + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.log_sched.Makefile cleanall MLOG=$(MLOG) MAKE_TARGET=log-sched else ifeq ($(OPT), install) - $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.syslog_sched.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=syslog-sched $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.api.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=ulog-api + $(MLOG)make $(MAKE_FLAGS) -C Platform/build -f user.ulog.log_sched.Makefile install DIR=$(DIR) MLOG=$(MLOG) MAKE_TARGET=log-sched else - $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.syslog_sched.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=syslog-sched $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.api.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=ulog-api + $(MLOG)make all $(MAKE_FLAGS) -C Platform/build -f user.ulog.log_sched.Makefile MLOG=$(MLOG) DISABLE_WARRING=$(DIS_BUILD_WARRING) MAKE_TARGET=log-sched endif database: diff --git a/Platform/build/user.ulog.api.Makefile b/Platform/build/user.ulog.api.Makefile index f1827ba94..adbe21463 100755 --- a/Platform/build/user.ulog.api.Makefile +++ b/Platform/build/user.ulog.api.Makefile @@ -34,7 +34,7 @@ PLAT_LINUX_SRCS = $(COMMON_SRCS) PLAT_ARM64_SRCS = $(COMMON_SRCS) # gcc CFLAGS -PLAT_ARM64_CFLAGS := -fPIC -I../../Common +PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../user/ulog PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) @@ -44,6 +44,7 @@ PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) #gcc libs + # this line must be at below of thus, because of... include ../../Common/common.Makefile diff --git a/Platform/build/user.ulog.syslog_sched.Makefile b/Platform/build/user.ulog.log_sched.Makefile similarity index 65% rename from Platform/build/user.ulog.syslog_sched.Makefile rename to Platform/build/user.ulog.log_sched.Makefile index ffa9dcb1d..baf911a8e 100755 --- a/Platform/build/user.ulog.syslog_sched.Makefile +++ b/Platform/build/user.ulog.log_sched.Makefile @@ -1,5 +1,5 @@ # target name, the target name must have the same name of c source file -TARGET_NAME=syslog-sched +TARGET_NAME=log-sched # target # for linux module driver: KO @@ -21,20 +21,20 @@ DEBUG = TRUE PLAT_LINUX ?= TRUE PLAT_ARM64 ?= TRUE -VPATH = ../user/ulog/syslog-schedule +VPATH = ../user/ulog/log-sched # source code # set the source file, don't used .o because of ... -COMMON_SRCS = log_file.c log_sched.c +COMMON_SRCS = log_file.c log_console.c log_common.c log_sched.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/rpc +PLAT_ARM64_CFLAGS := -fPIC -I../../Common -I../common/rpc -I../user/ulog PLAT_LINUX_CFLAGS := $(PLAT_ARM64_CFLAGS) @@ -42,17 +42,17 @@ PLAT_ARM64_LDFLAGS := -lpthread PLAT_LINUX_LDFLAGS := $(PLAT_ARM64_LDFLAGS) #gcc libs -ARM64_LIBS := ./libopenrpc-arm64.so ../thirdparty/arm64/libev-arm64.so -LINUX_LIBS := ./libopenrpc-linux.so ../thirdparty/x86_64/libev-linux.so +ARM64_LIBS := ./libopenrpc-arm64.so ./libulogapi-arm64.so ../thirdparty/arm64/libev-arm64.so +LINUX_LIBS := ./libopenrpc-linux.so ./libulogapi-linux.so ../thirdparty/x86_64/libev-linux.so ifeq ($(PLAT_ARM64), TRUE) -DEPEND_LIB += ./debug/libopenrpc-arm64.so -USER_CLEAN_ITEMS += ./libopenrpc-arm64.so +DEPEND_LIB += ./debug/libopenrpc-arm64.so ./debug/libulogapi-arm64.so +USER_CLEAN_ITEMS += ./libopenrpc-arm64.so ./libulogapi-arm64.so endif ifeq ($(PLAT_LINUX), TRUE) -DEPEND_LIB += ./debug/libopenrpc-linux.so -USER_CLEAN_ITEMS += ./libopenrpc-linux.so +DEPEND_LIB += ./debug/libopenrpc-linux.so ./debug/libulogapi-linux.so +USER_CLEAN_ITEMS += ./libopenrpc-linux.so ./libulogapi-linux.so endif # this line must be at below of thus, because of... diff --git a/Platform/common/rpc/rpc_module.h b/Platform/common/rpc/rpc_module.h index 837ac0415..529059531 100755 --- a/Platform/common/rpc/rpc_module.h +++ b/Platform/common/rpc/rpc_module.h @@ -7,6 +7,8 @@ #define MODULE_NAME_LEN 32 #define IP_STR_LEN 32 +#define RPC_MODULE_SYSLOG_NAME "syslog-schedule-rpc" + struct _rpc_module { char module_name[MODULE_NAME_LEN]; char host[IP_STR_LEN]; @@ -18,7 +20,8 @@ typedef struct _rpc_module rpc_module; #define MODULE_REG_ARRAY \ { \ - {"ConfigManger#0", "127.0.0.1", 10002, 1} \ + {"ConfigManger#0", "127.0.0.1", 10002, 1}, \ + {RPC_MODULE_SYSLOG_NAME, "127.0.0.1", 10003, 2} \ } #endif /* RPC_MODULE_H_ */ diff --git a/Platform/user/ulog/log-sched/log_common.c b/Platform/user/ulog/log-sched/log_common.c new file mode 100755 index 000000000..e370425ec --- /dev/null +++ b/Platform/user/ulog/log-sched/log_common.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include + +#include "ulog.h" +#include "log_common.h" + +#define FILTER_CONTENT ":msg,contains,\""MODULE_FMT"\"\n" + +#define BAK_FILE "/tmp/%s" + +int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, + const char *rediect_path, const char *filter_mod) +{ + FILE *fp = NULL; + int ret = 1; + u8 exist_backup = 1; + + /********** rsyslog configure **********/ + char conf_path_file[MAX_LINE_SIZE]; + snprintf(conf_path_file, sizeof(conf_path_file), "%s%s", conf_path, conf_file); + + char bak_file[MAX_LINE_SIZE]; + snprintf(bak_file, sizeof(bak_file), BAK_FILE, conf_file); + if (rename(conf_path_file, bak_file) < 0) { + if (errno == ENOENT) { + exist_backup = 0; + ULOG_INFO(g_log, "Been not exist, Configure file:%s is need to backup"); + } else { + ULOG_ERR(g_log, "Baking configure file:%s is failure:%s %d", conf_path_file, strerror(errno), errno); + return ret; + } + } + + fp = fopen(conf_path_file, "w"); + if (fp == NULL) { + ULOG_ERR(g_log, "Opening log configure file:%s is failure:%s", conf_path_file, strerror(errno)); + goto END; + } + + int i; + char line[MAX_LINE_SIZE + 100] = {0}; + if ((filter_mod != NULL) && (strlen(filter_mod) > 0)) { + snprintf(line, sizeof(line), FILTER_CONTENT, filter_mod); + if (fputs(line, fp) == EOF) { + ULOG_ERR(g_log, "Message filter:%s of configure file which is written is failure:%s", + filter_mod, strerror(errno)); + goto END; + } + } + + + char tmp[20]; + line[0] = '\0'; // 清零 + for (i = 0; i <= level;) { + if (snprintf(tmp, sizeof(tmp), "*.=%s", g_level_array[i].str) < 0) { + ULOG_ERR(g_log, "Setting content of log file configure is failure"); + goto END; + } + strcat(line, tmp); + i++; + if (level >= i) { + strcat(line, ";"); + } + } + strcat(line, " "); + + strcat(line, rediect_path); + + if (fputs(line, fp) == EOF) { + ULOG_ERR(g_log, "Configure file which is written is failure:%s", strerror(errno)); + goto END; + } + + ret = 0; +END: + if (fp != NULL) { + fclose(fp); + } + + if ((ret == 1) && (exist_backup == 1)) { + // 回复备份配置 + if (rename(bak_file, conf_path_file) < 0) { + ULOG_ERR(g_log, "Restoring configure file:%s is failure:%s", conf_path_file, strerror(errno)); + } + } + + return ret; +} + +int modify_authorizing(const char *redirect_path) +{ + if (chmod(redirect_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) < 0) { + ULOG_ERR(g_log, "Authorizing of %s which is modified is failure:%s", strerror(errno)); + return 1; + } + return 0; +} + diff --git a/Platform/user/ulog/log-sched/log_common.h b/Platform/user/ulog/log-sched/log_common.h new file mode 100755 index 000000000..e284f643f --- /dev/null +++ b/Platform/user/ulog/log-sched/log_common.h @@ -0,0 +1,32 @@ +#ifndef _LOG_COMMON_H +#define _LOG_COMMON_H + +#include "ulog_api.h" + +#define MAX_LINE_SIZE 1024 + +#define LOG_CONF_PATH "/etc/rsyslog.d/" + +typedef struct _level_str { + u32 level; + char str[10]; +} level_str_t; + +extern ulog_t *g_log; + +static level_str_t g_level_array[] = { + {LOG_EMERG, "emerg"}, + {LOG_ALERT, "alert"}, + {LOG_CRIT, "crit"}, + {LOG_ERR, "err"}, + {LOG_WARNING, "warn"}, + {LOG_NOTICE, "notice"}, + {LOG_INFO, "info"}, + {LOG_DEBUG, "debug"} +}; + +int write_log_conf(const u8 level, const char *conf_path, const char *conf_file, + const char *rediect_path, const char *filter_mod); +int modify_authorizing(const char *redirect_path); + +#endif diff --git a/Platform/user/ulog/log-sched/log_console.c b/Platform/user/ulog/log-sched/log_console.c new file mode 100755 index 000000000..f8796e501 --- /dev/null +++ b/Platform/user/ulog/log-sched/log_console.c @@ -0,0 +1,35 @@ +#include "log_console.h" +#include "log_common.h" + +#define LOG_CONF_COSOLE_FILE_NAME "log-console.conf" + +#define LOG_REDIRECT_CONSOLE "/dev/console" + +static int config_log_console(const log_console_t *conf) +{ + if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_COSOLE_FILE_NAME, LOG_REDIRECT_CONSOLE, conf->module_name) != 0) { + ULOG_ERR(g_log, "configure of log conosle which is written is failure"); + return 1; + } + + if (modify_authorizing(LOG_REDIRECT_CONSOLE) != 0) { + ULOG_ERR(g_log, "Modifying authorizing of %s is failure", LOG_REDIRECT_CONSOLE); + return 1; + } + + return 0; +} + +void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data) +{ + u32 need_len = sizeof(log_console_t); + if (input_len < need_len) { + ULOG_WARNING(g_log, + "The input paramter of rpc log console is needed length of %u, but the actual length is %u", + need_len, input_len); + return; + } + + config_log_console((const log_console_t *)input); +} + diff --git a/Platform/user/ulog/log-sched/log_console.h b/Platform/user/ulog/log-sched/log_console.h new file mode 100755 index 000000000..f4aac8489 --- /dev/null +++ b/Platform/user/ulog/log-sched/log_console.h @@ -0,0 +1,16 @@ +#ifndef _LOG_CONSOLE_H +#define _LOG_CONSOLE_H + +#include "ulog_api.h" +#include "common_types.h" +#include "rpc_common.h" + +typedef struct _log_console { + u8 level; + u8 on; + char module_name[MAX_MODULE_NAME_SZ]; +} log_console_t; + +void rpc_conf_log_console(rpc_conn *conn, pointer input, int input_len, pointer data); + +#endif diff --git a/Platform/user/ulog/log-sched/log_file.c b/Platform/user/ulog/log-sched/log_file.c new file mode 100755 index 000000000..8b04c8497 --- /dev/null +++ b/Platform/user/ulog/log-sched/log_file.c @@ -0,0 +1,137 @@ +#include +#include + +#include "log_file.h" +#include "log_common.h" + +#define LOG_CONF_FILE_NAME "log-file.conf" + +#define LOG_LOGRATATE_FILE_NAME "/etc/logrotate.d/log-syslog" + +#define DEFAULT_LOG_FILE "/var/log/syslog-test" +#define DEFAULT_LOG_DEL_OVER_DAYS (30 * 6) + +#define LOGROTATE_CONF "%s\n" \ + "{\n" \ + " rotate %u\n" \ + " daily\n" \ + " missingok\n" \ + "%s" \ + " postrotate\n" \ + " /usr/lib/rsyslog/rsyslog-rotate\n" \ + " endscript\n" \ + "}" + +#define STR_COMPRESS " compress\n" + +/* +typedef struct _level_str { + u32 level; + char str[10]; +} level_str_t; + + +static level_str_t g_level_array[] = { + {LOG_EMERG, "emerg"}, + {LOG_ALERT, "alert"}, + {LOG_CRIT, "crit"}, + {LOG_ERR, "err"}, + {LOG_WARNING, "warn"}, + {LOG_NOTICE, "notice"}, + {LOG_INFO, "info"}, + {LOG_DEBUG, "debug"} +}; +*/ + +#define MAX_LOG_LEVEL_VALUE (sizeof(g_level_array) / sizeof(level_str_t)) + +static int write_logratate_conf(const log_file_t *conf, const char *log_path) +{ + int ret = 1; + FILE *fp = NULL; + /********** logrotate **********/ + char str_compress[sizeof(STR_COMPRESS)] = {0}; + if (conf->is_compress == LOG_COMPRESS) { + strncpy(str_compress, STR_COMPRESS, sizeof(str_compress)); + } + + u32 days = DEFAULT_LOG_DEL_OVER_DAYS; + if (conf->del_over_days > 0) { + days = conf->del_over_days; + } + + char line[1024] = {0}; + if (snprintf(line, sizeof(line), LOGROTATE_CONF, log_path, days, str_compress) < 0) { + ULOG_ERR(g_log, "Setting content of logratote is failure"); + goto END; + } + + + fp = fopen(LOG_LOGRATATE_FILE_NAME, "w"); + if (fp == NULL) { + ULOG_ERR(g_log, "Opening logratate file:%s is failure:%s", LOG_CONF_FILE_NAME, strerror(errno)); + goto END; + } + + if (fputs(line, fp) == EOF) { + ULOG_ERR(g_log, "Configure file of logratate which is written is failure:%s", strerror(errno)); + goto END; + } + + ret = 0; +END: + if (fp != NULL) { + fclose(fp); + } + + return ret; +} + +static int conf_log_file(const log_file_t *conf) +{ + int ret = 1; + u32 max_level = MAX_LOG_LEVEL_VALUE; + if (conf->level > max_level) { + ULOG_WARNING(g_log, "Configure log level:%u more than max value:%u", conf->level, max_level); + return 1; + } + + char path[sizeof(conf->path)]; + if (strlen(conf->path) > 0) { + strncpy(path, conf->path, sizeof(path)); + } else { + strncpy(path, DEFAULT_LOG_FILE, sizeof(path)); + } + + if (write_log_conf(conf->level, LOG_CONF_PATH, LOG_CONF_FILE_NAME, path, NULL) != 0) { + ULOG_ERR(g_log, "Log configure which is written is failure"); + return 1; + } + + /********** logrotate **********/ + if (write_logratate_conf(conf, path) != 0) { + ULOG_ERR(g_log, "Logratate configure which is written is failure"); + return 1; + } + + + if (conf->del_over_size > 0) { + } + + return ret; +} + +void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data) +{ + u32 need_len = sizeof(log_file_t); + if (input_len < need_len) { + ULOG_WARNING(g_log, + "The input paramter of rpc log file is needed length of %u, but the actual length is %u", + need_len, input_len); + return; + } + + conf_log_file((const log_file_t *)input); + //rpc_return_null(conn); +} + diff --git a/Platform/user/ulog/log-sched/log_file.h b/Platform/user/ulog/log-sched/log_file.h new file mode 100755 index 000000000..087b2c16c --- /dev/null +++ b/Platform/user/ulog/log-sched/log_file.h @@ -0,0 +1,29 @@ +#ifndef __LOG_H +#define __LOG_H + +#include +#include + +#include "common_types.h" +#include "rpc_common.h" +#include "log_common.h" + +#define MAX_LOG_PATH MAX_LINE_SIZE + +typedef enum { + LOG_UNCOMPRESS = 0, + LOG_COMPRESS +} log_compress_t; + +typedef struct _log_file { + u8 level; + char path[MAX_LOG_PATH]; + log_compress_t is_compress; + u32 del_over_days; + u64 del_over_size; +} log_file_t; + +void rpc_conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data); + + +#endif diff --git a/Platform/user/ulog/log-sched/log_sched.c b/Platform/user/ulog/log-sched/log_sched.c new file mode 100755 index 000000000..5e284b618 --- /dev/null +++ b/Platform/user/ulog/log-sched/log_sched.c @@ -0,0 +1,72 @@ +#include +#include +#include +#include + +#include "rpc_server.h" +#include "ulog_api.h" +#include "log_file.h" +#include "log_console.h" +#include "rpc_module.h" + + +#define LOG_SCHED_MODULE_NAME "log-sched" +#define SERVICE_LOG_FILE_NAME "log-file" + +ulog_t *g_log = NULL; + + +int main(int argc, char **argv) +{ + int run_daemon = 0; + char *options = "d"; + int opt; + + while ((opt = getopt(argc, argv, options)) != -1) { + switch (opt) { + case 'd': + run_daemon = 1; + break; + } + } + + g_log = ulog_init(LOG_SCHED_MODULE_NAME, !run_daemon); + if (g_log == NULL) { + fprintf(stderr, "Initiating ulog is failure"); + return 1; + } + + if (run_daemon) { + if (daemon(0, 0) == -1) { + ULOG_ERR(g_log, "Setting daemon running is failure:%s", strerror(errno)); + goto END; + } + } + + rpc_server *server = rpc_server_create_ex(RPC_MODULE_SYSLOG_NAME); + if (server == NULL) + { + ULOG_ERR(g_log, "start server error"); + return 1; + + } + ULOG_INFO(g_log, "Server of log schedule is started"); + + /* 注册配置处理函数 */ + rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", rpc_conf_log_file); + + log_file_t log = {0}; + log.is_compress = LOG_UNCOMPRESS; //LOG_COMPRESS; + log.del_over_days = 10; + log.level = LOG_DEBUG; + rpc_conf_log_file(NULL, &log, sizeof(log), NULL); + + log_console_t console = {0}; + strcpy(console.module_name, "111"); + console.level = LOG_DEBUG; + console.on = 1; + rpc_conf_log_console(NULL, &console, sizeof(console), NULL); +END: + ulog_close(g_log); + return 0; +} diff --git a/Platform/user/ulog/syslog-schedule/log_file.c b/Platform/user/ulog/syslog-schedule/log_file.c deleted file mode 100755 index c6780a43b..000000000 --- a/Platform/user/ulog/syslog-schedule/log_file.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "log_file.h" - -int conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data) -{ - return 0; -} - diff --git a/Platform/user/ulog/syslog-schedule/log_file.h b/Platform/user/ulog/syslog-schedule/log_file.h deleted file mode 100755 index c87bbf145..000000000 --- a/Platform/user/ulog/syslog-schedule/log_file.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __LOG_H -#define __LOG_H - -#include -#include "common_types.h" -#include "rpc_common.h" - - -typedef struct _log_file { - u8 level; - char path[PATH_MAX]; - u32 compress_over_days; - u32 del_over_days; - u64 compress_over_size; - u64 del_over_size; -} log_file_t; - -int conf_log_file(rpc_conn *conn, pointer input, int input_len, pointer data); - -#endif diff --git a/Platform/user/ulog/syslog-schedule/log_sched.c b/Platform/user/ulog/syslog-schedule/log_sched.c deleted file mode 100755 index 97031aaf4..000000000 --- a/Platform/user/ulog/syslog-schedule/log_sched.c +++ /dev/null @@ -1,41 +0,0 @@ -#include -#include - -#include "log_file.h" - -#define SYSLOG_SCHED_RPC_NAME "syslog_schedule_rpc" - -#define SERVICE_LOG_FILE_NAME "log_file" - -int main(int argc, char **argv) -{ - int run_daemon = 0; - char *options = "d"; - int opt; - - while ((opt = getopt(argc, argv, options)) != -1) { - switch (opt) { - case 'd': - run_daemon = 1; - break; - } - } - - if (run_daemon) { - if (daemon(0, 0) == -1) { - return 1; - } - } - - rpc_server *server = rpc_server_create_ex(SYSLOG_SCHED_RPC_NAME); - if (server = NULL) - { - printf("start server error\n"); - return 1; - - } - printf("Server of log schedule is started\n"); - - /* 注册配置处理函数 */ - rpc_server_regservice(server, SERVICE_LOG_FILE_NAME, "conf_log_file", conf_log_file); -} diff --git a/Platform/user/ulog/ulog-api/ulog_api.c b/Platform/user/ulog/ulog-api/ulog_api.c index fbf56e0e9..912752ede 100755 --- a/Platform/user/ulog/ulog-api/ulog_api.c +++ b/Platform/user/ulog/ulog-api/ulog_api.c @@ -1,14 +1,16 @@ #include +#include +#include "ulog.h" #include "common_types.h" #include "ulog_api.h" #define LOG_MSG_SZ 1024 -ulog_t *ulog_init(const char *module_name) +ulog_t *ulog_init(const char *module_name, u8 is_print) { ulog_t *log; - u32 len = sizeof(module_name); + u32 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); @@ -22,30 +24,35 @@ ulog_t *ulog_init(const char *module_name) } strncpy(log->module_name, module_name, len); + int opt = LOG_PERROR |LOG_PID; + if (is_print > 0) { + opt |= LOG_CONS; + } + openlog(log->module_name, opt, LOG_USER); + return log; } void ulog_close(ulog_t *log) { - if (log == NULL) { - return; + if (log != NULL) { + free(log); } - - free(log); + closelog(); } void ulog_record(const ulog_t *log, int level, const char *fmt, ...) { if (log == NULL) { - perror("Log is null"); + fprintf(stderr, "Log is null"); return; } char log_buf[LOG_MSG_SZ]; va_list args; va_start(args, fmt); - vsnprintf(log_buf, sizeof(log_buf), log_buf, args); + vsnprintf(log_buf, sizeof(log_buf), fmt, args); va_end(args); - syslog(level, "[%s] %s", log->module_name, log_buf); + syslog(level, MODULE_FMT" %s", log->module_name, log_buf); } diff --git a/Platform/user/ulog/ulog.h b/Platform/user/ulog/ulog.h new file mode 100755 index 000000000..044141af6 --- /dev/null +++ b/Platform/user/ulog/ulog.h @@ -0,0 +1,6 @@ +#ifndef _ULOG_H +#define _ULOG_H + +#define MODULE_FMT "[%s]" + +#endif