diff --git a/CMakeLists.txt b/CMakeLists.txt index 4bf3084b59f1888d79f39a6c4c2a60a1507693c1..0dce2af8af8a9eebd217b3fb9e2c4016337e7aca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,7 +39,6 @@ execute_process( ) message("-- commit id: " ${GIT_COMMIT_HASH}) -set(CMAKE_C_FLAGS "-fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror") set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread") set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie") diff --git a/cmake/set_build_flags.cmake b/cmake/set_build_flags.cmake index 1a803331d61c56d61346545774c7cd4a7f00c116..0469ba8c222084dcea9696618377d426c7ebce3d 100644 --- a/cmake/set_build_flags.cmake +++ b/cmake/set_build_flags.cmake @@ -4,6 +4,9 @@ set(CMAKE_CXX_FLAGS "-fPIC -std=c++11 -fstack-protector-all -D_FORTIFY_SOURCE=2 set(CMAKE_SHARED_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie -shared -pthread") set(CMAKE_EXE_LINKER_FLAGS "-Wl,-E -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -Wtrampolines -fPIE -pie") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'") + if (LCR_GCOV) set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage") set(CMAKE_CXX_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9abc297b0d65e948e27bcef6c040e8b7e991e3d9..1be0966adfb2bbe846ea7ca3124d2458d614870d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -38,13 +38,6 @@ target_include_directories(liblcr # set liblcr compile flags set_target_properties(liblcr PROPERTIES PREFIX "") -if (LCR_GCOV) - set(CMAKE_C_FLAGS_DEBUG "-Wall -fprofile-arcs -ftest-coverage") - message("------compile with gcov-------------") - message("-----CFLAGS: " ${CMAKE_C_FLAGS_DEBUG}) - message("------------------------------------") -endif() - target_link_libraries(liblcr ${check_libs}) if (CMAKE_TOOLCHAIN_FILE) diff --git a/src/lcrcontainer.c b/src/lcrcontainer.c index 619c4366899e6e85df94b6f10438c993866d9e1e..b849d23e17de11d3cbfa07cf8348a1d7962408c0 100644 --- a/src/lcrcontainer.c +++ b/src/lcrcontainer.c @@ -1968,7 +1968,7 @@ int lcr_log_init(const char *name, const char *file, const char *priority, const lconf.driver = "fifo"; lconf.priority = priority; } - if (log_enable(&lconf)) { + if (engine_log_enable(&lconf)) { fprintf(stderr, "Failed to init log"); goto out; } diff --git a/src/log.c b/src/log.c index 21b5bdcd71c6d6b240e74744ffe805982a95e740..a4204d15675b1e89788f342b1952fd420bbd4985 100644 --- a/src/log.c +++ b/src/log.c @@ -1,6 +1,6 @@ /****************************************************************************** - * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved. - * lcr licensed under the Mulan PSL v1. + * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved. + * engine licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. * You may obtain a copy of Mulan PSL v1 at: * http://license.coscl.org.cn/MulanPSL @@ -9,11 +9,12 @@ * PURPOSE. * See the Mulan PSL v1 for more details. * Author: wujing - * Create: 2018-11-08 + * Create: 2019-4-08 * Description: provide container log functions ******************************************************************************/ #define _GNU_SOURCE #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ + #include #include #include @@ -24,27 +25,13 @@ #include #include #include +#include -#include "log.h" #include "utils.h" +#include "log.h" const char * const g_engine_log_prio_name[] = { - "FATAL", "ALERT", "CRIT", "ERROR", "WARN", - "NOTICE", "INFO", "DEBUG", "TRACE" -}; - -/* predefined priorities. */ -enum engine_log_priority { - LOG_PRIORITY_FATAL = LOG_EMERG, - LOG_PRIORITY_ALERT = LOG_ALERT, - LOG_PRIORITY_CRIT = LOG_CRIT, - LOG_PRIORITY_ERROR = LOG_ERR, - LOG_PRIORITY_WARN = LOG_WARNING, - LOG_PRIORITY_NOTICE = LOG_NOTICE, - LOG_PRIORITY_INFO = LOG_INFO, - LOG_PRIORITY_DEBUG = LOG_DEBUG, - LOG_PRIORITY_TRACE, - LOG_PRIORITY_MAX + "FATAL", "ALERT", "CRIT", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG", "TRACE" }; #define MAX_MSG_LENGTH 4096 @@ -52,9 +39,8 @@ enum engine_log_priority { static __thread char *g_engine_log_prefix = NULL; -static char *g_engine_log_vmname = NULL; -static bool g_engine_log_quiet = false; -static int g_engine_log_level = LOG_PRIORITY_DEBUG; +static char *g_engine_log_module = NULL; +static int g_engine_log_level = ENGINE_LOG_DEBUG; static int g_engine_log_driver = LOG_DRIVER_STDOUT; int g_engine_log_fd = -1; @@ -66,34 +52,34 @@ void engine_set_log_prefix(const char *prefix) } free(g_engine_log_prefix); - g_engine_log_prefix = util_strdup_s(prefix); } /* engine free log prefix */ -void engine_free_log_prefix() +void engine_free_log_prefix(void) { free(g_engine_log_prefix); - g_engine_log_prefix = NULL; } -ssize_t write_nointr(int fd, const void *buf, size_t count); +static ssize_t write_nointr(int fd, const void *buf, size_t count); -void log_append_logfile(const struct engine_log_event *event, const char *timestamp, const char *msg); -void log_append_stderr(const struct engine_log_event *event, const char *timestamp, const char *msg); -int engine_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *time); +static void do_fifo_log(const struct engine_log_object_metadata *metadata, const char *timestamp, + const char *msg); + +static void do_stderr_log(const struct engine_log_object_metadata *metadata, const char *timestamp, + const char *msg); /* engine change str logdriver to enum */ -int engine_change_str_logdriver_to_enum(const char *driver) +static int engine_change_str_logdriver_to_enum(const char *driver) { if (driver == NULL) { return LOG_DRIVER_NOSET; } - if (!strcasecmp(driver, "stdout")) { + if (strcasecmp(driver, "stdout") == 0) { return LOG_DRIVER_STDOUT; } - if (!strcasecmp(driver, "fifo")) { + if (strcasecmp(driver, "fifo") == 0) { return LOG_DRIVER_FIFO; } @@ -103,25 +89,25 @@ int engine_change_str_logdriver_to_enum(const char *driver) #define LOG_FIFO_SIZE (1024 * 1024) /* open fifo */ -int open_fifo(const char *fifo_path) +static int open_fifo(const char *fifo_path) { int nret; int fifo_fd = -1; nret = mknod(fifo_path, S_IFIFO | S_IRUSR | S_IWUSR, (dev_t)0); if (nret && errno != EEXIST) { - printf("Mknod failed: %s\n", strerror(errno)); + COMMAND_ERROR("Mknod failed: %s", strerror(errno)); return nret; } fifo_fd = util_open(fifo_path, O_RDWR | O_NONBLOCK, 0); if (fifo_fd == -1) { - fprintf(stderr, "Open fifo %s failed: %s\n", fifo_path, strerror(errno)); + COMMAND_ERROR("Open fifo %s failed: %s", fifo_path, strerror(errno)); return -1; } if (fcntl(fifo_fd, F_SETPIPE_SZ, LOG_FIFO_SIZE) == -1) { - fprintf(stderr, "Set fifo buffer size failed: %s", strerror(errno)); + COMMAND_ERROR("Set fifo buffer size failed: %s", strerror(errno)); close(fifo_fd); return -1; } @@ -129,91 +115,93 @@ int open_fifo(const char *fifo_path) return fifo_fd; } -/* engine close log file */ -void engine_close_log_file() -{ - if (g_engine_log_fd != -1) { - close(g_engine_log_fd); - g_engine_log_fd = -1; - } -} - /* init log driver */ static int init_log_driver(const struct engine_log_config *log) { int i, driver; - for (i = LOG_PRIORITY_FATAL; i < LOG_PRIORITY_MAX; i++) { - if (!strcasecmp(g_engine_log_prio_name[i], log->priority)) { + for (i = ENGINE_LOG_FATAL; i < ENGINE_LOG_MAX; i++) { + if (strcasecmp(g_engine_log_prio_name[i], log->priority) == 0) { g_engine_log_level = i; break; } } - if (i == LOG_PRIORITY_MAX) { - fprintf(stderr, "Unable to parse logging level:%s\n", log->priority); + if (i == ENGINE_LOG_MAX) { + COMMAND_ERROR("Unable to parse logging level:%s", log->priority); return -1; } driver = engine_change_str_logdriver_to_enum(log->driver); if (driver < 0) { - fprintf(stderr, "Invalid log driver: %s\n", log->driver); + COMMAND_ERROR("Invalid log driver: %s", log->driver); return -1; } g_engine_log_driver = driver; return 0; } -/* log enable */ -int log_enable(const struct engine_log_config *log) +static inline bool check_log_config_args(const struct engine_log_config *log) { - int nret = 0; - char *full_path = NULL; + return (log == NULL || log->name == NULL || log->priority == NULL); +} - if ((log->name == NULL) || (log->priority == NULL)) { +static int do_check_log_configs(const struct engine_log_config *log) +{ + bool invalid_arg = false; + + if (check_log_config_args(log)) { + COMMAND_ERROR("Invalid arguments"); return -1; } + invalid_arg = ((log->file == NULL) && (g_engine_log_driver == LOG_DRIVER_FIFO)); + if (invalid_arg) { + COMMAND_ERROR("Must set log file for driver %s", log->driver); + return -1; + } + return 0; +} + +/* log enable */ +int engine_log_enable(const struct engine_log_config *log) +{ + int nret = 0; + char *full_path = NULL; + if (g_engine_log_fd != -1) { - fprintf(stderr, "engine log already initialized\n"); + COMMAND_ERROR("engine log already initialized"); return 0; } - if (init_log_driver(log)) { + + nret = do_check_log_configs(log); + if (nret != 0) { return -1; } - free(g_engine_log_vmname); - - g_engine_log_vmname = util_strdup_s(log->name); + nret = init_log_driver(log); + if (nret != 0) { + return -1; + } - g_engine_log_quiet = log->quiet; + free(g_engine_log_module); + g_engine_log_module = util_strdup_s(log->name); - if ((log->file == NULL) || strcmp(log->file, "none") == 0) { - if (g_engine_log_driver == LOG_DRIVER_FIFO) { - fprintf(stderr, "Must set log file for driver %s\n", log->driver); - nret = -1; - } - goto out; - } full_path = util_strdup_s(log->file); - if (full_path == NULL) { - fprintf(stderr, "Out of memory\n"); - nret = -1; - goto out; - } - if (util_build_dir(full_path)) { - fprintf(stderr, "failed to create dir for log file\n"); - nret = -1; + nret = util_build_dir(full_path); + if (nret != 0) { + COMMAND_ERROR("failed to create dir for log file"); goto out; } - g_engine_log_fd = open_fifo(full_path); + g_engine_log_fd = open_fifo(full_path); if (g_engine_log_fd == -1) { nret = -1; } + out: - if (nret) { + if (nret != 0) { if (g_engine_log_driver == LOG_DRIVER_FIFO) { g_engine_log_driver = LOG_DRIVER_NOSET; } @@ -223,59 +211,110 @@ out: return nret; } -/* engine log append */ -int engine_log_append(const struct engine_log_event *event, const char *format, ...) +static int do_log_by_driver(const struct engine_log_object_metadata *metadata, const char *msg, + const char *date_time) +{ + switch (g_engine_log_driver) { + case LOG_DRIVER_STDOUT: + do_stderr_log(metadata, date_time, msg); + break; + case LOG_DRIVER_FIFO: + if (g_engine_log_fd == -1) { + COMMAND_ERROR("Do not set log file\n"); + return -1; + } + do_fifo_log(metadata, date_time, msg); + break; + case LOG_DRIVER_NOSET: + break; + default: + COMMAND_ERROR("Invalid log driver\n"); + return -1; + } + return 0; +} + +static char *parse_timespec_to_human() +{ + struct timespec timestamp; + struct tm ptm = {0}; + char date_time[ENGINE_LOG_TIME_STR_MAX_LEN] = { 0 }; + int nret; +#define SEC_TO_NSEC 1000000 +#define FIRST_YEAR_OF_GMT 1900 + + if (clock_gettime(CLOCK_REALTIME, ×tamp) == -1) { + COMMAND_ERROR("Failed to get real time"); + return 0; + } + + if (localtime_r(&(timestamp.tv_sec), &ptm) == NULL) { + SYSERROR("Transfer timespec failed"); + return NULL; + } + + nret = snprintf(date_time, ENGINE_LOG_TIME_STR_MAX_LEN, "%04d%02d%02d%02d%02d%02d.%03ld", + ptm.tm_year + FIRST_YEAR_OF_GMT, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, ptm.tm_sec, + timestamp.tv_nsec / SEC_TO_NSEC); + + if (nret < 0 || nret >= ENGINE_LOG_TIME_STR_MAX_LEN) { + COMMAND_ERROR("Sprintf failed"); + return NULL; + } + + return util_strdup_s(date_time); +} + +/* use to add log to driver */ +int engine_log(const struct engine_log_object_metadata *metadata, const char *format, ...) { int rc; - va_list args; char msg[MAX_MSG_LENGTH] = { 0 }; - char date_time[ENGINE_LOG_TIME_SIZE] = { 0 }; - struct timespec timestamp; + va_list args; + char *date_time = NULL; + int ret = 0; va_start(args, format); - rc = vsprintf(msg, format, args); + rc = vsnprintf(msg, MAX_MSG_LENGTH, format, args); va_end(args); if (rc < 0) { rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "!!LONG LONG A LOG!!"); - if (rc < 0 || rc >= MAX_MSG_LENGTH) { + if (rc < 0) { return 0; } } - if (clock_gettime(CLOCK_REALTIME, ×tamp) == -1) { - fprintf(stderr, "Failed to get real time\n"); - return 0; - } - if (engine_unix_trans_to_utc(date_time, ENGINE_LOG_TIME_SIZE, ×tamp) < 0) { - return 0; + date_time = parse_timespec_to_human(); + if (date_time == NULL) { + goto out; } - switch (g_engine_log_driver) { - case LOG_DRIVER_STDOUT: - if (g_engine_log_quiet) { - break; - } - log_append_stderr(event, date_time, msg); - break; - case LOG_DRIVER_FIFO: - if (g_engine_log_fd == -1) { - fprintf(stderr, "Do not set log file\n"); - return -1; - } - log_append_logfile(event, date_time, msg); - break; - case LOG_DRIVER_NOSET: - break; - default: - fprintf(stderr, "Invalid log driver\n"); - return -1; + ret = do_log_by_driver(metadata, msg, date_time); + +out: + free(date_time); + return ret; +} + +static void do_write_log_into_file(int log_fd, char *log_msg, size_t max_len, size_t write_size) +{ + size_t size = 0; + + size = write_size; + if (size > (max_len - 1)) { + size = max_len - 1; } - return 0; + log_msg[size] = '\n'; + + if (write_nointr(log_fd, log_msg, (size + 1)) == -1) { + COMMAND_ERROR("write log into logfile failed"); + } } /* log append logfile */ -void log_append_logfile(const struct engine_log_event *event, const char *timestamp, const char *msg) +static void do_fifo_log(const struct engine_log_object_metadata *metadata, const char *timestamp, + const char *msg) { char log_buffer[ENGINE_LOG_BUFFER_SIZE] = { 0 }; int log_fd = -1; @@ -283,7 +322,7 @@ void log_append_logfile(const struct engine_log_event *event, const char *timest size_t size; char *tmp_prefix = NULL; - if (event->priority > g_engine_log_level) { + if (metadata->level > g_engine_log_level) { return; } log_fd = g_engine_log_fd; @@ -291,131 +330,47 @@ void log_append_logfile(const struct engine_log_event *event, const char *timest return; } - tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_vmname; + tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_module; if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) { tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH); } - nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "", - timestamp, g_engine_log_prio_name[event->priority], - g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, event->locinfo->func, - event->locinfo->line, msg); + nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s:%s:%d - %s", tmp_prefix ? tmp_prefix : "", + timestamp, g_engine_log_prio_name[metadata->level], metadata->file, + metadata->func, metadata->line, msg); if (nret < 0) { - nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s", - tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority], - g_engine_log_vmname ? g_engine_log_vmname : "engine", event->locinfo->file, - event->locinfo->func, event->locinfo->line, "Large log message"); - if (nret < 0 || (size_t)nret >= sizeof(log_buffer)) { + nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s:%s:%d - %s", + tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[metadata->level], + metadata->file, metadata->func, metadata->line, "Large log message"); + if (nret < 0) { return; } } size = (size_t)nret; - if (size > (sizeof(log_buffer) - 1)) { - size = sizeof(log_buffer) - 1; - } - - log_buffer[size] = '\n'; - - if (write_nointr(log_fd, log_buffer, (size + 1)) == -1) { - fprintf(stderr, "write log into logfile failed"); - } + do_write_log_into_file(log_fd, log_buffer, sizeof(log_buffer), size); } /* log append stderr */ -void log_append_stderr(const struct engine_log_event *event, const char *timestamp, const char *msg) +static void do_stderr_log(const struct engine_log_object_metadata *metadata, const char *timestamp, + const char *msg) { char *tmp_prefix = NULL; - if (event->priority > g_engine_log_level) { + if (metadata->level > g_engine_log_level) { return; } - tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_vmname; + tmp_prefix = g_engine_log_prefix ? g_engine_log_prefix : g_engine_log_module; if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) { tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH); } - fprintf(stderr, "%15s %s %-8s ", tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[event->priority]); - fprintf(stderr, "%s - ", g_engine_log_vmname ? g_engine_log_vmname : "engine"); - fprintf(stderr, "%s:%s:%d - ", event->locinfo->file, event->locinfo->func, event->locinfo->line); - fprintf(stderr, "%s", msg); - fprintf(stderr, "\n"); -} - -/* engine unix trans to utc */ -int engine_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *time) -{ - int64_t trans_to_days, all_days, age, doa, yoa, doy, nom, hours_to_sec, trans_to_sec; - int64_t real_year, real_day, real_month, real_hours, real_minutes, real_seconds; - char ns[LCR_NUMSTRLEN64] = { 0 }; - int ret; - - /* Transtate seconds to number of days. */ - trans_to_days = time->tv_sec / 86400; - - /* Calculate days from 0000-03-01 to 1970-01-01.Days base it */ - all_days = trans_to_days + 719468; - - /* compute the age.One age means 400 years(146097 days) */ - age = (all_days >= 0 ? all_days : all_days - 146096) / 146097; - - /* The day-of-age (doa) can then be found by subtracting the genumber */ - doa = (all_days - age * 146097); - - /* Calculate year-of-age (yoa, range [0, 399]) */ - yoa = ((doa - doa / 1460) + (doa / 36524 - doa / 146096)) / 365; - - /* Compute the year this moment */ - real_year = yoa + age * 400; - - /* Calculate the day-of-year */ - doy = doa - (365 * yoa + yoa / 4 - yoa / 100); - - /* Compute the month number. */ - nom = (5 * doy + 2) / 153; - - /* Compute the real_day. */ - real_day = (doy - (153 * nom + 2) / 5) + 1; - - /* Compute the correct month. */ - real_month = nom + (nom < 10 ? 3 : -9); - - /* Add one year before March */ - if (real_month < 3) { - real_year++; - } - - /* Translate days in the age to seconds. */ - trans_to_sec = trans_to_days * 86400; - - /* Compute the real_hours */ - real_hours = (time->tv_sec - trans_to_sec) / 3600; - - /* Translate the real hours to seconds. */ - hours_to_sec = real_hours * 3600; - - /* Calculate the real minutes */ - real_minutes = ((time->tv_sec - trans_to_sec) - hours_to_sec) / 60; - - /* Calculate the real seconds */ - real_seconds = (((time->tv_sec - trans_to_sec) - hours_to_sec) - (real_minutes * 60)); - - ret = snprintf(ns, LCR_NUMSTRLEN64, "%ld", time->tv_nsec); - if (ret < 0 || ret >= LCR_NUMSTRLEN64) { - return -1; - } - - /* Create the final timestamp */ - ret = snprintf(buf, bufsize, "%" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 "%02" PRId64 ".%.3s", - real_year, real_month, real_day, real_hours, real_minutes, real_seconds, ns); - if (ret < 0 || (size_t)ret >= bufsize) { - return -1; - } - - return 0; + COMMAND_ERROR("%15s %s %-8s ", tmp_prefix ? tmp_prefix : "", timestamp, g_engine_log_prio_name[metadata->level]); + COMMAND_ERROR("%s:%s:%d - ", metadata->file, metadata->func, metadata->line); + COMMAND_ERROR("%s\n", msg); } /* write nointr */ -ssize_t write_nointr(int fd, const void *buf, size_t count) +static ssize_t write_nointr(int fd, const void *buf, size_t count) { ssize_t nret; for (;;) { @@ -428,3 +383,4 @@ ssize_t write_nointr(int fd, const void *buf, size_t count) } return nret; } + diff --git a/src/log.h b/src/log.h index 3581b221f423f1975c86f1c4d757bcb29c14dd5a..e41c78f719507c9532639f713cd6f2c4140f9a9a 100644 --- a/src/log.h +++ b/src/log.h @@ -1,6 +1,6 @@ /****************************************************************************** - * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved. - * lcr licensed under the Mulan PSL v1. + * Copyright (c) Huawei Technologies Co., Ltd. 2019. All rights reserved. + * engine licensed under the Mulan PSL v1. * You can use this software according to the terms and conditions of the Mulan PSL v1. * You may obtain a copy of Mulan PSL v1 at: * http://license.coscl.org.cn/MulanPSL @@ -9,17 +9,14 @@ * PURPOSE. * See the Mulan PSL v1 for more details. * Author: wujing - * Create: 2018-11-08 + * Create: 2019-4-08 * Description: provide container log functions ******************************************************************************/ -#ifndef __LCR_LOG_H -#define __LCR_LOG_H +#ifndef __ENGINE_LOG_H +#define __ENGINE_LOG_H -#include #include -#include #include -#include #ifdef __cplusplus extern "C" { @@ -29,151 +26,99 @@ extern "C" { #define O_CLOEXEC 02000000 #endif -#ifndef F_DUPFD_CLOEXEC -#define F_DUPFD_CLOEXEC 1030 -#endif - #define ENGINE_LOG_BUFFER_SIZE 4096 -/* We're logging in seconds and nanoseconds. Assuming that the underlying - * datatype is currently at maximum a 64bit integer, we have a date string that - * is of maximum length (2^64 - 1) * 2 = (21 + 21) = 42. - * */ -#define ENGINE_LOG_TIME_SIZE 42 - -/* errmsg that defined in iSulad */ -#define DAEMON_ERROR_GRPC_INIT_STR "Init failed" -#define DAEMON_ERROR_GRPC_CONNENCT_STR "Can not connect with server.Is the docker dameon running on the host?" -#define DAEMON_ERROR_GRPC_SERVER_STR "Server internal error" +#define ENGINE_LOG_TIME_STR_MAX_LEN 21 -enum g_engine_log_driver { +enum engine_log_drivers { LOG_DRIVER_STDOUT, LOG_DRIVER_FIFO, - LOG_DRIVER_SYSLOG, LOG_DRIVER_NOSET, }; +enum engine_log_level { + ENGINE_LOG_FATAL = 0, + ENGINE_LOG_ALERT, + ENGINE_LOG_CRIT, + ENGINE_LOG_ERROR, + ENGINE_LOG_WARN, + ENGINE_LOG_NOTICE, + ENGINE_LOG_INFO, + ENGINE_LOG_DEBUG, + ENGINE_LOG_TRACE, + ENGINE_LOG_MAX +}; + struct engine_log_config { const char *name; const char *file; const char *priority; const char *prefix; const char *driver; - bool quiet; }; -/* location information of the logging event */ -struct engine_log_locinfo { +/* brief logging event object */ +struct engine_log_object_metadata { + /* location information of the logging item */ const char *file; const char *func; int line; -}; - -#define ENGINE_LOG_LOCINFO_INIT \ - { \ - .file = __FILE__, .func = __func__, .line = __LINE__, \ - } -/* brief logging event object */ -struct engine_log_event { - int priority; - struct engine_log_locinfo *locinfo; + int level; }; -extern void engine_close_log_file(); -int log_enable(const struct engine_log_config *log); -void engine_set_log_prefix(const char *prefix); +int engine_log_enable(const struct engine_log_config *log); -void engine_free_log_prefix(); +void engine_set_log_prefix(const char *prefix); -int engine_change_str_logdriver_to_enum(const char *driver); +void engine_free_log_prefix(void); -int engine_log_append(const struct engine_log_event *event, const char *format, ...); +int engine_log(const struct engine_log_object_metadata *metadata, const char *format, ...); -#define DEBUG(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_DEBUG; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ +#define COMMON_LOG(loglevel, format, ...) \ + do { \ + struct engine_log_object_metadata meta = { \ + .file = __FILENAME__, .func = __func__, .line = __LINE__, .level = loglevel, \ + }; \ + (void)engine_log(&meta, format, ##__VA_ARGS__); \ } while (0) -#define INFO(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_INFO; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define DEBUG(format, ...) \ + COMMON_LOG(ENGINE_LOG_DEBUG, format, ##__VA_ARGS__) -#define NOTICE(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_NOTICE; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define INFO(format, ...) \ + COMMON_LOG(ENGINE_LOG_INFO, format, ##__VA_ARGS__) -#define WARN(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_WARNING; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define NOTICE(format, ...) \ + COMMON_LOG(ENGINE_LOG_NOTICE, format, ##__VA_ARGS__) -#define ERROR(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_ERR; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define WARN(format, ...) \ + COMMON_LOG(ENGINE_LOG_WARN, format, ##__VA_ARGS__) -#define CRIT(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_CRIT; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define ERROR(format, ...) \ + COMMON_LOG(ENGINE_LOG_ERROR, format, ##__VA_ARGS__) -#define ALERT(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_ALERT; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define CRIT(format, ...) \ + COMMON_LOG(ENGINE_LOG_CRIT, format, ##__VA_ARGS__) -#define FATAL(format, ...) \ - do { \ - struct engine_log_locinfo locinfo = ENGINE_LOG_LOCINFO_INIT; \ - struct engine_log_event event; \ - event.locinfo = &locinfo; \ - event.priority = LOG_EMERG; \ - (void)engine_log_append(&event, format, ##__VA_ARGS__); \ - } while (0) +#define ALERT(format, ...) \ + COMMON_LOG(ENGINE_LOG_ALERT, format, ##__VA_ARGS__) + +#define FATAL(format, ...) \ + COMMON_LOG(ENGINE_LOG_FATAL, format, ##__VA_ARGS__) #define SYSERROR(format, ...) \ do { \ ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \ } while (0) -#define COMMAND_ERROR(fmt, args...) \ - do { \ - (void)fprintf(stderr, fmt "\n", ##args); \ +#define COMMAND_ERROR(fmt, args...) \ + do { \ + (void)fprintf(stderr, fmt "\n", ##args); \ } while (0) #ifdef __cplusplus } #endif -#endif /* __LCR_LOG_H */ +#endif /* __ENGINE_LOG_H */