Skip to content

Commit

Permalink
Squashed 'features/FEATURE_COMMON_PAL/mbed-trace/' changes from 31e33…
Browse files Browse the repository at this point in the history
…8c23..af5f59cd2

af5f59cd2 Add CMakeLists.txt to support PAL Tools (#61)
24e16e766 Fix header documentation (#58)
23ea8f51c Update README.md (#57)
99aaa6eef Provide environment-agnostic configuration macros. (#54)
da9eb9f3a Merge pull request #53 from ARMmbed/max_trace_level
0a51dcae8 Flash size can be limited by defining MBED_TRACE_MAX_LEVEL

git-subtree-dir: features/FEATURE_COMMON_PAL/mbed-trace
git-subtree-split: af5f59cd2cbac4064875be0c339da114d88fd5e1
  • Loading branch information
Seppo Takalo committed Mar 16, 2017
1 parent 7a4b3d1 commit c8a16cc
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 46 deletions.
1 change: 1 addition & 0 deletions .yotta_ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*CMakeLists.txt
17 changes: 17 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
INCLUDE(CMakeForceCompiler)

cmake_minimum_required (VERSION 2.8)
SET(CMAKE_SYSTEM_NAME Generic)

project(mbedTrace)


include_directories(${CMAKE_CURRENT_SOURCE_DIR}/mbed-trace/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../nanostack-libservice/mbed-client-libservice/)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../nanostack-libservice/)

set (MBED_TRACE_SRC ${CMAKE_CURRENT_SOURCE_DIR}/source/mbed_trace.c)


CREATE_LIBRARY(mbedTrace "${MBED_TRACE_SRC}" "")

18 changes: 10 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ The purpose of the library is to provide a light, simple and general tracing sol

```
[DBG ][abc ]: This is a debug message from module abc<cr><lf>
[ERR ][abc ]: Something goes wrong in module abc<cr><lf>
[WARN][br ]: Oh no, br warning occurs!<cr><lf>
[INFO][br ]: Hi there.<cr><lf>
[WARN][br ]: Oh no, br warning occurs!<cr><lf>
[ERR ][abc ]: Something goes wrong in module abc<cr><lf>
```

## Usage
Expand All @@ -47,8 +47,10 @@ The purpose of the library is to provide a light, simple and general tracing sol
* To enable the tracing API:
* With yotta: set `YOTTA_CFG_MBED_TRACE` to 1 or true. Setting the flag to 0 or false disables tracing.
* [With mbed OS 5](#enabling-the-tracing-api-in-mbed-os-5)
* By default, trace uses 1024 bytes buffer for trace lines, but you can change it by yotta with: `YOTTA_CFG_MBED_TRACE_LINE_LENGTH`.
* To disable the IPv6 conversion, set `YOTTA_CFG_MBED_TRACE_FEA_IPV6 = 0`.
* 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.
* To disable the IPv6 conversion:
* With yotta: set `YOTTA_CFG_MBED_TRACE_FEA_IPV6 = 0`.
* With mbed OS 5: set `MBED_CONF_MBED_TRACE_FEA_IPV6 = 0`.
* 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.
* Call the trace initialization (`mbed_trace_init`) once before using any other APIs. It allocates the trace buffer and initializes the internal variables.
* 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.
Expand Down Expand Up @@ -82,9 +84,9 @@ When you want to print traces, use the `tr_<level>` macros. The macros behave li
Available levels:

* debug
* info
* warning
* error
* info
* 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 Expand Up @@ -128,7 +130,7 @@ See more in [mbed_trace.h](https://github.com/ARMmbed/mbed-trace/blob/master/mbe
## Usage example:
```c++
#define YOTTA_CFG_MBED_TRACE 1 //this can be defined also in the yotta configuration file config.json
#define MBED_CONF_MBED_TRACE_ENABLE 1 //this could be defined also in the mbed-cli configuration file mbed_app.json
#include "mbed-trace/mbed_trace.h"
#define TRACE_GROUP "main"
Expand All @@ -148,9 +150,9 @@ int main(void){
mbed_trace_mutex_release_function_set( my_mutex_release ); // only if thread safety is needed
mbed_trace_init(); // initialize the trace library
tr_debug("this is debug msg"); //-> "[DBG ][main]: this is a debug msg"
tr_err("this is error msg"); //-> "[ERR ][main]: this is an error msg"
tr_warn("this is warning msg"); //-> "[WARN][main]: this is a warning msg"
tr_info("this is info msg"); //-> "[INFO][main]: this is an info msg"
tr_warn("this is warning msg"); //-> "[WARN][main]: this is a warning msg"
tr_err("this is error msg"); //-> "[ERR ][main]: this is an error msg"
char arr[] = {30, 31, 32};
tr_debug("printing array: %s", mbed_trace_array(arr, 3)); //-> "[DBG ][main]: printing array: 01:02:03"
return 0;
Expand Down
52 changes: 43 additions & 9 deletions mbed-trace/mbed_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* \file mbed_trace.h
* Trace interface for MbedOS applications.
* This file provide simple but flexible way to handle software traces.
* Trace library are abstract layer, which use stdout (printf) by default,
* but outputs can be easily redirect to custom function, for example to
* Trace library are abstract layer, which use stdout (printf) by default,
* but outputs can be easily redirect to custom function, for example to
* store traces to memory or other interfaces.
*
* usage example:
Expand All @@ -30,14 +30,15 @@
* int main(void){
* mbed_trace_init(); // initialize trace library
* tr_debug("this is debug msg"); //print debug message to stdout: "[DBG]
* tr_err("this is error msg");
* tr_warn("this is warning msg");
* tr_info("this is info msg");
* tr_warn("this is warning msg");
* tr_err("this is error msg");
* return 0;
* }
* \endcode
* Activate with compiler flag: YOTTA_CFG_MBED_TRACE
* Configure trace line buffer size with compiler flag: YOTTA_CFG_MBED_TRACE_LINE_LENGTH. Default length: 1024.
* Limit the size of flash by setting MBED_TRACE_MAX_LEVEL value. Default is TRACE_LEVEL_DEBUG (all included)
*
*/
#ifndef MBED_TRACE_H_
Expand All @@ -63,12 +64,19 @@ extern "C" {

#ifndef YOTTA_CFG_MBED_TRACE_FEA_IPV6
#define YOTTA_CFG_MBED_TRACE_FEA_IPV6 1
#else
#warning YOTTA_CFG_MBED_TRACE_FEA_IPV6 is deprecated and will be removed in the future! Use MBED_CONF_MBED_TRACE_FEA_IPV6 instead.
#define MBED_CONF_MBED_TRACE_FEA_IPV6 YOTTA_CFG_MBED_TRACE_FEA_IPV6
#endif

#ifndef MBED_CONF_MBED_TRACE_ENABLE
#define MBED_CONF_MBED_TRACE_ENABLE 0
#endif

#ifndef MBED_CONF_MBED_TRACE_FEA_IPV6
#define MBED_CONF_MBED_TRACE_FEA_IPV6 1
#endif

/** 3 upper bits are trace modes related,
and 5 lower bits are trace level configuration */

Expand Down Expand Up @@ -110,13 +118,39 @@ extern "C" {
/** 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
#endif

//usage macros:
#define tr_info(...) mbed_tracef(TRACE_LEVEL_INFO, TRACE_GROUP, __VA_ARGS__) //!< Print info message
#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_DEBUG
#define tr_debug(...) mbed_tracef(TRACE_LEVEL_DEBUG, TRACE_GROUP, __VA_ARGS__) //!< Print debug message
#else
#define tr_debug(...)
#endif

#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_INFO
#define tr_info(...) mbed_tracef(TRACE_LEVEL_INFO, TRACE_GROUP, __VA_ARGS__) //!< Print info message
#else
#define tr_info(...)
#endif

#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_WARN
#define tr_warning(...) mbed_tracef(TRACE_LEVEL_WARN, TRACE_GROUP, __VA_ARGS__) //!< Print warning message
#define tr_warn(...) mbed_tracef(TRACE_LEVEL_WARN, TRACE_GROUP, __VA_ARGS__) //!< Alternative warning message
#else
#define tr_warning(...)
#define tr_warn(...)
#endif

#if MBED_TRACE_MAX_LEVEL >= TRACE_LEVEL_ERROR
#define tr_error(...) mbed_tracef(TRACE_LEVEL_ERROR, TRACE_GROUP, __VA_ARGS__) //!< Print Error Message
#define tr_err(...) mbed_tracef(TRACE_LEVEL_ERROR, TRACE_GROUP, __VA_ARGS__) //!< Alternative error message
#else
#define tr_error(...)
#define tr_err(...)
#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 @@ -180,7 +214,7 @@ void mbed_trace_buffer_sizes(int lineLength, int tmpLength);
* @endcode
*/
void mbed_trace_config_set(uint8_t config);
/** get trace configurations
/** get trace configurations
* @return trace configuration byte
*/
uint8_t mbed_trace_config_get(void);
Expand Down Expand Up @@ -232,7 +266,7 @@ void mbed_trace_mutex_release_function_set(void (*mutex_release_f)(void));
/**
* When trace group contains text in filters,
* trace print will be ignored.
* e.g.:
* e.g.:
* mbed_trace_exclude_filters_set("mygr");
* mbed_tracef(TRACE_ACTIVE_LEVEL_DEBUG, "ougr", "This is not printed");
*/
Expand Down Expand Up @@ -294,7 +328,7 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap);
* Get last trace from buffer
*/
const char* mbed_trace_last(void);
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
/**
* mbed_tracef helping function for convert ipv6
* table to human readable string.
Expand Down Expand Up @@ -327,7 +361,7 @@ char* mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len);
* @param buf hex array pointer
* @param len buffer length
* @return temporary buffer where string copied
* if array as string not fit to temp buffer, this function write '*' as last character,
* if array as string not fit to temp buffer, this function write '*' as last character,
* which indicate that buffer is too small for array.
*/
char* mbed_trace_array(const uint8_t* buf, uint16_t len);
Expand Down
5 changes: 5 additions & 0 deletions mbed_lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"enable": {
"help": "Used to globally enable traces.",
"value": null
},
"fea-ipv6": {
"help": "Used to globally disable ipv6 tracing features.",
"value": null
}

}
}
75 changes: 49 additions & 26 deletions source/mbed_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
#include <string.h>
#include <stdarg.h>

#ifndef YOTTA_CFG_MBED_TRACE
#define YOTTA_CFG_MBED_TRACE 1
#define YOTTA_CFG_MBED_TRACE_FEA_IPV6 1
#ifdef MBED_CONF_MBED_TRACE_ENABLE
#undef MBED_CONF_MBED_TRACE_ENABLE
#endif
#define MBED_CONF_MBED_TRACE_ENABLE 1
#ifndef MBED_CONF_MBED_TRACE_FEA_IPV6
#define MBED_CONF_MBED_TRACE_FEA_IPV6 1
#endif

#include "mbed-trace/mbed_trace.h"
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
#include "mbed-client-libservice/ip6string.h"
#include "mbed-client-libservice/common_functions.h"
#endif
Expand Down Expand Up @@ -52,23 +55,42 @@
#define VT100_COLOR_DEBUG "\x1b[90m"

/** default max trace line size in bytes */
#ifdef YOTTA_CFG_MBED_TRACE_LINE_LENGTH
#ifdef MBED_TRACE_LINE_LENGTH
#define DEFAULT_TRACE_LINE_LENGTH MBED_TRACE_LINE_LENGTH
#elif defined YOTTA_CFG_MBED_TRACE_LINE_LENGTH
#warning YOTTA_CFG_MBED_TRACE_LINE_LENGTH is deprecated and will be removed in the future! Use MBED_TRACE_LINE_LENGTH instead.
#define DEFAULT_TRACE_LINE_LENGTH YOTTA_CFG_MBED_TRACE_LINE_LENGTH
#else
#define DEFAULT_TRACE_LINE_LENGTH 1024
#endif

/** default max temporary buffer size in bytes, used in
trace_ipv6, trace_ipv6_prefix and trace_array */
#ifdef YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
#ifdef MBED_TRACE_TMP_LINE_LENGTH
#define DEFAULT_TRACE_TMP_LINE_LEN MBED_TRACE_TMP_LINE_LENGTH
#elif defined YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
#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.
#define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN
#elif defined YOTTA_CFG_MTRACE_TMP_LINE_LEN
#warning The YOTTA_CFG_MTRACE_TMP_LINE_LEN flag is deprecated! Use YOTTA_CFG_MBED_TRACE_TMP_LINE_LEN instead.
#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.
#define DEFAULT_TRACE_TMP_LINE_LEN YOTTA_CFG_MTRACE_TMP_LINE_LEN
#else
#define DEFAULT_TRACE_TMP_LINE_LEN 128
#endif

/** default max filters (include/exclude) length in bytes */
#ifdef MBED_TRACE_FILTER_LENGTH
#define DEFAULT_TRACE_FILTER_LENGTH MBED_TRACE_FILTER_LENGTH
#else
#define DEFAULT_TRACE_FILTER_LENGTH 24
#endif

/** default trace configuration bitmask */
#ifdef MBED_TRACE_CONFIG
#define DEFAULT_TRACE_CONFIG MBED_TRACE_CONFIG
#else
#define DEFAULT_TRACE_CONFIG TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN
#endif

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

static trace_t m_trace = {
.trace_config = DEFAULT_TRACE_CONFIG,
.filters_exclude = 0,
.filters_include = 0,
.filters_length = DEFAULT_TRACE_FILTER_LENGTH,
.line = 0,
.line_length = DEFAULT_TRACE_LINE_LENGTH,
.tmp_data = 0,
.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN,
.prefix_f = 0,
.suffix_f = 0,
.printf = 0,
.printf = mbed_trace_default_print,
.cmd_printf = 0,
.mutex_wait_f = 0,
.mutex_release_f = 0,
Expand All @@ -127,17 +153,15 @@ static trace_t m_trace = {

int mbed_trace_init(void)
{
m_trace.trace_config = TRACE_MODE_COLOR | TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN;
m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
if (m_trace.line == NULL) {
m_trace.line = MBED_TRACE_MEM_ALLOC(m_trace.line_length);
}
m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;

if (m_trace.tmp_data == NULL) {
m_trace.tmp_data = MBED_TRACE_MEM_ALLOC(m_trace.tmp_data_length);
}
m_trace.tmp_data_ptr = m_trace.tmp_data;
m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;

if (m_trace.filters_exclude == NULL) {
m_trace.filters_exclude = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
}
Expand All @@ -158,29 +182,28 @@ int mbed_trace_init(void)
memset(m_trace.filters_include, 0, m_trace.filters_length);
memset(m_trace.line, 0, m_trace.line_length);

m_trace.prefix_f = 0;
m_trace.suffix_f = 0;
m_trace.printf = mbed_trace_default_print;
m_trace.cmd_printf = 0;

return 0;
}
void mbed_trace_free(void)
{
// release memory
MBED_TRACE_MEM_FREE(m_trace.line);
m_trace.line_length = 0;
m_trace.line = 0;
MBED_TRACE_MEM_FREE(m_trace.tmp_data);
m_trace.tmp_data = 0;
m_trace.tmp_data_ptr = 0;
MBED_TRACE_MEM_FREE(m_trace.filters_exclude);
m_trace.filters_exclude = 0;
MBED_TRACE_MEM_FREE(m_trace.filters_include);

// reset to default values
m_trace.trace_config = DEFAULT_TRACE_CONFIG;
m_trace.filters_exclude = 0;
m_trace.filters_include = 0;
m_trace.filters_length = 0;
m_trace.filters_length = DEFAULT_TRACE_FILTER_LENGTH;
m_trace.line = 0;
m_trace.line_length = DEFAULT_TRACE_LINE_LENGTH;
m_trace.tmp_data = 0;
m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
m_trace.prefix_f = 0;
m_trace.suffix_f = 0;
m_trace.printf = mbed_trace_default_print;
m_trace.printf = mbed_trace_default_print;
m_trace.cmd_printf = 0;
m_trace.mutex_wait_f = 0;
m_trace.mutex_release_f = 0;
Expand Down Expand Up @@ -480,7 +503,7 @@ const char *mbed_trace_last(void)
}
/* Helping functions */
#define tmp_data_left() m_trace.tmp_data_length-(m_trace.tmp_data_ptr-m_trace.tmp_data)
#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
#if MBED_CONF_MBED_TRACE_FEA_IPV6 == 1
char *mbed_trace_ipv6(const void *addr_ptr)
{
/** Acquire mutex. It is released before returning from mbed_vtracef. */
Expand Down Expand Up @@ -524,7 +547,7 @@ char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
m_trace.tmp_data_ptr += ip6_prefix_tos(prefix, prefix_len, str) + 1;
return str;
}
#endif //YOTTA_CFG_MBED_TRACE_FEA_IPV6
#endif //MBED_CONF_MBED_TRACE_FEA_IPV6
char *mbed_trace_array(const uint8_t *buf, uint16_t len)
{
/** Acquire mutex. It is released before returning from mbed_vtracef. */
Expand Down
Loading

0 comments on commit c8a16cc

Please sign in to comment.