3 ms·
Your points about logging with care saving more cycles that logging efficiently is very right. That said, here's how I've handled efficient logging in realtime
by cjensen 7y ago
Your points about logging with care saving more cycles that logging efficiently is very right.
That said, here's how I've handled efficient logging in realtime threads since the old days of 100MHz processors: postpone the formatting to a non-realtime thread. Assume your log_printf() will have no more than 10 int-sized args. Grab and save the 10 "ints". When doing the format in the non-realtime thread, pass in the 10 ints. Obviously this won't work with every CPU arch, but it does work fine for all the most common ones. Now every time you do a log_printf(), 1 pointer and 10 ints are copied to a buffer, which is very quick.
The catch is that your format string must be not be changed (e.g. use a string literal). Also any strings you want to print must also be const.