[sheepdog] [PATCH stable-0.7 3/3] logger: add a unified log destination option and syslog support

Hitoshi Mitake mitake.hitoshi at lab.ntt.co.jp
Mon Jan 27 11:54:35 CET 2014


This patch adds a new parameter "dst=" to the "-l" option for
specifying logging destination. Possible parameters are below:
- default: a dedicated file under sheep's directory
- syslog: standard syslog
- stdout: standard output

In addition, this patch removes "-o" option and "-f" option, previous
options for logging with stdout and let sheep run as a foreground
process. Now it is replaced with "-l dst=stdout".

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
Signed-off-by: Liu Yuan <namei.unix at gmail.com>

Conflicts:
	CHANGELOG.md
	lib/logger.c
	sheep/sheep.c

Conflicts were resolved by Hitoshi Mitake. In addition, a new option
'-s' is added for using syslog as a log destination.

Signed-off-by: Hitoshi Mitake <mitake.hitoshi at lab.ntt.co.jp>
---
 include/logger.h |    9 +++++++-
 lib/logger.c     |   61 +++++++++++++++++++++++++++++++++++++-----------------
 sheep/sheep.c    |   11 ++++++++--
 3 files changed, 59 insertions(+), 22 deletions(-)

diff --git a/include/logger.h b/include/logger.h
index a86c369..ae9af30 100644
--- a/include/logger.h
+++ b/include/logger.h
@@ -30,9 +30,16 @@ struct logger_user_info {
 
 extern int sd_log_level;
 
+enum log_dst_type {
+	LOG_DST_DEFAULT,
+	LOG_DST_STDOUT,
+	LOG_DST_SYSLOG,
+};
+
 void early_log_init(const char *format_name,
 		struct logger_user_info *user_info);
-int log_init(const char *progname, bool to_stdout, int level, char *outfile);
+int log_init(const char *progname, enum log_dst_type type, int level,
+	     char *outfile);
 void log_close(void);
 void dump_logmsg(void *);
 void log_write(int prio, const char *func, int line, const char *fmt, ...)
diff --git a/lib/logger.c b/lib/logger.c
index 25da92a..f5111ab 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -95,9 +95,11 @@ struct logmsg {
 	char str[0];
 };
 
+typedef int (*formatter_fn)(char *, size_t, const struct logmsg *, bool);
+
 struct log_format {
 	const char *name;
-	int (*formatter)(char *, size_t, const struct logmsg *);
+	int (*formatter)(char *, size_t, const struct logmsg *, bool);
 	struct list_head list;
 };
 
@@ -126,6 +128,8 @@ static char *log_buff;
 
 static int64_t max_logsize = 500 * 1024 * 1024;  /*500MB*/
 
+static enum log_dst_type dst_type = LOG_DST_STDOUT;
+
 /*
  * block_sighup()
  *
@@ -260,17 +264,20 @@ static void free_logarea(void)
 }
 
 static int server_log_formatter(char *buff, size_t size,
-				const struct logmsg *msg)
+				const struct logmsg *msg, bool print_time)
 {
 	char *p = buff;
 	struct tm tm;
 	size_t len;
 	char thread_name[MAX_THREAD_NAME_LEN];
 
-	localtime_r(&msg->tv.tv_sec, &tm);
-	len = strftime(p, size, "%b %2d %H:%M:%S ", (const struct tm *)&tm);
-	p += len;
-	size -= len;
+	if (print_time) {
+		localtime_r(&msg->tv.tv_sec, &tm);
+		len = strftime(p, size, "%b %2d %H:%M:%S ",
+			       (const struct tm *)&tm);
+		p += len;
+		size -= len;
+	}
 
 	len = snprintf(p, size, "%s%6s %s[%s] %s(%d) %s%s%s",
 		       colorize ? log_color[msg->prio] : "",
@@ -290,7 +297,7 @@ static int server_log_formatter(char *buff, size_t size,
 log_format_register("server", server_log_formatter);
 
 static int default_log_formatter(char *buff, size_t size,
-				 const struct logmsg *msg)
+				 const struct logmsg *msg, bool print_time)
 {
 	size_t len = min(size, msg->str_len);
 
@@ -301,7 +308,7 @@ static int default_log_formatter(char *buff, size_t size,
 log_format_register("default", default_log_formatter);
 
 static int json_log_formatter(char *buff, size_t size,
-				const struct logmsg *msg)
+			      const struct logmsg *msg, bool print_time)
 {
 	char *p = buff;
 	size_t len;
@@ -353,8 +360,11 @@ static void log_syslog(const struct logmsg *msg)
 	char str[MAX_MSG_SIZE];
 	int len;
 
-	len = format->formatter(str, sizeof(str) - 1, msg);
-	str[len++] = '\n';
+	len = format->formatter(str, sizeof(str) - 1, msg, log_fd >= 0);
+	if (dst_type == LOG_DST_DEFAULT)
+		str[len++] = '\n';
+	else	/* LOG_DST_SYSLOG */
+		str[len++] = '\0';
 
 	block_sighup();
 
@@ -430,7 +440,8 @@ static void dolog(int prio, const char *func, int line,
 		char str_final[MAX_MSG_SIZE];
 
 		init_logmsg(msg, &tv, prio, func, line);
-		len = format->formatter(str_final, sizeof(str_final) - 1, msg);
+		len = format->formatter(str_final, sizeof(str_final) - 1, msg,
+					true);
 		str_final[len++] = '\n';
 		xwrite(fileno(stderr), str_final, len);
 		fflush(stderr);
@@ -550,10 +561,12 @@ static void logger(char *log_dir, char *outfile)
 
 	log_buff = xzalloc(la->end - la->start);
 
-	log_fd = open(outfile, O_CREAT | O_RDWR | O_APPEND, 0644);
-	if (log_fd < 0) {
-		syslog(LOG_ERR, "failed to open %s\n", outfile);
-		exit(1);
+	if (dst_type == LOG_DST_DEFAULT) {
+		log_fd = open(outfile, O_CREAT | O_RDWR | O_APPEND, 0644);
+		if (log_fd < 0) {
+			syslog(LOG_ERR, "failed to open %s\n", outfile);
+			exit(1);
+		}
 	}
 	la->active = true;
 
@@ -588,7 +601,7 @@ static void logger(char *log_dir, char *outfile)
 
 		block_sighup();
 
-		if (max_logsize) {
+		if (dst_type == LOG_DST_DEFAULT && max_logsize) {
 			off_t offset;
 
 			offset = lseek(log_fd, 0, SEEK_END);
@@ -638,12 +651,13 @@ void early_log_init(const char *format_name, struct logger_user_info *user_info)
 	exit(1);
 }
 
-int log_init(const char *program_name, bool to_stdout, int level,
+int log_init(const char *program_name, enum log_dst_type type, int level,
 		     char *outfile)
 {
 	char log_dir[PATH_MAX], tmp[PATH_MAX];
 	int size = level == SDOG_DEBUG ? LOG_SPACE_DEBUG_SIZE : LOG_SPACE_SIZE;
 
+	dst_type = type;
 	sd_log_level = level;
 
 	log_name = program_name;
@@ -653,10 +667,15 @@ int log_init(const char *program_name, bool to_stdout, int level,
 
 	semkey = random();
 
-	if (to_stdout) {
+	switch (type) {
+	case LOG_DST_STDOUT:
 		if (is_stdout_console())
 			colorize = true;
-	} else {
+		break;
+	case LOG_DST_SYSLOG:
+		openlog(program_name, LOG_PID, LOG_DAEMON);
+		/* fall through */
+	case LOG_DST_DEFAULT:
 		if (logarea_init(size)) {
 			syslog(LOG_ERR, "failed to initialize the logger\n");
 			return 1;
@@ -679,6 +698,10 @@ int log_init(const char *program_name, bool to_stdout, int level,
 			syslog(LOG_WARNING, "logger pid %d starting\n", logger_pid);
 		else
 			logger(log_dir, outfile);
+		break;
+	default:
+		sd_err("unknown type of log destination type: %d", type);
+		return -1;
 	}
 
 	return 0;
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 60cc158..eb6afe8 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -126,6 +126,7 @@ static struct sd_option sheep_options[] = {
 	 "(default: 7000)"},
 	{'P', "pidfile", true, "create a pid file"},
 	{'r', "http", true, "enable http service", http_help},
+	{'s', "syslog", false, "use syslog as an output of log"},
 	{'u', "upgrade", false, "upgrade to the latest data layout"},
 	{'v', "version", false, "show the version"},
 	{'w', "cache", true, "enable object cache", cache_help},
@@ -600,6 +601,7 @@ int main(int argc, char **argv)
 	const char *log_format = "server", *http_address = NULL;
 	static struct logger_user_info sheep_info;
 	struct stat logdir_st;
+	enum log_dst_type log_dst = LOG_DST_DEFAULT;
 
 	install_crash_handler(crash_handler);
 	signal(SIGPIPE, SIG_IGN);
@@ -644,6 +646,9 @@ int main(int argc, char **argv)
 				exit(1);
 			}
 			break;
+		case 's':
+			log_dst = LOG_DST_SYSLOG;
+			break;
 		case 'n':
 			sys->nosync = true;
 			break;
@@ -766,7 +771,9 @@ int main(int argc, char **argv)
 		exit(1);
 	}
 
-	if (logdir) {
+	if (to_stdout)
+		log_dst = LOG_DST_STDOUT;
+	else if (logdir) {
 		memset(&logdir_st, 0, sizeof(logdir_st));
 		ret = stat(logdir, &logdir_st);
 		if (ret < 0) {
@@ -790,7 +797,7 @@ int main(int argc, char **argv)
 	if (lock_and_daemon(is_daemon, dir))
 		exit(1);
 
-	ret = log_init(program_name, to_stdout, log_level, log_path);
+	ret = log_init(program_name, log_dst, log_level, log_path);
 	if (ret)
 		exit(1);
 
-- 
1.7.10.4




More information about the sheepdog mailing list