Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Design of Print.printf

  1. #1
    Junior Member
    Join Date
    Mar 2020
    Posts
    19

    Design of Print.printf

    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/co...nt.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

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    -
    Posts
    9,756
    I don't know which vdprintf() implementation you use
    Teensys (and almost all other ARM-Cortex boards or systems) uses the newlib, by Red Hat: https://sourceware.org/newlib/libc.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •