Skip to content

Commit

Permalink
Merge pull request #1118 from bitcraze/krichardsson/debug-logs
Browse files Browse the repository at this point in the history
Add debug log macros
  • Loading branch information
krichardsson authored Sep 20, 2022
2 parents 732713b + 1ab3474 commit 7d9f984
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
62 changes: 60 additions & 2 deletions docs/userguides/logparam.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ log data from the Crazyflie and to set variables during runtime.

The variables that are available for the logging/parameter framework are
defined at compile-time for the Crazyflie firmware. C macros are used to define
which variables that should be available to the framework.
which variables that should be available to the framework.

A parameter or logging variable that is created with `PARAM_ADD_CORE` or `LOG_ADD_CORE` is considered stable API and will with a very high likelihood be available a cross firmware versions. All core parameters and logging variables must have documentation associated with it. None-core, development, parameter and logging variables should be indicated with only `PARAM_ADD` and `LOG_ADD`, which should indicate that no guarantee is given that this values will stay in later versions. See below for examples of the documentation syntax.

Expand Down Expand Up @@ -227,7 +227,7 @@ specifed in the logging configuration. The rate, on the other hand, is calculate
last computation exceeds the configured update time of the rate logger, and if the logging intervall
is longer than the update intervall, updates will be done for each logging call.

### Parameter callback function to get notifed when a parameter has been updated.
### Parameter callback function to get notified when a parameter has been updated.

Using the macro `PARAM_ADD_WITH_CALLBACK` it is possible to register a callback function that will be called
when the parameter gets updated. This callback will run from the parameter task so it is important to not
Expand All @@ -244,3 +244,61 @@ Example:
...

PARAM_ADD_WITH_CALLBACK(PARAM_UINT8, setIO1pin, &pinValue, &myCallbackFunction)

### Debug logging

Sometimes it is useful to log data when debugging a module, but we want the logs to be disabled by default to avoid
adding to the size of the TOC. There is a set of macros to support this, all ending in `_DEBUG`.

``` C
LOG_ADD_DEBUG(TYPE, NAME, ADDRESS)
STATS_CNT_RATE_EVENT_DEBUG(LOGGER)
STATS_CNT_RATE_MULTI_EVENT_DEBUG(LOGGER, CNT)
STATS_CNT_RATE_LOG_ADD_DEBUG(NAME, LOGGER)
```
These macros behaves the same way as the "normal" macros without the `_DEBUG` extension, but they are ignored unless
`CONFIG_DEBUG_LOG_ENABLE` is defined.
The intended use is to add a define in a C file **before** the log.h or statsCnt.h is included to enable the macros.
Example:
``` C
// Uncomment this line to enable extra logs when debugging
// #define CONFIG_DEBUG_LOG_ENABLE 1
#include "log.h"
#include "statsCnt.h"
#define ONE_SECOND 1000
static STATS_CNT_RATE_DEFINE(firstRate, ONE_SECOND);
#ifdef CONFIG_DEBUG_LOG_ENABLE
static STATS_CNT_RATE_DEFINE(secondRate, ONE_SECOND);
#endif
static float someData;
static float otherData;
void aFunction(float aValue, bool doStuff) {
someData = aValue;
STATS_CNT_RATE_EVENT(&firstRate);
if (doStuff) {
otherData = someData * 2.0f;
STATS_CNT_RATE_EVENT_DEBUG(&secondRate);
}
}
LOG_GROUP_START(example)
// These will always be available
LOG_ADD(LOG_FLOAT, someData, &someData)
STATS_CNT_RATE_LOG_ADD(firstRt, &firstRate)
// These will only be available if CONFIG_DEBUG_LOG_ENABLE is defined
LOG_ADD_DEBUG(LOG_FLOAT, otherData, &otherData)
STATS_CNT_RATE_LOG_ADD_DEBUG(secondRt, &secondRate)
LOG_GROUP_STOP(example)
```
7 changes: 7 additions & 0 deletions src/modules/interface/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ typedef struct {
LOG_ADD_GROUP(LOG_GROUP | LOG_STOP, stop_##NAME, 0x0) \
};

#ifdef CONFIG_DEBUG_LOG_ENABLE
#define LOG_ADD_DEBUG(TYPE, NAME, ADDRESS)
#else
#define LOG_ADD_DEBUG(TYPE, NAME, ADDRESS) LOG_ADD(TYPE, NAME, ADDRESS)
#endif

#else // UNIT_TEST_MODE

// Empty defines when running unit tests
Expand All @@ -197,6 +203,7 @@ typedef struct {
#define LOG_ADD_GROUP(TYPE, NAME, ADDRESS)
#define LOG_GROUP_START(NAME)
#define LOG_GROUP_STOP(NAME)
#define LOG_ADD_DEBUG(TYPE, NAME, ADDRESS)

#endif // UNIT_TEST_MODE

Expand Down
10 changes: 10 additions & 0 deletions src/utils/interface/statsCnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ typedef struct {
*/
#define STATS_CNT_RATE_LOG_ADD(NAME, LOGGER) LOG_ADD_BY_FUNCTION(LOG_FLOAT, NAME, LOGGER)

#ifdef CONFIG_DEBUG_LOG_ENABLE
#define STATS_CNT_RATE_EVENT_DEBUG(LOGGER) STATS_CNT_RATE_EVENT(LOGGER)
#define STATS_CNT_RATE_MULTI_EVENT_DEBUG(LOGGER, CNT) STATS_CNT_RATE_MULTI_EVENT(LOGGER, CNT)
#define STATS_CNT_RATE_LOG_ADD_DEBUG(NAME, LOGGER) STATS_CNT_RATE_LOG_ADD(NAME, LOGGER)
#else
#define STATS_CNT_RATE_EVENT_DEBUG(LOGGER)
#define STATS_CNT_RATE_MULTI_EVENT_DEBUG(LOGGER, CNT)
#define STATS_CNT_RATE_LOG_ADD_DEBUG(NAME, LOGGER)
#endif

/**
* @brief Initialize a statsCntRateLogger_t struct.
*
Expand Down

0 comments on commit 7d9f984

Please sign in to comment.