Skip to content

Commit

Permalink
add silly and critical trace levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Jussi Vatjus-Anttila committed Apr 5, 2018
1 parent 6d3590f commit f7bf781
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ When you want to print traces, use the `tr_<level>` 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:
Expand Down
52 changes: 38 additions & 14 deletions mbed-trace/mbed_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down
14 changes: 14 additions & 0 deletions source/mbed_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
63 changes: 60 additions & 3 deletions test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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)
Expand Down

0 comments on commit f7bf781

Please sign in to comment.