From 1958525ba64c9f2f255e5e0bc098eeaf7cbfc383 Mon Sep 17 00:00:00 2001
From: liuhao <liuhao27@huawei.com>
Date: Fri, 11 Jan 2019 17:00:48 +0800
Subject: [PATCH 007/131] use isulad log format

use isulad log format

Signed-off-by: LiFeng <lifeng68@huawei.com>
---
 src/lxc/log.c | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/src/lxc/log.c b/src/lxc/log.c
index 4e744599..91fb7efa 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -139,6 +139,7 @@ static char *lxc_log_get_va_msg(struct lxc_log_event *event)
 	return msg;
 }
 
+/* use fifo to save log */
 static const char *isulad_use_log_fifo(const char *file)
 {
 #define ISULAD_FIFO_PREFIX "fifo:"
@@ -150,6 +151,7 @@ static const char *isulad_use_log_fifo(const char *file)
 	return file;
 }
 
+/* open isulad fifo */
 static int isulad_open_fifo(const char *file_path)
 {
 #define LOG_FIFO_SIZE (1024 * 1024)
@@ -349,6 +351,8 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
 	ssize_t ret;
 	int fd_to_use = -1;
 	const char *log_container_name;
+	const char *isulad_prefix;
+	size_t isulad_len = 0;
 
 #ifndef NO_LXC_CONF
 	if (current_config)
@@ -367,11 +371,14 @@ static int log_append_logfile(const struct lxc_log_appender *appender,
 	if (lxc_unix_epoch_to_utc(date_time, LXC_LOG_TIME_SIZE, &event->timestamp) < 0)
 		return -1;
 
+	/* use isulad log format */
+	if (log_container_name && strlen(log_container_name) > 15) {
+		isulad_len = strlen(log_container_name) - 15;
+	}
+	isulad_prefix = log_container_name ? (log_container_name + isulad_len) : log_prefix;
 	n = snprintf(buffer, sizeof(buffer),
-		     "%s%s%s %s %-8s %s - %s:%s:%d - ",
-		     log_prefix,
-		     log_container_name ? " " : "",
-		     log_container_name ? log_container_name : "",
+		     "%15s %s %-8s %s - %s:%s:%d - ",
+		     isulad_prefix,
 		     date_time,
 		     lxc_log_priority_to_string(event->priority),
 		     event->category,
@@ -752,7 +759,9 @@ int lxc_log_init(struct lxc_log *log)
 
 	if (lxc_log_fd != -1) {
 		lxc_log_category_lxc.appender = &log_appender_logfile;
-		lxc_log_category_lxc.appender->next = &log_appender_stderr;
+		if (!lxc_quiet_specified)
+			if (!log->quiet)
+				lxc_log_category_lxc.appender->next = &log_appender_stderr;
 	}
 
 	return ret;
-- 
2.23.0

