Shell Logging

Shell commands can return information to the user. Keyword : commands.

What if you want to send messages to the terminal unprompted ?

It’s traditional to add a logging facility to any microcontroller project that has an available UART. That sort of channel is very useful for sending out error messages and progress reports.

Naturally, STM Shell isn’t stopping you from logging to a UART. But you’ll likely want to log to the same UART it uses… and that’s going to mess things up.

I’ve solved this by integrating a logging facility directly in the shell.

Simply use the LOG() macro to send a string to the terminal.

There are a few rules to follow, however :

First and foremost, LOG is meant to be used in your application code. It must not be used inside a command function, because it would break the operation of the shell’s state machine. Note that there’s no loop-hole here : if a command function calls an application function and that function calls LOG, you’re still breaking the law.

Under the hood, LOG calls the shell_out function. This function sets the shell’s busy flag, which will only get released once your string has been fully transmitted to the terminal. In other words, if you call LOG too much, too fast, you’ll get garbage on your terminal. But you can always check shell_state.busy to find out if it’s time to send another message to the terminal.

Keep in mind that robustness wasn’t a major consideration in designing STM Shell : it’s a validation tool, its interests are always secondary to letting the application code run as best as possible. Use responsibly.