[sheepdog] [PATCH] logger: colorize logger output when stdout is console

MORITA Kazutaka morita.kazutaka at lab.ntt.co.jp
Tue Mar 12 16:14:05 CET 2013


With this patch, the logger output will be readable and, in addition,
we can easily review the log level of each output.

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/collie.c   |  2 +-
 collie/collie.h   |  3 ---
 collie/treeview.c |  3 ---
 include/util.h    | 21 +++++++++++++++++++++
 lib/logger.c      | 40 +++++++++++++++++++++++++++++++++++-----
 5 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/collie/collie.c b/collie/collie.c
index 4f5e974..08c78eb 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -380,7 +380,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	if (!isatty(STDOUT_FILENO) || raw_output)
+	if (!is_stdout_console() || raw_output)
 		highlight = false;
 
 	if (flags & SUBCMD_FLAG_NEED_NODELIST) {
diff --git a/collie/collie.h b/collie/collie.h
index 7e9b6be..d8c65d4 100644
--- a/collie/collie.h
+++ b/collie/collie.h
@@ -25,9 +25,6 @@
 #define SUBCMD_FLAG_NEED_NODELIST (1 << 0)
 #define SUBCMD_FLAG_NEED_THIRD_ARG (1 << 1)
 
-#define TEXT_NORMAL "\033[0m"
-#define TEXT_BOLD   "\033[1m"
-
 #define UINT64_DECIMAL_SIZE 21
 
 struct command {
diff --git a/collie/treeview.c b/collie/treeview.c
index cb3bd87..2910170 100644
--- a/collie/treeview.c
+++ b/collie/treeview.c
@@ -20,9 +20,6 @@
 #define MAX_DEPTH    100
 #endif
 
-#define TEXT_NORMAL "\033[0m"
-#define TEXT_BOLD   "\033[1m"
-
 struct vdi_tree {
 	char name[1024];
 	char label[256];
diff --git a/include/util.h b/include/util.h
index eae4e9a..5a72bd1 100644
--- a/include/util.h
+++ b/include/util.h
@@ -131,4 +131,25 @@ static inline void uatomic_set_false(uatomic_bool *val)
 	uatomic_set(&val->val, 0);
 }
 
+/* colors */
+#define TEXT_NORMAL         "\033[0m"
+#define TEXT_BOLD           "\033[1m"
+#define TEXT_RED            "\033[0;31m"
+#define TEXT_BOLD_RED       "\033[1;31m"
+#define TEXT_GREEN          "\033[0;32m"
+#define TEXT_BOLD_GREEN     "\033[1;32m"
+#define TEXT_YELLOW         "\033[0;33m"
+#define TEXT_BOLD_YELLOW    "\033[1;33m"
+#define TEXT_BLUE           "\033[0;34m"
+#define TEXT_BOLD_BLUE      "\033[1;34m"
+#define TEXT_MAGENTA        "\033[0;35m"
+#define TEXT_BOLD_MAGENTA   "\033[1;35m"
+#define TEXT_CYAN           "\033[0;36m"
+#define TEXT_BOLD_CYAN      "\033[1;36m"
+
+static inline bool is_stdout_console(void)
+{
+	return isatty(STDOUT_FILENO);
+}
+
 #endif
diff --git a/lib/logger.c b/lib/logger.c
index e48471d..15b6880 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -38,6 +38,18 @@
 #include "logger.h"
 #include "util.h"
 
+static bool colorize;
+static const char *log_color[] = {
+	[SDOG_EMERG] = TEXT_BOLD_RED,
+	[SDOG_ALERT] = TEXT_BOLD_RED,
+	[SDOG_CRIT] = TEXT_BOLD_RED,
+	[SDOG_ERR] = TEXT_BOLD_RED,
+	[SDOG_WARNING] = TEXT_BOLD_YELLOW,
+	[SDOG_NOTICE] = TEXT_BOLD_CYAN,
+	[SDOG_INFO] = TEXT_CYAN,
+	[SDOG_DEBUG] = TEXT_GREEN,
+};
+
 struct logger_user_info *logger_user_info;
 
 static void dolog(int prio, const char *func, int line, const char *fmt,
@@ -205,22 +217,37 @@ static notrace int default_log_formatter(char *buff, size_t size,
 	strftime(p, size, "%b %2d %H:%M:%S ", (const struct tm *)&tm);
 	p += strlen(p);
 
+	if (colorize) {
+		pstrcpy(p, size - strlen(buff), TEXT_YELLOW);
+		p += strlen(p);
+	}
+
 	if (worker_name_len && msg->worker_idx)
-		snprintf(p, size, "[%s %d] ", msg->worker_name,
-			msg->worker_idx);
+		snprintf(p, size - strlen(buff), "[%s %d] ", msg->worker_name,
+			 msg->worker_idx);
 	else if (worker_name_len)
-		snprintf(p, size, "[%s] ", msg->worker_name);
+		snprintf(p, size - strlen(buff), "[%s] ", msg->worker_name);
 	else
-		pstrcpy(p, size, "[main] ");
+		pstrcpy(p, size - strlen(buff), "[main] ");
 
 	p += strlen(p);
 
 	snprintf(p, size - strlen(buff), "%s(%d) ", msg->func, msg->line);
 	p += strlen(p);
 
+	if (colorize) {
+		pstrcpy(p, size - strlen(buff), log_color[msg->prio]);
+		p += strlen(p);
+	}
+
 	snprintf(p, size - strlen(buff), "%s", (char *)msg->str);
 	p += strlen(p);
 
+	if (colorize) {
+		pstrcpy(p, size - strlen(buff), "\033[m");
+		p += strlen(p);
+	}
+
 	return p - buff;
 }
 log_format_register("default", default_log_formatter);
@@ -569,7 +596,10 @@ notrace int log_init(const char *program_name, int size, bool to_stdout,
 
 	semkey = random();
 
-	if (!to_stdout) {
+	if (to_stdout) {
+		if (is_stdout_console())
+			colorize = true;
+	} else {
 		if (logarea_init(size)) {
 			syslog(LOG_ERR, "failed to initialize the logger\n");
 			return 1;
-- 
1.8.1.3.566.gaa39828




More information about the sheepdog mailing list