提交 adffcc20 编写于 作者: L LiuHao

iSulad: refactor log

Signed-off-by: NLiuHao <liuhao27@huawei.com>
上级 4807f2d3
# set common FLAGS
set(CMAKE_C_FLAGS "-fPIC -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror -rdynamic")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
if (GRPC_CONNECTOR OR ENABLE_OCI_IMAGE)
set(CMAKE_CXX_FLAGS "-fPIC -std=c++11 -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 -Wall -Werror")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__FILENAME__='\"$(subst ${CMAKE_SOURCE_DIR}/,,$(abspath $<))\"'")
endif()
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")
......
......@@ -32,27 +32,14 @@ const char * const g_log_prio_name[] = {
"FATAL", "ALERT", "CRIT", "ERROR", "WARN", "NOTICE", "INFO", "DEBUG", "TRACE"
};
/* predefined priorities. */
enum 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
};
#define MAX_MSG_LENGTH 4096
#define MAX_LOG_PREFIX_LENGTH 15
static __thread char *g_log_prefix = NULL;
static char *g_log_vmname = NULL;
static bool g_log_quiet = false;
static int g_log_level = LOG_PRIORITY_DEBUG;
static char *g_log_module = NULL;
static int g_log_level = ISULA_LOG_DEBUG;
static int g_log_driver = LOG_DRIVER_STDOUT;
int g_lcrd_log_fd = -1;
......@@ -70,8 +57,8 @@ void set_log_prefix(const char *prefix)
void set_default_command_log_config(const char *name, struct log_config *log)
{
log->name = name;
log->quiet = true;
log->name = name;
log->file = NULL;
log->priority = "DEBUG";
log->driver = "stdout";
......@@ -86,8 +73,7 @@ void free_log_prefix()
g_log_prefix = NULL;
}
/* write nointr */
static ssize_t write_nointr(int fd, const void *buf, size_t count)
static ssize_t isulad_save_log(int fd, const void *buf, size_t count)
{
ssize_t nret = 0;
......@@ -102,10 +88,8 @@ static ssize_t write_nointr(int fd, const void *buf, size_t count)
return nret;
}
void log_append_logfile(const struct log_event *event, const char *timestamp, const char *msg);
void log_append_stderr(const struct log_event *event, const char *timestamp, const char *msg);
int lcrd_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *time);
void do_fifo_log(const struct log_object_metadata *meta, const char *timestamp, const char *msg);
void do_stderr_log(const struct log_object_metadata *meta, const char *timestamp, const char *msg);
/* change str logdriver to enum */
int change_str_logdriver_to_enum(const char *driver)
......@@ -164,14 +148,14 @@ static int log_init_checker(const struct log_config *log)
return -1;
}
for (i = LOG_PRIORITY_FATAL; i < LOG_PRIORITY_MAX; i++) {
for (i = ISULA_LOG_FATAL; i < ISULA_LOG_MAX; i++) {
if (strcasecmp(g_log_prio_name[i], log->priority) == 0) {
g_log_level = i;
break;
}
}
if (i == LOG_PRIORITY_MAX) {
if (i == ISULA_LOG_MAX) {
fprintf(stderr, "Unable to parse logging level:%s\n", log->priority);
return -1;
}
......@@ -200,12 +184,11 @@ int log_init(struct log_config *log)
return -1;
}
free(g_log_vmname);
g_log_vmname = util_strdup_s(log->name);
free(g_log_module);
g_log_module = util_strdup_s(log->name);
g_log_quiet = log->quiet;
if (log->file == NULL || strcmp(log->file, "none") == 0) {
if (log->file == NULL) {
if (g_log_driver == LOG_DRIVER_FIFO) {
fprintf(stderr, "Must set log file if driver is %s\n", log->driver);
nret = -1;
......@@ -233,59 +216,93 @@ out:
return nret;
}
/* log append */
int log_append(const struct log_event *event, const char *format, ...)
static char *parse_timespec_to_human()
{
int rc = 0;
va_list args;
char msg[MAX_MSG_LENGTH] = { 0 };
char date_time[LCRD_LOG_TIME_SIZE] = { 0 };
struct timespec timestamp;
struct tm ptm = {0};
char date_time[LCRD_LOG_TIME_MAX_LEN] = { 0 };
int nret;
#define SEC_TO_NSEC 1000000
#define FIRST_YEAR_OF_GMT 1900
va_start(args, format);
rc = vsnprintf(msg, MAX_MSG_LENGTH, format, args);
va_end(args);
if (rc < 0) {
rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "Failed to truncate print error log");
if (rc < 0 || (size_t)rc >= MAX_MSG_LENGTH) {
return 0;
}
if (clock_gettime(CLOCK_REALTIME, &timestamp) == -1) {
COMMAND_ERROR("Failed to get real time");
return 0;
}
if (clock_gettime(CLOCK_REALTIME, &timestamp) == -1) {
fprintf(stderr, "Failed to get real time");
return -1;
if (localtime_r(&(timestamp.tv_sec), &ptm) == NULL) {
SYSERROR("Transfer timespec failed");
return NULL;
}
if (lcrd_unix_trans_to_utc(date_time, LCRD_LOG_TIME_SIZE, &timestamp) < 0) {
return 0;
nret = snprintf(date_time, LCRD_LOG_TIME_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 >= LCRD_LOG_TIME_MAX_LEN) {
COMMAND_ERROR("Sprintf failed");
return NULL;
}
return util_strdup_s(date_time);
}
static int do_log_by_driver(const struct log_object_metadata *meta, const char *msg, const char *date_time)
{
switch (g_log_driver) {
case LOG_DRIVER_STDOUT:
if (g_log_quiet) {
break;
}
log_append_stderr(event, date_time, msg);
do_stderr_log(meta, date_time, msg);
break;
case LOG_DRIVER_FIFO:
if (g_lcrd_log_fd == -1) {
fprintf(stderr, "Do not set log file\n");
return -1;
}
log_append_logfile(event, date_time, msg);
do_fifo_log(meta, date_time, msg);
break;
case LOG_DRIVER_NOSET:
break;
default:
fprintf(stderr, "Invalid log driver\n");
COMMAND_ERROR("Invalid log driver");
return -1;
}
return 0;
}
/* log append logfile */
void log_append_logfile(const struct log_event *event, const char *timestamp, const char *msg)
int new_log(const struct log_object_metadata *meta, const char *format, ...)
{
int rc = 0;
int ret = 0;
va_list args;
char msg[MAX_MSG_LENGTH] = { 0 };
char *date_time = NULL;
va_start(args, format);
rc = vsnprintf(msg, MAX_MSG_LENGTH, format, args);
va_end(args);
if (rc < 0) {
rc = snprintf(msg, MAX_MSG_LENGTH, "%s", "Failed to truncate print error log");
if (rc < 0 || (size_t)rc >= MAX_MSG_LENGTH) {
return 0;
}
}
date_time = parse_timespec_to_human();
if (date_time == NULL) {
goto out;
}
ret = do_log_by_driver(meta, msg, date_time);
out:
free(date_time);
return ret;
}
void do_fifo_log(const struct log_object_metadata *meta, const char *timestamp, const char *msg)
{
int log_fd = -1;
int nret = 0;
......@@ -293,7 +310,7 @@ void log_append_logfile(const struct log_event *event, const char *timestamp, co
char *tmp_prefix = NULL;
char log_buffer[LCRD_LOG_BUFFER_SIZE] = { 0 };
if (event == NULL || event->priority > g_log_level) {
if (meta == NULL || meta->level > g_log_level) {
return;
}
log_fd = g_lcrd_log_fd;
......@@ -301,15 +318,14 @@ void log_append_logfile(const struct log_event *event, const char *timestamp, co
return;
}
tmp_prefix = g_log_prefix != NULL ? g_log_prefix : g_log_vmname;
if (tmp_prefix != NULL && strlen(tmp_prefix) > 15) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - 15);
tmp_prefix = g_log_prefix != NULL ? g_log_prefix : g_log_module;
if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH);
}
if (event->locinfo != NULL) {
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s - %s:%s:%d - %s",
tmp_prefix ? tmp_prefix : "", timestamp, g_log_prio_name[event->priority],
g_log_vmname ? g_log_vmname : "lcrd", event->locinfo->file, event->locinfo->func,
event->locinfo->line, msg);
if (meta->file != NULL) {
nret = snprintf(log_buffer, sizeof(log_buffer), "%15s %s %-8s %s:%s:%d - %s",
tmp_prefix ? tmp_prefix : "", timestamp, g_log_prio_name[meta->level],
meta->file, meta->func, meta->line, msg);
} else {
nret = snprintf(log_buffer, sizeof(log_buffer), "%s %s", timestamp, msg);
}
......@@ -325,121 +341,34 @@ void log_append_logfile(const struct log_event *event, const char *timestamp, co
log_buffer[size] = '\n';
if (write_nointr(log_fd, log_buffer, (size + 1)) == -1) {
fprintf(stderr, "write log into logfile failed");
if (isulad_save_log(log_fd, log_buffer, (size + 1)) == -1) {
COMMAND_ERROR("Write log into logfile failed");
}
}
/* log append stderr */
void log_append_stderr(const struct log_event *event, const char *timestamp, const char *msg)
void do_stderr_log(const struct log_object_metadata *meta, const char *timestamp, const char *msg)
{
char *tmp_prefix = NULL;
if (event == NULL || event->priority > g_log_level) {
if (meta == NULL || meta->level > g_log_level) {
return;
}
tmp_prefix = g_log_prefix ? g_log_prefix : g_log_vmname;
if (tmp_prefix != NULL && strlen(tmp_prefix) > 15) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - 15);
tmp_prefix = g_log_prefix ? g_log_prefix : g_log_module;
if (tmp_prefix != NULL && strlen(tmp_prefix) > MAX_LOG_PREFIX_LENGTH) {
tmp_prefix = tmp_prefix + (strlen(tmp_prefix) - MAX_LOG_PREFIX_LENGTH);
}
if (event->locinfo != NULL) {
if (meta->file != NULL) {
fprintf(stderr, "%15s ", tmp_prefix ? tmp_prefix : "");
}
fprintf(stderr, "%s ", timestamp);
if (event->locinfo != NULL) {
fprintf(stderr, "%-8s ", g_log_prio_name[event->priority]);
fprintf(stderr, "%s - ", g_log_vmname ? g_log_vmname : "lcrd");
fprintf(stderr, "%s:%s:%d - ", event->locinfo->file, event->locinfo->func, event->locinfo->line);
if (meta->file != NULL) {
fprintf(stderr, "%-8s ", g_log_prio_name[meta->level]);
fprintf(stderr, "%s:%s:%d - ", meta->file, meta->func, meta->line);
}
fprintf(stderr, "%s", msg);
fprintf(stderr, "\n");
}
/* lcrd unix trans to utc */
int lcrd_unix_trans_to_utc(char *buf, size_t bufsize, const struct timespec *time)
{
int ret = 0;
int64_t trans_to_days = 0;
int64_t all_days = 0;
int64_t age = 0;
int64_t doa = 0;
int64_t yoa = 0;
int64_t real_year = 0;
int64_t doy = 0;
int64_t nom = 0;
int64_t real_day = 0;
int64_t real_month = 0;
int64_t trans_to_sec = 0;
int64_t real_hours = 0;
int64_t hours_to_sec = 0;
int64_t real_minutes = 0;
int64_t real_seconds = 0;
char ns[LCRD_NUMSTRLEN64] = { 0 };
/* 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, LCRD_NUMSTRLEN64, "%ld", time->tv_nsec);
if (ret < 0 || (size_t)ret >= LCRD_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;
}
......@@ -15,11 +15,9 @@
#ifndef __LCRD_LOG_H
#define __LCRD_LOG_H
#include <syslog.h>
#include <stdbool.h>
#include <errno.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#ifdef __cplusplus
extern "C" {
......@@ -29,46 +27,48 @@ extern "C" {
#define O_CLOEXEC 02000000
#endif
#ifndef F_DUPFD_CLOEXEC
#define F_DUPFD_CLOEXEC 1030
#endif
#define LCRD_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 LCRD_LOG_TIME_SIZE 42
#define LCRD_LOG_TIME_MAX_LEN 21
enum g_log_driver {
LOG_DRIVER_STDOUT,
LOG_DRIVER_FIFO,
LOG_DRIVER_NOSET,
};
enum g_log_driver { LOG_DRIVER_STDOUT, LOG_DRIVER_FIFO, LOG_DRIVER_SYSLOG, LOG_DRIVER_NOSET };
enum isula_log_level {
ISULA_LOG_FATAL = 0,
ISULA_LOG_ALERT,
ISULA_LOG_CRIT,
ISULA_LOG_ERROR,
ISULA_LOG_WARN,
ISULA_LOG_NOTICE,
ISULA_LOG_INFO,
ISULA_LOG_DEBUG,
ISULA_LOG_TRACE,
ISULA_LOG_MAX
};
struct log_config {
bool quiet;
const char *name;
const char *file;
const char *priority;
const char *prefix;
const char *driver;
bool quiet;
};
/* location information of the logging event */
struct log_locinfo {
/* brief logging event object */
struct log_object_metadata {
/* location information of the logging item */
const char *file;
const char *func;
int line;
};
#define LOG_LOCINFO_INIT \
{ \
.file = __FILE__, .func = __func__, .line = __LINE__, \
}
/* brief logging event object */
struct log_event {
int priority;
struct log_locinfo *locinfo;
int level;
};
int log_init(struct log_config *log);
void set_default_command_log_config(const char *name, struct log_config *log);
......@@ -79,87 +79,47 @@ void free_log_prefix();
int change_str_logdriver_to_enum(const char *driver);
int log_append(const struct log_event *event, const char *format, ...);
int new_log(const struct log_object_metadata *meta, const char *format, ...);
#define DEBUG(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_DEBUG; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
#define COMMON_LOG(loglevel, format, ...) \
do { \
struct log_object_metadata meta = { \
.file = __FILENAME__, .func = __func__, .line = __LINE__, .level = loglevel, \
}; \
(void)new_log(&meta, format, ##__VA_ARGS__); \
} while (0)
#define DEBUG(format, ...) \
COMMON_LOG(ISULA_LOG_DEBUG, format, ##__VA_ARGS__)
#define INFO(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_INFO; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
COMMON_LOG(ISULA_LOG_INFO, format, ##__VA_ARGS__)
#define NOTICE(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_NOTICE; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
COMMON_LOG(ISULA_LOG_NOTICE, format, ##__VA_ARGS__)
#define WARN(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_WARNING; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
COMMON_LOG(ISULA_LOG_WARN, format, ##__VA_ARGS__)
#define ERROR(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_ERR; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
#define EVENT(format, ...) \
do { \
struct log_event append_log_event; \
append_log_event.locinfo = NULL; \
append_log_event.priority = LOG_ERR; \
log_append(&append_log_event, format, ##__VA_ARGS__); \
COMMON_LOG(ISULA_LOG_ERROR, format, ##__VA_ARGS__)
#define EVENT(format, ...) \
do { \
struct log_object_metadata meta = { \
.file = NULL, .func = NULL, .line = 0, .level = ISULA_LOG_ERROR, \
}; \
(void)new_log(&meta, format, ##__VA_ARGS__); \
} while (0)
#define CRIT(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_CRIT; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
COMMON_LOG(ISULA_LOG_CRIT, format, ##__VA_ARGS__)
#define ALERT(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_ALERT; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
COMMON_LOG(ISULA_LOG_ALERT, format, ##__VA_ARGS__)
#define FATAL(format, ...) \
do { \
struct log_locinfo locinfo = LOG_LOCINFO_INIT; \
struct log_event append_log_event; \
append_log_event.locinfo = &locinfo; \
append_log_event.priority = LOG_EMERG; \
(void)log_append(&append_log_event, format, ##__VA_ARGS__); \
} while (0)
COMMON_LOG(ISULA_LOG_FATAL, format, ##__VA_ARGS__)
#define SYSERROR(format, ...) \
do { \
......@@ -168,7 +128,7 @@ int log_append(const struct log_event *event, const char *format, ...);
#define COMMAND_ERROR(fmt, args...) \
do { \
fprintf(stderr, fmt "\n", ##args); \
(void)fprintf(stderr, fmt "\n", ##args); \
} while (0)
#ifdef __cplusplus
......
......@@ -402,7 +402,6 @@ void WebsocketServer::Wait()
CloseAllWsSession();
lws_context_destroy(m_context);
closelog();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册