In the above code, I can map the BufferedSerial FilePointer to printf just fine, but when I attempt to to instead return my FilePointer to my log file, it crashes immediatly (and since my printf is remapped, I can’t see the output).
What is the correct way to do this? Per the mbed docs, this should work.
The code snippet above represents only variable declaration + initialization and function definition. It does not define the sequence of program execution (apart of the code inside the function’s body). So when you have
it does not mean that the logFile is already opened when the mbed::mbed_override_console function is getting called in the program. In fact, the compilation of the following code results in the same binary as the one above:
Thank you for the help! The your recommendations seem to get it closer, but I am actually seeing some really strange behavior on the logfile.open call. The value of err2 actually ends up being -22 or -EINVAL due to return statement at File.cpp line 44. It appears that, only when done in the mbed_override_console definition, that _fs is actually set to a value rather than being set to Null, despite never having been assigned. logFile isn’t yet used anywhere else, so how that is being set is quite beyond me. Any ideas of why that would be the case?
I’ll add, if the code is moved out of the mbed_override block, it does not exhibit this behavior.
Maybe the reason is same as for the other stuff: the logFile has not been initialized yet and the _fs data member contains some random value. Try to move the logFile definition into the mbed_override_console function too:
bd = new CustomSPIFBlockDevice(PTD2, PTD3, PTD1, PTD0);
fs = new LittleFileSystem("fs", bd);
int err = fs->mount(bd);
static File logFile;
int err2 = logFile.open(fs,FULL_LOG_FILE_PATH,O_RDWR|O_APPEND);
That’s strange. Try to use the second File constructor:
/** Create a file on a filesystem
* Creates and opens a file on a filesystem
* @param fs Filesystem as target for the file
* @param path The name of the file to open
* @param flags The flags to open the file in, one of O_RDONLY, O_WRONLY, O_RDWR,
* bitwise or'd with one of O_CREAT, O_TRUNC, O_APPEND
File(FileSystem *fs, const char *path, int flags = O_RDONLY);
Try to define the FULL_LOG_FILE_PATH as #define FULL_LOG_FILE_PATH "/fs/logs.txt"
You can find a very nice explanation how the printf works in Mbed OS 6, including overriding the console, at Hitchhiker's Guide to Printf in Mbed 6
To redirect the printf it’s sufficient to override the console only for the stdout file descriptor: