When I override the console, I see no way of flushing the buffer created by using printf
.
For example, if I override the console with a BufferedSerial
, printf
statements without a line break in them are not flushed. They only are printed when printf
is called with a line break.
Minimal reproducible code:
#include "mbed.h"
BufferedSerial serial(CONSOLE_TX, CONSOLE_RX, 115200);
FileHandle *mbed::mbed_override_console(int)
{
return &serial;
}
int main()
{
FileHandle *console_out = mbed_file_handle(STDOUT_FILENO);
FILE *f = fdopen(console_out, "w");
int counter = 0;
while (1)
{
console_out->write("hi", 2); // written immediately
serial.write("\tbye", 4); // written immediately
fprintf(f, "\tf"); // written immediately
printf("\t%d", ++counter); // not written until new line is printed
fprintf(f, "\tg"); // printed _before_ counter
serial.sync(); // does nothing
console_out->sync(); // does nothing
fflush(f); // does nothing
ThisThread::sleep_for(1s);
printf("\tFlushing now...\r\n");
}
}
How do I immediately print when directly using printf
(without getting a FILE *
or FileHandle
), but still use a BufferedSerial
?