Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added silly and critical trace levels #80

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
74 changes: 49 additions & 25 deletions mbed-trace/mbed_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,48 +81,62 @@ extern "C" {
and 5 lower bits are trace level configuration */

/** Config mask */
#define TRACE_MASK_CONFIG 0xE0
#define TRACE_MASK_CONFIG 0xF0
/** Trace level mask */
#define TRACE_MASK_LEVEL 0x1F
#define TRACE_MASK_LEVEL 0x0F

/** plain trace data instead of "headers" */
#define TRACE_MODE_PLAIN 0x80
#define TRACE_MODE_PLAIN 0x40
/** color mode */
#define TRACE_MODE_COLOR 0x40
#define TRACE_MODE_COLOR 0x20
/** Use print CR before trace line */
#define TRACE_CARRIAGE_RETURN 0x20

/** used to activate all trace levels */
#define TRACE_ACTIVE_LEVEL_ALL 0x1F
/** 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
/** print only cmd line data */
#define TRACE_ACTIVE_LEVEL_CMD 0x01
/** trace nothing */
#define TRACE_ACTIVE_LEVEL_NONE 0x00
#define TRACE_CARRIAGE_RETURN 0x10

/** this print is some silly deep information for debug purpose */
#define TRACE_LEVEL_SILLY 0x07
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be just called DEEP rather than SILLY?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with both keys. I just pick silly keyword from winston library :) Votes ?

/** this print is some deep information for debug purpose */
#define TRACE_LEVEL_DEBUG 0x10
#define TRACE_LEVEL_DEBUG 0x06
/** Info print, for general purpose prints */
#define TRACE_LEVEL_INFO 0x08
#define TRACE_LEVEL_INFO 0x05
/** warning prints, which shouldn't causes any huge problems */
#define TRACE_LEVEL_WARN 0x04
/** Error prints, which causes probably problems, e.g. out of mem. */
#define TRACE_LEVEL_ERROR 0x02
#define TRACE_LEVEL_ERROR 0x03
/** Critical prints, which causes critical problems */
#define TRACE_LEVEL_CRITICAL 0x02
/** special level for cmdline. Behaviours like "plain mode" */
#define TRACE_LEVEL_CMD 0x01

/** used to activate all trace levels */
#define TRACE_ACTIVE_LEVEL_ALL TRACE_LEVEL_SILLY
/** print all traces same as above */
#define TRACE_ACTIVE_LEVEL_SILLY TRACE_LEVEL_SILLY
/** print debug,warn, error and critical traces */
#define TRACE_ACTIVE_LEVEL_DEBUG TRACE_LEVEL_DEBUG
/** print info, warn, error and critical traces */
#define TRACE_ACTIVE_LEVEL_INFO TRACE_LEVEL_INFO
/** print warn, error and critical traces */
#define TRACE_ACTIVE_LEVEL_WARN TRACE_LEVEL_WARN
/** print error and critical trace */
#define TRACE_ACTIVE_LEVEL_ERROR TRACE_LEVEL_ERROR
/** print only critical trace */
#define TRACE_ACTIVE_LEVEL_CRITICAL TRACE_LEVEL_CRITICAL
/** print only cmd line data */
#define TRACE_ACTIVE_LEVEL_CMD TRACE_LEVEL_CMD
/** trace nothing */
#define TRACE_ACTIVE_LEVEL_NONE 0x00
jupe marked this conversation as resolved.
Show resolved Hide resolved

#ifndef MBED_TRACE_MAX_LEVEL
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_CRITICAL
jupe marked this conversation as resolved.
Show resolved Hide resolved
#endif

//usage macros:
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_SILLY && !defined(tr_silly)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tr_silly could cause conflict if somebody define it after including this header but maybe we shouldn’t do any more hacks in here

#define tr_silly(...) mbed_tracef(TRACE_LEVEL_SILLY, TRACE_GROUP, __VA_ARGS__) //!< Print silly 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
jupe marked this conversation as resolved.
Show resolved Hide resolved
#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
16 changes: 15 additions & 1 deletion 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"
jupe marked this conversation as resolved.
Show resolved Hide resolved
#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 @@ -329,7 +331,7 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
mbed_trace_reset_tmp();
goto end;
}
if ((m_trace.trace_config & TRACE_MASK_LEVEL) & dlevel) {
if ((m_trace.trace_config & TRACE_MASK_LEVEL) >= dlevel) {
bool color = (m_trace.trace_config & TRACE_MODE_COLOR) != 0;
bool plain = (m_trace.trace_config & TRACE_MODE_PLAIN) != 0;
bool cr = (m_trace.trace_config & TRACE_CARRIAGE_RETURN) != 0;
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