Yet another printf() post ;-)
I've been looking into Teensyduino's Print.printf because I'm implementing a Tee Print class with multiple outputs and I wanted to make sure that the printf formatting would happen only once. That is solved, but I have a few questions:
(1) Print.cpp uses vdprintf to a file descriptor and declares a _write() function that casts file descriptors to a Print*: https://github.com/PaulStoffregen/c...54e4c11f869e685b7d4/teensy4/Print.cpp#L91-L98
That's smart but also looks scary to me. I'm not up-to-date on my C linkage and file descriptors -- does it mean any file descriptor, anywhere in the program, will be cast to a Print* when used for writing, or is it limited to the scope of the Print.cpp file?
Why not use vsnprintf() with a buffer? I don't know which vdprintf() implementation you use, but all the ones I could find seem to use a 512 bytes buffer internally anyways.
(2) It would be really nice to also declare vprintf() so that one can easily forward calls eg. to MyLogger.info(const char*, ...) to Serial.vprintf(const char*, va_list);
(3) I think there's a bug where va_end() isn't called and I've reported it here: https://github.com/PaulStoffregen/cores/issues/591
I've been looking into Teensyduino's Print.printf because I'm implementing a Tee Print class with multiple outputs and I wanted to make sure that the printf formatting would happen only once. That is solved, but I have a few questions:
(1) Print.cpp uses vdprintf to a file descriptor and declares a _write() function that casts file descriptors to a Print*: https://github.com/PaulStoffregen/c...54e4c11f869e685b7d4/teensy4/Print.cpp#L91-L98
Code:
extern "C" {
__attribute__((weak))
int _write(int file, char *ptr, int len)
{
((class Print *)file)->write((uint8_t *)ptr, len);
return len;
}
}
That's smart but also looks scary to me. I'm not up-to-date on my C linkage and file descriptors -- does it mean any file descriptor, anywhere in the program, will be cast to a Print* when used for writing, or is it limited to the scope of the Print.cpp file?
Why not use vsnprintf() with a buffer? I don't know which vdprintf() implementation you use, but all the ones I could find seem to use a 512 bytes buffer internally anyways.
(2) It would be really nice to also declare vprintf() so that one can easily forward calls eg. to MyLogger.info(const char*, ...) to Serial.vprintf(const char*, va_list);
(3) I think there's a bug where va_end() isn't called and I've reported it here: https://github.com/PaulStoffregen/cores/issues/591