Skip to content

Commit 1e1900d

Browse files
author
Jussi Vatjus-Anttila
committed
add silly and critical trace levels
1 parent 6d3590f commit 1e1900d

File tree

4 files changed

+123
-26
lines changed

4 files changed

+123
-26
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,12 @@ When you want to print traces, use the `tr_<level>` macros. The macros behave li
8484

8585
Available levels:
8686

87+
* silly
8788
* debug
8889
* info
8990
* warning
9091
* error
92+
* critical
9193
* cmdline (special behavior, should not be used)
9294

9395
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:

mbed-trace/mbed_trace.h

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,46 +83,60 @@ extern "C" {
8383
/** Config mask */
8484
#define TRACE_MASK_CONFIG 0xE0
8585
/** Trace level mask */
86-
#define TRACE_MASK_LEVEL 0x1F
86+
#define TRACE_MASK_LEVEL 0x7F
8787

8888
/** plain trace data instead of "headers" */
89-
#define TRACE_MODE_PLAIN 0x80
89+
#define TRACE_MODE_PLAIN 0x400
9090
/** color mode */
91-
#define TRACE_MODE_COLOR 0x40
91+
#define TRACE_MODE_COLOR 0x200
9292
/** Use print CR before trace line */
93-
#define TRACE_CARRIAGE_RETURN 0x20
93+
#define TRACE_CARRIAGE_RETURN 0x100
9494

9595
/** used to activate all trace levels */
96-
#define TRACE_ACTIVE_LEVEL_ALL 0x1F
96+
#define TRACE_ACTIVE_LEVEL_ALL 0x7F
9797
/** print all traces same as above */
98-
#define TRACE_ACTIVE_LEVEL_DEBUG 0x1f
99-
/** print info,warn and error traces */
100-
#define TRACE_ACTIVE_LEVEL_INFO 0x0f
101-
/** print warn and error traces */
102-
#define TRACE_ACTIVE_LEVEL_WARN 0x07
103-
/** print only error trace */
104-
#define TRACE_ACTIVE_LEVEL_ERROR 0x03
98+
#define TRACE_ACTIVE_LEVEL_SILLY 0x7f
99+
/** print debug,warn, error and critical traces */
100+
#define TRACE_ACTIVE_LEVEL_DEBUG 0x3f
101+
/** print info, warn, error and critical traces */
102+
#define TRACE_ACTIVE_LEVEL_INFO 0x1f
103+
/** print warn, error and critical traces */
104+
#define TRACE_ACTIVE_LEVEL_WARN 0x0f
105+
/** print error and critical trace */
106+
#define TRACE_ACTIVE_LEVEL_ERROR 0x07
107+
/** print only critical trace */
108+
#define TRACE_ACTIVE_LEVEL_CRITICAL 0x03
105109
/** print only cmd line data */
106110
#define TRACE_ACTIVE_LEVEL_CMD 0x01
107111
/** trace nothing */
108112
#define TRACE_ACTIVE_LEVEL_NONE 0x00
109113

114+
/** this print is some silly deep information for debug purpose */
115+
#define TRACE_LEVEL_SILLY 0x40
110116
/** this print is some deep information for debug purpose */
111-
#define TRACE_LEVEL_DEBUG 0x10
117+
#define TRACE_LEVEL_DEBUG 0x20
112118
/** Info print, for general purpose prints */
113-
#define TRACE_LEVEL_INFO 0x08
119+
#define TRACE_LEVEL_INFO 0x10
114120
/** warning prints, which shouldn't causes any huge problems */
115-
#define TRACE_LEVEL_WARN 0x04
121+
#define TRACE_LEVEL_WARN 0x08
116122
/** Error prints, which causes probably problems, e.g. out of mem. */
117-
#define TRACE_LEVEL_ERROR 0x02
123+
#define TRACE_LEVEL_ERROR 0x04
124+
/** Critical prints, which causes critical problems */
125+
#define TRACE_LEVEL_CRITICAL 0x02
118126
/** special level for cmdline. Behaviours like "plain mode" */
119127
#define TRACE_LEVEL_CMD 0x01
120128

121129
#ifndef MBED_TRACE_MAX_LEVEL
122-
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
130+
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_CRITICAL
123131
#endif
124132

125133
//usage macros:
134+
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_SILLY && !defined(tr_silly)
135+
#define tr_silly(...) mbed_tracef(TRACE_LEVEL_SILLY, TRACE_GROUP, __VA_ARGS__) //!< Print silly message
136+
#else
137+
#define tr_silly(...)
138+
#endif
139+
126140
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_DEBUG
127141
#define tr_debug(...) mbed_tracef(TRACE_LEVEL_DEBUG, TRACE_GROUP, __VA_ARGS__) //!< Print debug message
128142
#else
@@ -151,6 +165,14 @@ extern "C" {
151165
#define tr_err(...)
152166
#endif
153167

168+
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_CRITICAL
169+
#define tr_critical(...) mbed_tracef(TRACE_LEVEL_CRITICAL, TRACE_GROUP, __VA_ARGS__) //!< Print Critical Message
170+
#define tr_crit(...) mbed_tracef(TRACE_LEVEL_CRITICAL, TRACE_GROUP, __VA_ARGS__) //!< Alternative Critical message
171+
#else
172+
#define tr_critical(...)
173+
#define tr_crit(...)
174+
#endif
175+
154176
#define tr_cmdline(...) mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, __VA_ARGS__) //!< Special print for cmdline. See more from TRACE_LEVEL_CMD -level
155177

156178
//aliases for the most commonly used functions and the helper functions
@@ -200,10 +222,12 @@ void mbed_trace_buffer_sizes(int lineLength, int tmpLength);
200222
* TRACE_CARRIAGE_RETURN (print CR before trace line)
201223
*
202224
* TRACE_ACTIVE_LEVEL_ALL - to activate all trace levels
203-
* or TRACE_ACTIVE_LEVEL_DEBUG (alternative)
225+
* or TRACE_ACTIVE_LEVEL_SILLY (alternative)
226+
* TRACE_ACTIVE_LEVEL_DEBUG
204227
* TRACE_ACTIVE_LEVEL_INFO
205228
* TRACE_ACTIVE_LEVEL_WARN
206229
* TRACE_ACTIVE_LEVEL_ERROR
230+
* TRACE_ACTIVE_LEVEL_CRITICAL
207231
* TRACE_ACTIVE_LEVEL_CMD
208232
* TRACE_LEVEL_NONE - to deactivate all traces
209233
*
@@ -213,11 +237,11 @@ void mbed_trace_buffer_sizes(int lineLength, int tmpLength);
213237
* mbed_trace_config_set( TRACE_ACTIVE_LEVEL_ALL|TRACE_MODE_COLOR );
214238
* @endcode
215239
*/
216-
void mbed_trace_config_set(uint8_t config);
240+
void mbed_trace_config_set(uint16_t config);
217241
/** get trace configurations
218242
* @return trace configuration byte
219243
*/
220-
uint8_t mbed_trace_config_get(void);
244+
uint16_t mbed_trace_config_get(void);
221245
/**
222246
* Set trace prefix function
223247
* pref_f -function return string with null terminated

source/mbed_trace.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@
4949
#endif
5050
#endif /* YOTTA_CFG_MEMLIB */
5151

52+
#define VT100_COLOR_CRITICAL "\x1b[31m"
5253
#define VT100_COLOR_ERROR "\x1b[31m"
5354
#define VT100_COLOR_WARN "\x1b[33m"
5455
#define VT100_COLOR_INFO "\x1b[39m"
5556
#define VT100_COLOR_DEBUG "\x1b[90m"
57+
#define VT100_COLOR_SILLY "\x1b[90m"
5658

5759
/** default max trace line size in bytes */
5860
#ifdef MBED_TRACE_LINE_LENGTH
@@ -99,7 +101,7 @@ static void mbed_trace_reset_tmp(void);
99101

100102
typedef struct trace_s {
101103
/** trace configuration bits */
102-
uint8_t trace_config;
104+
uint16_t trace_config;
103105
/** exclude filters list, related group name */
104106
char *filters_exclude;
105107
/** include filters list, related group name */
@@ -225,11 +227,11 @@ void mbed_trace_buffer_sizes(int lineLength, int tmpLength)
225227
mbed_trace_reset_tmp();
226228
}
227229
}
228-
void mbed_trace_config_set(uint8_t config)
230+
void mbed_trace_config_set(uint16_t config)
229231
{
230232
m_trace.trace_config = config;
231233
}
232-
uint8_t mbed_trace_config_get(void)
234+
uint16_t mbed_trace_config_get(void)
233235
{
234236
return m_trace.trace_config;
235237
}
@@ -361,6 +363,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
361363
if (bLeft > 0) {
362364
//include color in ANSI/VT100 escape code
363365
switch (dlevel) {
366+
case (TRACE_LEVEL_CRITICAL):
367+
retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_CRITICAL);
368+
break;
364369
case (TRACE_LEVEL_ERROR):
365370
retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_ERROR);
366371
break;
@@ -373,6 +378,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
373378
case (TRACE_LEVEL_DEBUG):
374379
retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_DEBUG);
375380
break;
381+
case (TRACE_LEVEL_SILLY):
382+
retval = snprintf(ptr, bLeft, "%s", VT100_COLOR_SILLY);
383+
break;
376384
default:
377385
color = 0; //avoid unneeded color-terminate code
378386
retval = 0;
@@ -408,6 +416,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
408416
if (bLeft > 0) {
409417
//add group tag
410418
switch (dlevel) {
419+
case (TRACE_LEVEL_CRITICAL):
420+
retval = snprintf(ptr, bLeft, "[CRIT][%-4s]: ", grp);
421+
break;
411422
case (TRACE_LEVEL_ERROR):
412423
retval = snprintf(ptr, bLeft, "[ERR ][%-4s]: ", grp);
413424
break;
@@ -420,6 +431,9 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
420431
case (TRACE_LEVEL_DEBUG):
421432
retval = snprintf(ptr, bLeft, "[DBG ][%-4s]: ", grp);
422433
break;
434+
case (TRACE_LEVEL_SILLY):
435+
retval = snprintf(ptr, bLeft, "[SILL][%-4s]: ", grp);
436+
break;
423437
default:
424438
retval = snprintf(ptr, bLeft, " ");
425439
break;

test/Test.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,16 +229,18 @@ TEST(trace, active_level_all_ipv6)
229229
TEST(trace, config_change)
230230
{
231231
mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL);
232-
CHECK(mbed_trace_config_get() == TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL);
232+
CHECK(mbed_trace_config_get() == (TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL));
233233
mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_NONE);
234-
CHECK(mbed_trace_config_get() == TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_NONE);
234+
CHECK(mbed_trace_config_get() == (TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_NONE));
235235
mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL);
236-
CHECK(mbed_trace_config_get() == TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL);
236+
CHECK(mbed_trace_config_get() == (TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL));
237237
}
238238

239239
TEST(trace, active_level_all_color)
240240
{
241241
mbed_trace_config_set(TRACE_MODE_COLOR|TRACE_ACTIVE_LEVEL_ALL);
242+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "Begin");
243+
STRCMP_EQUAL("\x1b[90m[SILL][mygr]: Begin\x1b[0m", buf);
242244
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
243245
STRCMP_EQUAL("\x1b[90m[DBG ][mygr]: hello\x1b[0m", buf);
244246
mbed_tracef(TRACE_LEVEL_INFO, "mygr", "to one");
@@ -247,6 +249,8 @@ TEST(trace, active_level_all_color)
247249
STRCMP_EQUAL("\x1b[33m[WARN][mygr]: and all\x1b[0m", buf);
248250
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "even you");
249251
STRCMP_EQUAL("\x1b[31m[ERR ][mygr]: even you\x1b[0m", buf);
252+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "and you");
253+
STRCMP_EQUAL("\x1b[31m[CRIT][mygr]: and you\x1b[0m", buf);
250254
}
251255

252256
TEST(trace, change_levels)
@@ -265,9 +269,35 @@ TEST(trace, change_levels)
265269

266270
}
267271

272+
TEST(trace, active_level_silly)
273+
{
274+
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_SILLY);
275+
276+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi");
277+
STRCMP_EQUAL("[SILL][mygr]: hoi", buf);
278+
279+
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
280+
STRCMP_EQUAL("[DBG ][mygr]: hep", buf);
281+
282+
mbed_tracef(TRACE_LEVEL_INFO, "mygr", "test");
283+
STRCMP_EQUAL("[INFO][mygr]: test", buf);
284+
285+
mbed_tracef(TRACE_LEVEL_WARN, "mygr", "hups");
286+
STRCMP_EQUAL("[WARN][mygr]: hups", buf);
287+
288+
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou");
289+
STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf);
290+
291+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn");
292+
STRCMP_EQUAL("[CRIT][mygr]: damn", buf);
293+
}
294+
268295
TEST(trace, active_level_debug)
269296
{
270297
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_DEBUG);
298+
299+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi");
300+
STRCMP_EQUAL("", mbed_trace_last());
271301

272302
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
273303
STRCMP_EQUAL("[DBG ][mygr]: hep", buf);
@@ -280,12 +310,18 @@ TEST(trace, active_level_debug)
280310

281311
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou");
282312
STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf);
313+
314+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn");
315+
STRCMP_EQUAL("[CRIT][mygr]: damn", buf);
283316
}
284317

285318
TEST(trace, active_level_info)
286319
{
287320
buf[0] = 0;
288321
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO);
322+
323+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi");
324+
STRCMP_EQUAL("", mbed_trace_last());
289325

290326
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
291327
STRCMP_EQUAL("", mbed_trace_last());
@@ -298,11 +334,17 @@ TEST(trace, active_level_info)
298334

299335
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou");
300336
STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf);
337+
338+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn");
339+
STRCMP_EQUAL("[CRIT][mygr]: damn", buf);
301340
}
302341

303342
TEST(trace, active_level_warn)
304343
{
305344
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_WARN);
345+
346+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi");
347+
STRCMP_EQUAL("", mbed_trace_last());
306348

307349
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
308350
STRCMP_EQUAL("", mbed_trace_last());
@@ -315,11 +357,17 @@ TEST(trace, active_level_warn)
315357

316358
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou");
317359
STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf);
360+
361+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn");
362+
STRCMP_EQUAL("[CRIT][mygr]: damn", buf);
318363
}
319364

320365
TEST(trace, active_level_error)
321366
{
322367
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ERROR);
368+
369+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi");
370+
STRCMP_EQUAL("", mbed_trace_last());
323371

324372
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
325373
STRCMP_EQUAL("", mbed_trace_last());
@@ -332,10 +380,16 @@ TEST(trace, active_level_error)
332380

333381
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou");
334382
STRCMP_EQUAL("[ERR ][mygr]: o'ou", buf);
383+
384+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn");
385+
STRCMP_EQUAL("[CRIT][mygr]: damn", buf);
335386
}
336387
TEST(trace, active_level_none)
337388
{
338389
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_NONE);
390+
391+
mbed_tracef(TRACE_LEVEL_SILLY, "mygr", "hoi");
392+
STRCMP_EQUAL("", mbed_trace_last());
339393

340394
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
341395
STRCMP_EQUAL("", mbed_trace_last());
@@ -348,6 +402,9 @@ TEST(trace, active_level_none)
348402

349403
mbed_tracef(TRACE_LEVEL_ERROR, "mygr", "o'ou");
350404
STRCMP_EQUAL("", mbed_trace_last());
405+
406+
mbed_tracef(TRACE_LEVEL_CRITICAL, "mygr", "damn");
407+
STRCMP_EQUAL("", mbed_trace_last());
351408
}
352409

353410
TEST(trace, active_level_all_1)

0 commit comments

Comments
 (0)