From 551e97c4ab04f331c312f9c4e7b5ef488c764819 Mon Sep 17 00:00:00 2001 From: Jussi Vatjus-Anttila Date: Thu, 5 Apr 2018 23:15:40 +0300 Subject: [PATCH] add silly and critical trace levels --- README.md | 2 ++ mbed-trace/mbed_trace.h | 54 +++++++++++++++++++++++++---------- source/mbed_trace.c | 14 +++++++++ test/Test.cpp | 63 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 115 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 2552730..d9c87ae 100644 --- a/README.md +++ b/README.md @@ -84,10 +84,12 @@ When you want to print traces, use the `tr_` macros. The macros behave li Available levels: +* silly * debug * info * warning * error +* critical * cmdline (special behavior, should not be used) For the thread safety, set the mutex wait and release functions. You need do this before the initialization to have the functions available right away: diff --git a/mbed-trace/mbed_trace.h b/mbed-trace/mbed_trace.h index eb4055b..4c361a1 100644 --- a/mbed-trace/mbed_trace.h +++ b/mbed-trace/mbed_trace.h @@ -83,7 +83,7 @@ extern "C" { /** Config mask */ #define TRACE_MASK_CONFIG 0xE0 /** Trace level mask */ -#define TRACE_MASK_LEVEL 0x1F +#define TRACE_MASK_LEVEL 0x7F /** plain trace data instead of "headers" */ #define TRACE_MODE_PLAIN 0x80 @@ -93,36 +93,50 @@ extern "C" { #define TRACE_CARRIAGE_RETURN 0x20 /** used to activate all trace levels */ -#define TRACE_ACTIVE_LEVEL_ALL 0x1F +#define TRACE_ACTIVE_LEVEL_ALL 0x7F /** print all traces same as above */ -#define TRACE_ACTIVE_LEVEL_DEBUG 0x1f -/** print info,warn and error traces */ -#define TRACE_ACTIVE_LEVEL_INFO 0x0f -/** print warn and error traces */ -#define TRACE_ACTIVE_LEVEL_WARN 0x07 -/** print only error trace */ -#define TRACE_ACTIVE_LEVEL_ERROR 0x03 +#define TRACE_ACTIVE_LEVEL_SILLY 0x7f +/** print debug,warn, error and critical traces */ +#define TRACE_ACTIVE_LEVEL_DEBUG 0x3f +/** print info, warn, error and critical traces */ +#define TRACE_ACTIVE_LEVEL_INFO 0x1f +/** print warn, error and critical traces */ +#define TRACE_ACTIVE_LEVEL_WARN 0x0f +/** print error and critical trace */ +#define TRACE_ACTIVE_LEVEL_ERROR 0x07 +/** print only critical trace */ +#define TRACE_ACTIVE_LEVEL_CRITICAL 0x03 /** print only cmd line data */ #define TRACE_ACTIVE_LEVEL_CMD 0x01 /** trace nothing */ #define TRACE_ACTIVE_LEVEL_NONE 0x00 +/** this print is some silly deep information for debug purpose */ +#define TRACE_LEVEL_SILLY 0x40 /** this print is some deep information for debug purpose */ -#define TRACE_LEVEL_DEBUG 0x10 +#define TRACE_LEVEL_DEBUG 0x20 /** Info print, for general purpose prints */ -#define TRACE_LEVEL_INFO 0x08 +#define TRACE_LEVEL_INFO 0x10 /** warning prints, which shouldn't causes any huge problems */ -#define TRACE_LEVEL_WARN 0x04 +#define TRACE_LEVEL_WARN 0x08 /** Error prints, which causes probably problems, e.g. out of mem. */ -#define TRACE_LEVEL_ERROR 0x02 +#define TRACE_LEVEL_ERROR 0x04 +/** Critical prints, which causes critical problems */ +#define TRACE_LEVEL_CRITICAL 0x02 /** special level for cmdline. Behaviours like "plain mode" */ #define TRACE_LEVEL_CMD 0x01 #ifndef MBED_TRACE_MAX_LEVEL -#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG +#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_CRITICAL #endif //usage macros: +#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_SILLY && !defined(tr_silly) +#define tr_silly(...) mbed_tracef(TRACE_LEVEL_SILLY, TRACE_GROUP, __VA_ARGS__) //!< Print debug message +#else +#define tr_silly(...) +#endif + #if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_DEBUG #define tr_debug(...) mbed_tracef(TRACE_LEVEL_DEBUG, TRACE_GROUP, __VA_ARGS__) //!< Print debug message #else @@ -151,6 +165,14 @@ extern "C" { #define tr_err(...) #endif +#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_CRITICAL +#define tr_critical(...) mbed_tracef(TRACE_LEVEL_CRITICAL, TRACE_GROUP, __VA_ARGS__) //!< Print Critical Message +#define tr_crit(...) mbed_tracef(TRACE_LEVEL_CRITICAL, TRACE_GROUP, __VA_ARGS__) //!< Alternative Critical message +#else +#define tr_critical(...) +#define tr_crit(...) +#endif + #define tr_cmdline(...) mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, __VA_ARGS__) //!< Special print for cmdline. See more from TRACE_LEVEL_CMD -level //aliases for the most commonly used functions and the helper functions @@ -200,10 +222,12 @@ void mbed_trace_buffer_sizes(int lineLength, int tmpLength); * TRACE_CARRIAGE_RETURN (print CR before trace line) * * TRACE_ACTIVE_LEVEL_ALL - to activate all trace levels - * or TRACE_ACTIVE_LEVEL_DEBUG (alternative) + * or TRACE_ACTIVE_LEVEL_SILLY (alternative) + * TRACE_ACTIVE_LEVEL_DEBUG * TRACE_ACTIVE_LEVEL_INFO * TRACE_ACTIVE_LEVEL_WARN * TRACE_ACTIVE_LEVEL_ERROR + * TRACE_ACTIVE_LEVEL_CRITICAL * TRACE_ACTIVE_LEVEL_CMD * TRACE_LEVEL_NONE - to deactivate all traces * diff --git a/source/mbed_trace.c b/source/mbed_trace.c index 5fdffd5..83381a2 100644 --- a/source/mbed_trace.c +++ b/source/mbed_trace.c @@ -49,10 +49,12 @@ #endif #endif /* YOTTA_CFG_MEMLIB */ +#define VT100_COLOR_CRITICAL "\x1b[31m" #define VT100_COLOR_ERROR "\x1b[31m" #define VT100_COLOR_WARN "\x1b[33m" #define VT100_COLOR_INFO "\x1b[39m" #define VT100_COLOR_DEBUG "\x1b[90m" +#define VT100_COLOR_SILLY "\x1b[90m" /** default max trace line size in bytes */ #ifdef MBED_TRACE_LINE_LENGTH @@ -361,6 +363,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) if (bLeft > 0) { //include color in ANSI/VT100 escape code switch (dlevel) { + case (TRACE_LEVEL_CRITICAL): + retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_CRITICAL); + break; case (TRACE_LEVEL_ERROR): retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_ERROR); break; @@ -373,6 +378,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) case (TRACE_LEVEL_DEBUG): retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_DEBUG); break; + case (TRACE_LEVEL_SILLY): + retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_SILLY); + break; default: color = 0; //avoid unneeded color-terminate code retval = 0; @@ -408,6 +416,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) if (bLeft > 0) { //add group tag switch (dlevel) { + case (TRACE_LEVEL_CRITICAL): + retval = snprintf(ptr, bLeft, "[CRIT][%-4s]: ", grp); + break; case (TRACE_LEVEL_ERROR): retval = snprintf(ptr, bLeft, "[ERR ][%-4s]: ", grp); break; @@ -420,6 +431,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap) case (TRACE_LEVEL_DEBUG): retval = snprintf(ptr, bLeft, "[DBG ][%-4s]: ", grp); break; + case (TRACE_LEVEL_SILLY): + retval = snprintf(ptr, bLeft, "[SILL][%-4s]: ", grp); + break; default: retval = snprintf(ptr, bLeft, " "); break; diff --git a/test/Test.cpp b/test/Test.cpp index 52b5934..fd28791 100644 --- a/test/Test.cpp +++ b/test/Test.cpp @@ -229,16 +229,18 @@ TEST(trace, active_level_all_ipv6) TEST(trace, config_change) { mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL); - CHECK(mbed_trace_config_get() == TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL); + CHECK(mbed_trace_config_get() == (TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL)); mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_NONE); - CHECK(mbed_trace_config_get() == TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_NONE); + CHECK(mbed_trace_config_get() == (TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_NONE)); mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL); - CHECK(mbed_trace_config_get() == TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL); + CHECK(mbed_trace_config_get() == (TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL)); } TEST(trace, active_level_all_color) { mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL); + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "Begin"); + STRCMP_EQUAL("\x1b[90m[SILL][mygr]: Begin\x1b[0m", buf); mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello"); STRCMP_EQUAL("\x1b[90m[DBG ][mygr]: hello\x1b[0m", buf); mbed_tracef(TRACE_LEVEL_INFO, "mygr", "to one"); @@ -247,6 +249,8 @@ TEST(trace, active_level_all_color) STRCMP_EQUAL("\x1b[33m[WARN][mygr]: and all\x1b[0m", buf); mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "even you"); STRCMP_EQUAL("\x1b[31m[ERR ][mygr]: even you\x1b[0m", buf); + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "and you"); + STRCMP_EQUAL("\x1b[31m[CRIT][mygr]: and you\x1b[0m", buf); } TEST(trace, change_levels) @@ -265,9 +269,35 @@ TEST(trace, change_levels) } +TEST(trace, active_level_silly) +{ + mbed_trace_config_set(TRACE_ACTIVE_LEVEL_SILLY); + + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi"); + STRCMP_EQUAL("[SILL][mygr]: hoi", buf); + + mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep"); + STRCMP_EQUAL("[DBG ][mygr]: hep", buf); + + mbed_tracef(TRACE_LEVEL_INFO, "mygr", "test"); + STRCMP_EQUAL("[INFO][mygr]: test", buf); + + mbed_tracef(TRACE_LEVEL_WARN, "mygr", "hups"); + STRCMP_EQUAL("[WARN][mygr]: hups", buf); + + mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou"); + STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf); + + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn"); + STRCMP_EQUAL("[CRIT][mygr]: damn", buf); +} + TEST(trace, active_level_debug) { mbed_trace_config_set(TRACE_ACTIVE_LEVEL_DEBUG); + + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi"); + STRCMP_EQUAL("", mbed_trace_last()); mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep"); STRCMP_EQUAL("[DBG ][mygr]: hep", buf); @@ -280,12 +310,18 @@ TEST(trace, active_level_debug) mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou"); STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf); + + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn"); + STRCMP_EQUAL("[CRIT][mygr]: damn", buf); } TEST(trace, active_level_info) { buf[0] = 0; mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO); + + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi"); + STRCMP_EQUAL("", mbed_trace_last()); mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep"); STRCMP_EQUAL("", mbed_trace_last()); @@ -298,11 +334,17 @@ TEST(trace, active_level_info) mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou"); STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf); + + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn"); + STRCMP_EQUAL("[CRIT][mygr]: damn", buf); } TEST(trace, active_level_warn) { mbed_trace_config_set(TRACE_ACTIVE_LEVEL_WARN); + + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi"); + STRCMP_EQUAL("", mbed_trace_last()); mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep"); STRCMP_EQUAL("", mbed_trace_last()); @@ -315,11 +357,17 @@ TEST(trace, active_level_warn) mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou"); STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf); + + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn"); + STRCMP_EQUAL("[CRIT][mygr]: damn", buf); } TEST(trace, active_level_error) { mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ERROR); + + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi"); + STRCMP_EQUAL("", mbed_trace_last()); mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep"); STRCMP_EQUAL("", mbed_trace_last()); @@ -332,10 +380,16 @@ TEST(trace, active_level_error) mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou"); STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf); + + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn"); + STRCMP_EQUAL("[CRIT][mygr]: damn", buf); } TEST(trace, active_level_none) { mbed_trace_config_set(TRACE_ACTIVE_LEVEL_NONE); + + mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi"); + STRCMP_EQUAL("", mbed_trace_last()); mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep"); STRCMP_EQUAL("", mbed_trace_last()); @@ -348,6 +402,9 @@ TEST(trace, active_level_none) mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou"); STRCMP_EQUAL("", mbed_trace_last()); + + mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn"); + STRCMP_EQUAL("", mbed_trace_last()); } TEST(trace, active_level_all_1)