From 5bca89b9a3258487d1252d409177b920cc1b5d5f Mon Sep 17 00:00:00 2001 From: ju5t4fun Date: Wed, 26 Oct 2016 16:04:11 +0800 Subject: [PATCH] add log text color Signed-off-by: ju5t4fun --- easylogger/inc/color.h | 45 ++++++++++++++++++++++++++++++++++++++++++ easylogger/src/elog.c | 31 +++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 easylogger/inc/color.h diff --git a/easylogger/inc/color.h b/easylogger/inc/color.h new file mode 100644 index 0000000..569e5f2 --- /dev/null +++ b/easylogger/inc/color.h @@ -0,0 +1,45 @@ +#ifndef __COLORS_H__ +#define __COLORS_H__ + +/* use Escape Sequence control the text color + * details please reference http://www.cnblogs.com/clover-toeic/p/4031618.html + */ + +#define ESC_START "\e[" +#define ESC_END "\e[0m" + +//front color +#define F_BLACK "30;" +#define F_RED "31;" +#define F_GREEN "32;" +#define F_YELLOW "33;" +#define F_BLUE "34;" +#define F_MAGENTA "35;" +#define F_CYAN "36;" +#define F_WHITE "37;" + +//background color +#define B_BLACK "40;" +#define B_RED "41;" +#define B_GREEN "42;" +#define B_YELLOW "43;" +#define B_BLUE "44;" +#define B_MAGENTA "45;" +#define B_CYAN "46;" +#define B_WHITE "47;" + +//show style +#define NORMAL "0m" +#define BOLD "1m" +#define BLINK "5m" +#define NO_BOLD "22m" + +//[front color] + [background color] + [show style] +#define COLOR_ASSERT F_MAGENTA B_BLACK NO_BOLD +#define COLOR_ERROR F_RED B_BLACK NO_BOLD +#define COLOR_WARN F_YELLOW B_BLACK NO_BOLD +#define COLOR_INFO F_BLUE B_BLACK NO_BOLD +#define COLOR_DEBUG F_GREEN B_BLACK NO_BOLD +#define COLOR_VERBOSE F_WHITE B_BLACK NO_BOLD + +#endif diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index dccdf98..517edc4 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -26,6 +26,7 @@ * Created on: 2015-04-28 */ +#include "color.h" #include #include #include @@ -240,6 +241,33 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f /* lock output */ output_lock(); + /* add Escape Sequence start sign and color info*/ + log_len += elog_strcpy(log_len, log_buf + log_len, ESC_START); + char *color = NULL; + switch(level) + { + case ELOG_LVL_ASSERT: + color = (char*) COLOR_ASSERT; + break; + case ELOG_LVL_ERROR: + color = (char*) COLOR_ERROR; + break; + case ELOG_LVL_WARN: + color = (char*) COLOR_WARN; + break; + case ELOG_LVL_INFO: + color = (char*) COLOR_INFO; + break; + case ELOG_LVL_DEBUG: + color = (char*) COLOR_DEBUG; + break; + case ELOG_LVL_VERBOSE: + color = (char*) COLOR_VERBOSE; + break; + default: + ; + } + log_len += elog_strcpy(log_len, log_buf + log_len, color); /* package level info */ if (get_fmt_enabled(level, ELOG_FMT_LVL)) { log_len += elog_strcpy(log_len, log_buf + log_len, level_output_info[level]); @@ -334,6 +362,9 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f strcpy(log_buf + ELOG_BUF_SIZE - newline_len, ELOG_NEWLINE_SIGN); } + /* add Escape Sequence end sign */ + log_len += elog_strcpy(log_len, log_buf + log_len, ESC_END); + /* output log */ elog_port_output(log_buf, log_len);