I created a debug print helper for adding debug output to projects, attached. To use:
#include "debug.h" ... LogError("Hello! %d", 1); LogWarn("World! %d", 2); LogInfo("Goodbye! %d", 3);
Thank you for sharing your solution!
My only caution is that "DEBUG" is a very generic and widely used symbol, so if the intent is only to control the log messages a more specific symbol might be used instead. (see platform\base\hal\micro\generic\compiler\platform-common.h for example).
Thanks for your suggestion @jpitt, a different name would probably be more appropriate. This was just intended to guide others though, and should be fairly simple to understand and modify for different circumstances. The process to add the global symbol and the macros themselves are hopefully helpful to others though.
Side note: I can't see a way to edit posts, so I couldn't make your suggested changes in my original posts.
Thanks for posting this. In my project DEBUG_H was already defined so the header file wasn't included. Do you know where stderr is should be defined? It's not in my project.
Sorry about that, I should have included stdio.h in the file. Improved version attached with the macro changed from DEBUG to LOG_LEVEL. I also added some options to control if it outputs colours (some terminals don't support it like the built in console in Simplicity Studio) and if it outputs the file, function and line number.
If you don't already have printf set up to print to your chosen output you'll need to set it up:
Note that if LOG_LEVEL is not defined, this will not add the print statements to your code, so will not affect performance in production.
A better version again, sorry, can't edit posts.
What is the trick for getting output into the console window? No errors (I see you already found the cut and paste error for the no location option) but nothing shows up in the window. I tried just putting some fprintf and fflush for both stderr and stdout statements in my code but nothing.
If it isn't working with your STK/board then it's because of a bug in Simplicity Studio which I have reported (it doesn't work for me). If you put a breakpoint in retargetswo.c > RETARGET_WriteChar and keep resuming you will see the characters appear in the console one by one. Your other options are to use commander in terminal (macOS):
/Applications/Simplicity\ Studio.app/Contents/Eclipse/developer/adapter_packs/commander/Commander.app/Contents/MacOS/commander swo read -s <serial>
Get the serial from Launcher in Simplicity studio "J-Link Silicon Labs (<serial>)"
You cannot debug in Simplicity Studio and run commander at the same time.
Or, configure it to use UART with retargetserial.c. For me this conflicts with the joystick input so I'm using SWO for now.
Yes I am using the STK board. I just assumed stdout (stderr is set equal to stdout in stdio.h) would be piped to the Jlink debugger but that may not be the case. I'll look into it more next week if I get time. This would be a very helpful tool.
Not by default. You have to use retargetswo for that, but as I say it doesn't work right now due to a bug. Best to use a serial terminal and retargetserial.c. Both options are described in the link in my previous post