Skip to content

Commit 4b04a81

Browse files
authored
Merge pull request #3939 from ARMmbed/update_mbed-trace
Update mbed trace
2 parents ead3170 + da80d6b commit 4b04a81

File tree

7 files changed

+160
-46
lines changed

7 files changed

+160
-46
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*CMakeLists.txt
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
INCLUDE(CMakeForceCompiler)
2+
3+
cmake_minimum_required (VERSION 2.8)
4+
SET(CMAKE_SYSTEM_NAME Generic)
5+
6+
project(mbedTrace)
7+
8+
9+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/mbed-trace/)
10+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../nanostack-libservice/mbed-client-libservice/)
11+
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../nanostack-libservice/)
12+
13+
set (MBED_TRACE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/source/mbed_trace.c)
14+
15+
16+
CREATE_LIBRARY(mbedTrace "${MBED_TRACE_SRC}" "")
17+

features/FEATURE_COMMON_PAL/mbed-trace/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ The purpose of the library is to provide a light, simple and general tracing sol
3333

3434
```
3535
[DBG ][abc ]: This is a debug message from module abc<cr><lf>
36-
[ERR ][abc ]: Something goes wrong in module abc<cr><lf>
37-
[WARN][br ]: Oh no, br warning occurs!<cr><lf>
3836
[INFO][br ]: Hi there.<cr><lf>
37+
[WARN][br ]: Oh no, br warning occurs!<cr><lf>
38+
[ERR ][abc ]: Something goes wrong in module abc<cr><lf>
3939
```
4040

4141
## Usage
@@ -47,8 +47,10 @@ The purpose of the library is to provide a light, simple and general tracing sol
4747
* To enable the tracing API:
4848
* With yotta: set `YOTTA_CFG_MBED_TRACE` to 1 or true. Setting the flag to 0 or false disables tracing.
4949
* [With mbed OS 5](#enabling-the-tracing-api-in-mbed-os-5)
50-
* By default, trace uses 1024 bytes buffer for trace lines, but you can change it by yotta with: `YOTTA_CFG_MBED_TRACE_LINE_LENGTH`.
51-
* To disable the IPv6 conversion, set `YOTTA_CFG_MBED_TRACE_FEA_IPV6 = 0`.
50+
* By default, trace uses 1024 bytes buffer for trace lines, but you can change it by setting the configuration macro `MBED_TRACE_LINE_LENGTH` to the desired value.
51+
* To disable the IPv6 conversion:
52+
* With yotta: set `YOTTA_CFG_MBED_TRACE_FEA_IPV6 = 0`.
53+
* With mbed OS 5: set `MBED_CONF_MBED_TRACE_FEA_IPV6 = 0`.
5254
* If thread safety is needed, configure the wait and release callback functions before initialization to enable the protection. Usually, this needs to be done only once in the application's lifetime.
5355
* Call the trace initialization (`mbed_trace_init`) once before using any other APIs. It allocates the trace buffer and initializes the internal variables.
5456
* Define `TRACE_GROUP` in your source code (not in the header!) to use traces. It is a 1-4 characters long char-array (for example `#define TRACE_GROUP "APPL"`). This will be printed on every trace line.
@@ -82,9 +84,9 @@ When you want to print traces, use the `tr_<level>` macros. The macros behave li
8284
Available levels:
8385

8486
* debug
87+
* info
8588
* warning
8689
* error
87-
* info
8890
* cmdline (special behavior, should not be used)
8991

9092
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:
@@ -128,7 +130,7 @@ See more in [mbed_trace.h](https://github.com/ARMmbed/mbed-trace/blob/master/mbe
128130
## Usage example:
129131
130132
```c++
131-
#define YOTTA_CFG_MBED_TRACE 1 //this can be defined also in the yotta configuration file config.json
133+
#define MBED_CONF_MBED_TRACE_ENABLE 1 //this could be defined also in the mbed-cli configuration file mbed_app.json
132134
#include "mbed-trace/mbed_trace.h"
133135
#define TRACE_GROUP "main"
134136
@@ -148,9 +150,9 @@ int main(void){
148150
mbed_trace_mutex_release_function_set( my_mutex_release ); // only if thread safety is needed
149151
mbed_trace_init(); // initialize the trace library
150152
tr_debug("this is debug msg"); //-> "[DBG ][main]: this is a debug msg"
151-
tr_err("this is error msg"); //-> "[ERR ][main]: this is an error msg"
152-
tr_warn("this is warning msg"); //-> "[WARN][main]: this is a warning msg"
153153
tr_info("this is info msg"); //-> "[INFO][main]: this is an info msg"
154+
tr_warn("this is warning msg"); //-> "[WARN][main]: this is a warning msg"
155+
tr_err("this is error msg"); //-> "[ERR ][main]: this is an error msg"
154156
char arr[] = {30, 31, 32};
155157
tr_debug("printing array: %s", mbed_trace_array(arr, 3)); //-> "[DBG ][main]: printing array: 01:02:03"
156158
return 0;

features/FEATURE_COMMON_PAL/mbed-trace/mbed-trace/mbed_trace.h

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
* \file mbed_trace.h
1919
* Trace interface for MbedOS applications.
2020
* This file provide simple but flexible way to handle software traces.
21-
* Trace library are abstract layer, which use stdout (printf) by default,
22-
* but outputs can be easily redirect to custom function, for example to
21+
* Trace library are abstract layer, which use stdout (printf) by default,
22+
* but outputs can be easily redirect to custom function, for example to
2323
* store traces to memory or other interfaces.
2424
*
2525
* usage example:
@@ -30,14 +30,15 @@
3030
* int main(void){
3131
* mbed_trace_init(); // initialize trace library
3232
* tr_debug("this is debug msg"); //print debug message to stdout: "[DBG]
33-
* tr_err("this is error msg");
34-
* tr_warn("this is warning msg");
3533
* tr_info("this is info msg");
34+
* tr_warn("this is warning msg");
35+
* tr_err("this is error msg");
3636
* return 0;
3737
* }
3838
* \endcode
3939
* Activate with compiler flag: YOTTA_CFG_MBED_TRACE
4040
* Configure trace line buffer size with compiler flag: YOTTA_CFG_MBED_TRACE_LINE_LENGTH. Default length: 1024.
41+
* Limit the size of flash by setting MBED_TRACE_MAX_LEVEL value. Default is TRACE_LEVEL_DEBUG (all included)
4142
*
4243
*/
4344
#ifndef MBED_TRACE_H_
@@ -63,12 +64,19 @@ extern "C" {
6364

6465
#ifndef YOTTA_CFG_MBED_TRACE_FEA_IPV6
6566
#define YOTTA_CFG_MBED_TRACE_FEA_IPV6 1
67+
#else
68+
#warning YOTTA_CFG_MBED_TRACE_FEA_IPV6 is deprecated and will be removed in the future! Use MBED_CONF_MBED_TRACE_FEA_IPV6 instead.
69+
#define MBED_CONF_MBED_TRACE_FEA_IPV6 YOTTA_CFG_MBED_TRACE_FEA_IPV6
6670
#endif
6771

6872
#ifndef MBED_CONF_MBED_TRACE_ENABLE
6973
#define MBED_CONF_MBED_TRACE_ENABLE 0
7074
#endif
7175

76+
#ifndef MBED_CONF_MBED_TRACE_FEA_IPV6
77+
#define MBED_CONF_MBED_TRACE_FEA_IPV6 1
78+
#endif
79+
7280
/** 3 upper bits are trace modes related,
7381
and 5 lower bits are trace level configuration */
7482

@@ -110,13 +118,39 @@ extern "C" {
110118
/** special level for cmdline. Behaviours like "plain mode" */
111119
#define TRACE_LEVEL_CMD 0x01
112120

121+
#ifndef MBED_TRACE_MAX_LEVEL
122+
#define MBED_TRACE_MAX_LEVEL TRACE_LEVEL_DEBUG
123+
#endif
124+
113125
//usage macros:
114-
#define tr_info(...) mbed_tracef(TRACE_LEVEL_INFO, TRACE_GROUP, __VA_ARGS__) //!< Print info message
126+
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_DEBUG
115127
#define tr_debug(...) mbed_tracef(TRACE_LEVEL_DEBUG, TRACE_GROUP, __VA_ARGS__) //!< Print debug message
128+
#else
129+
#define tr_debug(...)
130+
#endif
131+
132+
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_INFO
133+
#define tr_info(...) mbed_tracef(TRACE_LEVEL_INFO, TRACE_GROUP, __VA_ARGS__) //!< Print info message
134+
#else
135+
#define tr_info(...)
136+
#endif
137+
138+
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_WARN
116139
#define tr_warning(...) mbed_tracef(TRACE_LEVEL_WARN, TRACE_GROUP, __VA_ARGS__) //!< Print warning message
117140
#define tr_warn(...) mbed_tracef(TRACE_LEVEL_WARN, TRACE_GROUP, __VA_ARGS__) //!< Alternative warning message
141+
#else
142+
#define tr_warning(...)
143+
#define tr_warn(...)
144+
#endif
145+
146+
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_ERROR
118147
#define tr_error(...) mbed_tracef(TRACE_LEVEL_ERROR, TRACE_GROUP, __VA_ARGS__) //!< Print Error Message
119148
#define tr_err(...) mbed_tracef(TRACE_LEVEL_ERROR, TRACE_GROUP, __VA_ARGS__) //!< Alternative error message
149+
#else
150+
#define tr_error(...)
151+
#define tr_err(...)
152+
#endif
153+
120154
#define tr_cmdline(...) mbed_tracef(TRACE_LEVEL_CMD, TRACE_GROUP, __VA_ARGS__) //!< Special print for cmdline. See more from TRACE_LEVEL_CMD -level
121155

122156
//aliases for the most commonly used functions and the helper functions
@@ -180,7 +214,7 @@ void mbed_trace_buffer_sizes(int lineLength, int tmpLength);
180214
* @endcode
181215
*/
182216
void mbed_trace_config_set(uint8_t config);
183-
/** get trace configurations
217+
/** get trace configurations
184218
* @return trace configuration byte
185219
*/
186220
uint8_t mbed_trace_config_get(void);
@@ -232,7 +266,7 @@ void mbed_trace_mutex_release_function_set(void (*mutex_release_f)(void));
232266
/**
233267
* When trace group contains text in filters,
234268
* trace print will be ignored.
235-
* e.g.:
269+
* e.g.:
236270
* mbed_trace_exclude_filters_set("mygr");
237271
* mbed_tracef(TRACE_ACTIVE_LEVEL_DEBUG, "ougr", "This is not printed");
238272
*/
@@ -294,7 +328,7 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap);
294328
* Get last trace from buffer
295329
*/
296330
const char* mbed_trace_last(void);
297-
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
331+
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
298332
/**
299333
* mbed_tracef helping function for convert ipv6
300334
* table to human readable string.
@@ -327,7 +361,7 @@ char* mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len);
327361
* @param buf hex array pointer
328362
* @param len buffer length
329363
* @return temporary buffer where string copied
330-
* if array as string not fit to temp buffer, this function write '*' as last character,
364+
* if array as string not fit to temp buffer, this function write '*' as last character,
331365
* which indicate that buffer is too small for array.
332366
*/
333367
char* mbed_trace_array(const uint8_t* buf, uint16_t len);

features/FEATURE_COMMON_PAL/mbed-trace/mbed_lib.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
"enable": {
55
"help": "Used to globally enable traces.",
66
"value": null
7+
},
8+
"fea-ipv6": {
9+
"help": "Used to globally disable ipv6 tracing features.",
10+
"value": null
711
}
12+
813
}
914
}

features/FEATURE_COMMON_PAL/mbed-trace/source/mbed_trace.c

Lines changed: 49 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
#include <string.h>
1818
#include <stdarg.h>
1919

20-
#ifndef YOTTA_CFG_MBED_TRACE
21-
#define YOTTA_CFG_MBED_TRACE 1
22-
#define YOTTA_CFG_MBED_TRACE_FEA_IPV6 1
20+
#ifdef MBED_CONF_MBED_TRACE_ENABLE
21+
#undef MBED_CONF_MBED_TRACE_ENABLE
22+
#endif
23+
#define MBED_CONF_MBED_TRACE_ENABLE 1
24+
#ifndef MBED_CONF_MBED_TRACE_FEA_IPV6
25+
#define MBED_CONF_MBED_TRACE_FEA_IPV6 1
2326
#endif
2427

2528
#include "mbed-trace/mbed_trace.h"
26-
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
29+
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
2730
#include "mbed-client-libservice/ip6string.h"
2831
#include "mbed-client-libservice/common_functions.h"
2932
#endif
@@ -52,23 +55,42 @@
5255
#define VT100_COLOR_DEBUG "\x1b[90m"
5356

5457
/** default max trace line size in bytes */
55-
#ifdef YOTTA_CFG_MBED_TRACE_LINE_LENGTH
58+
#ifdef MBED_TRACE_LINE_LENGTH
59+
#define DEFAULT_TRACE_LINE_LENGTH MBED_TRACE_LINE_LENGTH
60+
#elif defined YOTTA_CFG_MBED_TRACE_LINE_LENGTH
61+
#warning YOTTA_CFG_MBED_TRACE_LINE_LENGTH is deprecated and will be removed in the future! Use MBED_TRACE_LINE_LENGTH instead.
5662
#define DEFAULT_TRACE_LINE_LENGTH YOTTA_CFG_MBED_TRACE_LINE_LENGTH
5763
#else
5864
#define DEFAULT_TRACE_LINE_LENGTH 1024
5965
#endif
66+
6067
/** default max temporary buffer size in bytes, used in
6168
trace_ipv6, trace_ipv6_prefix and trace_array */
62-
#ifdef YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
69+
#ifdef MBED_TRACE_TMP_LINE_LENGTH
70+
#define DEFAULT_TRACE_TMP_LINE_LEN MBED_TRACE_TMP_LINE_LENGTH
71+
#elif defined YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
72+
#warning The YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN flag is deprecated and will be removed in the future! Use MBED_TRACE_TMP_LINE_LENGTH instead.
6373
#define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
6474
#elif defined YOTTA_CFG_MTRACE_TMP_LINE_LEN
65-
#warning The YOTTA_CFG_MTRACE_TMP_LINE_LEN flag is deprecated! Use YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN instead.
75+
#warning The YOTTA_CFG_MTRACE_TMP_LINE_LEN flag is deprecated and will be removed in the future! Use MBED_TRACE_TMP_LINE_LENGTH instead.
6676
#define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MTRACE_TMP_LINE_LEN
6777
#else
6878
#define DEFAULT_TRACE_TMP_LINE_LEN 128
6979
#endif
80+
7081
/** default max filters (include/exclude) length in bytes */
82+
#ifdef MBED_TRACE_FILTER_LENGTH
83+
#define DEFAULT_TRACE_FILTER_LENGTH MBED_TRACE_FILTER_LENGTH
84+
#else
7185
#define DEFAULT_TRACE_FILTER_LENGTH 24
86+
#endif
87+
88+
/** default trace configuration bitmask */
89+
#ifdef MBED_TRACE_CONFIG
90+
#define DEFAULT_TRACE_CONFIG MBED_TRACE_CONFIG
91+
#else
92+
#define DEFAULT_TRACE_CONFIG TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN
93+
#endif
7294

7395
/** default print function, just redirect str to printf */
7496
static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length);
@@ -112,13 +134,17 @@ typedef struct trace_s {
112134
} trace_t;
113135

114136
static trace_t m_trace = {
137+
.trace_config = DEFAULT_TRACE_CONFIG,
115138
.filters_exclude = 0,
116139
.filters_include = 0,
140+
.filters_length = DEFAULT_TRACE_FILTER_LENGTH,
117141
.line = 0,
142+
.line_length = DEFAULT_TRACE_LINE_LENGTH,
118143
.tmp_data = 0,
144+
.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN,
119145
.prefix_f = 0,
120146
.suffix_f = 0,
121-
.printf = 0,
147+
.printf = mbed_trace_default_print,
122148
.cmd_printf = 0,
123149
.mutex_wait_f = 0,
124150
.mutex_release_f = 0,
@@ -127,17 +153,15 @@ static trace_t m_trace = {
127153

128154
int mbed_trace_init(void)
129155
{
130-
m_trace.trace_config = TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN;
131-
m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
132156
if (m_trace.line == NULL) {
133157
m_trace.line = MBED_TRACE_MEM_ALLOC(m_trace.line_length);
134158
}
135-
m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
159+
136160
if (m_trace.tmp_data == NULL) {
137161
m_trace.tmp_data = MBED_TRACE_MEM_ALLOC(m_trace.tmp_data_length);
138162
}
139163
m_trace.tmp_data_ptr = m_trace.tmp_data;
140-
m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;
164+
141165
if (m_trace.filters_exclude == NULL) {
142166
m_trace.filters_exclude = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
143167
}
@@ -158,29 +182,28 @@ int mbed_trace_init(void)
158182
memset(m_trace.filters_include, 0, m_trace.filters_length);
159183
memset(m_trace.line, 0, m_trace.line_length);
160184

161-
m_trace.prefix_f = 0;
162-
m_trace.suffix_f = 0;
163-
m_trace.printf = mbed_trace_default_print;
164-
m_trace.cmd_printf = 0;
165-
166185
return 0;
167186
}
168187
void mbed_trace_free(void)
169188
{
189+
// release memory
170190
MBED_TRACE_MEM_FREE(m_trace.line);
171-
m_trace.line_length = 0;
172-
m_trace.line = 0;
173191
MBED_TRACE_MEM_FREE(m_trace.tmp_data);
174-
m_trace.tmp_data = 0;
175-
m_trace.tmp_data_ptr = 0;
176192
MBED_TRACE_MEM_FREE(m_trace.filters_exclude);
177-
m_trace.filters_exclude = 0;
178193
MBED_TRACE_MEM_FREE(m_trace.filters_include);
194+
195+
// reset to default values
196+
m_trace.trace_config = DEFAULT_TRACE_CONFIG;
197+
m_trace.filters_exclude = 0;
179198
m_trace.filters_include = 0;
180-
m_trace.filters_length = 0;
199+
m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;
200+
m_trace.line = 0;
201+
m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
202+
m_trace.tmp_data = 0;
203+
m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
181204
m_trace.prefix_f = 0;
182205
m_trace.suffix_f = 0;
183-
m_trace.printf = mbed_trace_default_print;
206+
m_trace.printf = mbed_trace_default_print;
184207
m_trace.cmd_printf = 0;
185208
m_trace.mutex_wait_f = 0;
186209
m_trace.mutex_release_f = 0;
@@ -480,7 +503,7 @@ const char *mbed_trace_last(void)
480503
}
481504
/* Helping functions */
482505
#define tmp_data_left() m_trace.tmp_data_length-(m_trace.tmp_data_ptr-m_trace.tmp_data)
483-
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
506+
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
484507
char *mbed_trace_ipv6(const void *addr_ptr)
485508
{
486509
/** Acquire mutex. It is released before returning from mbed_vtracef. */
@@ -524,7 +547,7 @@ char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
524547
m_trace.tmp_data_ptr += ip6_prefix_tos(prefix, prefix_len, str) + 1;
525548
return str;
526549
}
527-
#endif //YOTTA_CFG_MBED_TRACE_FEA_IPV6
550+
#endif //MBED_CONF_MBED_TRACE_FEA_IPV6
528551
char *mbed_trace_array(const uint8_t *buf, uint16_t len)
529552
{
530553
/** Acquire mutex. It is released before returning from mbed_vtracef. */

0 commit comments

Comments
 (0)