To use stream buffer of message queues when there are multiple readers/writers and there isn't a need to have fixed length messages?

I’m debating between using the type of a data structure for my case: I have a logger task that merely prints the stuff passed into the debug console over UART.

That means this logger task could be used by multiple other tasks to print stuff for debugging purposes, and the stuff passed in may not be of the same length i.e if I want to print characters, it may not be of the fixed-length at all times.

Does that mean stream buffer is a better option here as opposed to queues for this very reason? Though I read that stream buffers are suggested for a single reader/writer and in case of multiple, you might want to use mutex.

If using a StreamBuffer with multiple writers, you just need to protect the write side with a Mutex, so only one task will be trying to write at a time. Then it works just fine. The one small limitation is while the Mutex will respect task priority, once it gives control to a task, even if the StreamBuffer is full and the task needs to wait, a higher priority task getting in line has to wait for it to finish.

This implementation posts the logging messages to a Queue which is drained/serviced by a logging task: amazon-freertos/iot_logging_task_dynamic_buffers.c at master · aws/amazon-freertos · GitHub


What I do is have an array of {len, fixed buff, inflight} and rotate through them. vsnprintf into a fixed buffer. Then I queue a short description, {len, const char*, info}. This also gives me an in-memory short history. Then the logging thread runs at the lowers priority, i.e., using idle time.
One can also do a large fixed circular buffer space and vsnprintf into the last location. But then you need to deal with the wrap before calling vsnprintf.
Interestingly if you overrun your circular list of buffers, it tells you that some thread is misbehaving.