Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs

Logging with char array

Environment details:

  • Board: Custom
  • Chip: nRF52840
  • PCA Number: PCA10056
  • SoftDevice: S140, v7.0.1
  • nRF5 SDK: Version 16.0.0
  • mdk version: 8.46.0
  • nrfjprog version: 10.15.4 external
  • JLinkARM.dll version: 7.64d

Issue:

I'd expect the following log lines to all result in the same thing, but only the first one works as expected, the others result in either no output or mangled output.

Do you have any suggestions? I'd like to be able to log dynamically created character arrays.

// works as expected
NRF_LOG_INFO("some string");

// viewable in debugger, doesn't print (or prints empty string)
static char s[] = "some string";
NRF_LOG_INFO(s);

// debugger displays 'Memory read failure', mangled string printed,
// switch debugger to display array and we see the correct value.
char s[] = "some string";
NRF_LOG_INFO(s);

For the 3rd case, This is what it looks like in the debugger initially:

And if I change it to be of type `array`:

Parents
  • Okay, my teammate just found this little nugget in here:

    "Since the logs are not processed directly, it is risky to pass pointers as arguments, especially to variables that do not have static addresses, for example variables allocated on the stack. By the time the log is processed, the pointer can point to invalid data. The logger provides a mechanism to overcome this issue by enforcing a string that is copied into the internal logger buffer."

    With the suggestion to use `NRF_LOG_PUSH()`.

    Thankfully, this solves the problem. I still don't understand why if we can see the data is not corrupted the original did not work though.

Reply
  • Okay, my teammate just found this little nugget in here:

    "Since the logs are not processed directly, it is risky to pass pointers as arguments, especially to variables that do not have static addresses, for example variables allocated on the stack. By the time the log is processed, the pointer can point to invalid data. The logger provides a mechanism to overcome this issue by enforcing a string that is copied into the internal logger buffer."

    With the suggestion to use `NRF_LOG_PUSH()`.

    Thankfully, this solves the problem. I still don't understand why if we can see the data is not corrupted the original did not work though.

Children
No Data
Related