From c74837f75cbb7cbdd318ad5f7cac5e7019965957 Mon Sep 17 00:00:00 2001 From: armink Date: Wed, 12 Oct 2016 15:46:54 +0800 Subject: [PATCH 1/3] =?UTF-8?q?1=E3=80=81=E3=80=90=E5=AE=8C=E5=96=84?= =?UTF-8?q?=E3=80=91=E6=97=A5=E5=BF=97=E6=BA=A2=E5=87=BA=E5=A4=84=E7=90=86?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- LICENSE | 2 +- easylogger/inc/elog.h | 2 +- easylogger/src/elog.c | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/LICENSE b/LICENSE index 8d9bc06..c431db6 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2015 Armink (armink.ztl@gmail.com) +Copyright (c) 2015-2016 Armink (armink.ztl@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index 4d7c74a..f8f183f 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -74,7 +74,7 @@ extern "C" { #define ELOG_LVL_TOTAL_NUM 6 /* EasyLogger software version number */ -#define ELOG_SW_VERSION "1.05.13" +#define ELOG_SW_VERSION "1.10.12" /* EasyLogger assert for developer. */ #ifdef ELOG_ASSERT_ENABLE diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index d8e7406..dccdf98 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -1,7 +1,7 @@ /* * This file is part of the EasyLogger Library. * - * Copyright (c) 2015, Armink, + * Copyright (c) 2015-2016, Armink, * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -329,8 +329,9 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN); } else { + log_len = ELOG_BUF_SIZE; /* copy newline sign */ - strcpy(log_buf - newline_len, ELOG_NEWLINE_SIGN); + strcpy(log_buf + ELOG_BUF_SIZE - newline_len, ELOG_NEWLINE_SIGN); } /* output log */ From 5bca89b9a3258487d1252d409177b920cc1b5d5f Mon Sep 17 00:00:00 2001 From: ju5t4fun Date: Wed, 26 Oct 2016 16:04:11 +0800 Subject: [PATCH 2/3] 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); From 764d600a4887ca1d3111ea93b631d52d485c4432 Mon Sep 17 00:00:00 2001 From: ju5t4fun Date: Wed, 26 Oct 2016 16:49:50 +0800 Subject: [PATCH 3/3] add function "elog_set_text_color_enabled" and "elog_get_text_color_enabled" to enable and disable log text color Signed-off-by: ju5t4fun --- easylogger/inc/elog.h | 2 ++ easylogger/src/elog.c | 80 ++++++++++++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 27 deletions(-) diff --git a/easylogger/inc/elog.h b/easylogger/inc/elog.h index f8f183f..fe57a8a 100644 --- a/easylogger/inc/elog.h +++ b/easylogger/inc/elog.h @@ -132,6 +132,8 @@ ElogErrCode elog_init(void); void elog_start(void); void elog_set_output_enabled(bool enabled); bool elog_get_output_enabled(void); +void elog_set_text_color_enabled(bool enabled); +bool elog_get_text_color_enabled(void); void elog_set_fmt(uint8_t level, size_t set); void elog_set_filter(uint8_t level, const char *tag, const char *keyword); void elog_set_filter_lvl(uint8_t level); diff --git a/easylogger/src/elog.c b/easylogger/src/elog.c index 517edc4..6640a6c 100644 --- a/easylogger/src/elog.c +++ b/easylogger/src/elog.c @@ -49,6 +49,8 @@ static const char *level_output_info[] = { }; /* the output lock enable or disable. default is enable */ static bool output_lock_enabled = true; +/* the log text color enable or disable. default is enable */ +static bool text_color_enabled = true; /* the output is locked before enable. */ static bool output_is_locked_before_enable = false; /* the output is locked before disable. */ @@ -97,6 +99,24 @@ void elog_set_output_enabled(bool enabled) { elog.output_enabled = enabled; } +/** + * set log text color enable or disable + * + * @param enabled TRUE: enable FALSE:disable + */ + void elog_set_text_color_enabled(bool enabled) { + text_color_enabled = enabled; + } + + /** + * get log text color enable status + * + * @return enable or disable + */ + bool elog_get_text_color_enabled(void) { + return text_color_enabled; + } + /** * get output is enable or disable * @@ -241,33 +261,37 @@ 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); + if(text_color_enabled) { + 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]); @@ -363,7 +387,9 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f } /* add Escape Sequence end sign */ - log_len += elog_strcpy(log_len, log_buf + log_len, ESC_END); + if(text_color_enabled) { + log_len += elog_strcpy(log_len, log_buf + log_len, ESC_END); + } /* output log */ elog_port_output(log_buf, log_len);