A blue button is always a nice toy ...
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Fork of BLE_HeartRate_IDB0XA1 by
bricks/trace.cpp@29:a6b74dfdd5f2, 2017-10-01 (annotated)
- Committer:
- hux
- Date:
- Sun Oct 01 12:49:25 2017 +0000
- Revision:
- 29:a6b74dfdd5f2
A blue button is always a nice toy ...
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hux | 29:a6b74dfdd5f2 | 1 | // trace.cpp - trace a message depending on verbose level |
hux | 29:a6b74dfdd5f2 | 2 | // |
hux | 29:a6b74dfdd5f2 | 3 | |
hux | 29:a6b74dfdd5f2 | 4 | #include <stdio.h> |
hux | 29:a6b74dfdd5f2 | 5 | #include "bricks/trace.h" |
hux | 29:a6b74dfdd5f2 | 6 | |
hux | 29:a6b74dfdd5f2 | 7 | static Serial pc(USBTX, USBRX); // serial port to PC terminal |
hux | 29:a6b74dfdd5f2 | 8 | |
hux | 29:a6b74dfdd5f2 | 9 | //============================================================================== |
hux | 29:a6b74dfdd5f2 | 10 | // Managing the Verbose Lewvel |
hux | 29:a6b74dfdd5f2 | 11 | //============================================================================== |
hux | 29:a6b74dfdd5f2 | 12 | |
hux | 29:a6b74dfdd5f2 | 13 | static int threshold = 0; // verbose threshold, no verbose messages |
hux | 29:a6b74dfdd5f2 | 14 | |
hux | 29:a6b74dfdd5f2 | 15 | void verbose(O&o, int level) // setup verbose level |
hux | 29:a6b74dfdd5f2 | 16 | { |
hux | 29:a6b74dfdd5f2 | 17 | threshold = level; // update verbose threshold |
hux | 29:a6b74dfdd5f2 | 18 | } |
hux | 29:a6b74dfdd5f2 | 19 | |
hux | 29:a6b74dfdd5f2 | 20 | //============================================================================== |
hux | 29:a6b74dfdd5f2 | 21 | // Printing Trace Messages |
hux | 29:a6b74dfdd5f2 | 22 | //============================================================================== |
hux | 29:a6b74dfdd5f2 | 23 | |
hux | 29:a6b74dfdd5f2 | 24 | void trace(O&o, int level, const char *msg) // trace a message |
hux | 29:a6b74dfdd5f2 | 25 | { |
hux | 29:a6b74dfdd5f2 | 26 | if (level <= threshold) // update verbose threshold |
hux | 29:a6b74dfdd5f2 | 27 | { char buf[2] = " "; // must be terminated |
hux | 29:a6b74dfdd5f2 | 28 | |
hux | 29:a6b74dfdd5f2 | 29 | for (; *msg; msg++) |
hux | 29:a6b74dfdd5f2 | 30 | { buf[0] = *msg; |
hux | 29:a6b74dfdd5f2 | 31 | printf(buf); |
hux | 29:a6b74dfdd5f2 | 32 | if (*msg == '\n') |
hux | 29:a6b74dfdd5f2 | 33 | { buf[0] = '\r'; // auto adding carriage return |
hux | 29:a6b74dfdd5f2 | 34 | printf(buf); |
hux | 29:a6b74dfdd5f2 | 35 | } |
hux | 29:a6b74dfdd5f2 | 36 | } |
hux | 29:a6b74dfdd5f2 | 37 | } |
hux | 29:a6b74dfdd5f2 | 38 | } |
hux | 29:a6b74dfdd5f2 | 39 | |
hux | 29:a6b74dfdd5f2 | 40 | void trace(O&o, int level, const char*format, int value) |
hux | 29:a6b74dfdd5f2 | 41 | { |
hux | 29:a6b74dfdd5f2 | 42 | char buf[80]; |
hux | 29:a6b74dfdd5f2 | 43 | if (strlen(format) > 70) |
hux | 29:a6b74dfdd5f2 | 44 | trace(o,level," *** buffer overflow *** "); |
hux | 29:a6b74dfdd5f2 | 45 | else |
hux | 29:a6b74dfdd5f2 | 46 | { sprintf(buf,format,value); |
hux | 29:a6b74dfdd5f2 | 47 | trace(o,level,buf); |
hux | 29:a6b74dfdd5f2 | 48 | } |
hux | 29:a6b74dfdd5f2 | 49 | } |
hux | 29:a6b74dfdd5f2 | 50 | |
hux | 29:a6b74dfdd5f2 | 51 | void traceln(O&o, int level, const char*msg) // trace with CR & LF |
hux | 29:a6b74dfdd5f2 | 52 | { |
hux | 29:a6b74dfdd5f2 | 53 | trace(o,level,msg); trace(o,level,"\r\n"); |
hux | 29:a6b74dfdd5f2 | 54 | } |