AceCommon  1.4.5
Arduino library for low-level common functions and features with no external dependencies
Functions | Variables
printfTo.h File Reference
#include <stdio.h>
#include <stdarg.h>
Include dependency graph for printfTo.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void ace_common::vprintfTo (Print &printer, const char *fmt, va_list args)
 Helper function for implementing the printfTo() function.
 
void ace_common::printfTo (Print &printer, const char *fmt,...)
 A printf() that works on an Arduino Print object using the built-in vsnprintf(). More...
 

Variables

const int ace_common::PRINTF_TO_BUF_SIZE = 192
 Maximum size of the internal stack buffer used by printfTo().
 

Detailed Description

Implement a printfTo() that accept formatting strings like printf() and prints the result to the given Print object. This is intended to be used on platforms whose Print does not provide a Print.printf() function, for example, the original Arduino UNO or Nano. I finally got tired of writing multiple lines of SERIAL_PORT_MONITOR.print() for debugging.

The implementation uses an fixed size char[] buffer on the stack that is defined to be of length PRINTF_TO_BUF_SIZE, which is current 192. That should be enough for most debugging uses. It would be more robust if the format string was interpreted by the method to avoid that buffer. But this function is intended only for debugging purposes. For "production" deployments, this function should be disabled through an #if statement.

NOTE: These must be implemented as inline function to allow the compiler to remove unused functions from the binary. For some reason, on AVR, ESP8266 and ESP32 compilers, link-time-optimization does not seem to work well. If these functions are defined in a .cpp file, they are included in the binary, even if they are not reference at all by anything. This causes the binary to be about 700 (AVR) to 1000 (ESP32) bytes larger in flash memory.

It looks like <stdio.h> is the header to get vsnprintf(), so we no longer need to include <Arduino.h> here.

Definition in file printfTo.h.

Function Documentation

◆ printfTo()

void ace_common::printfTo ( Print &  printer,
const char *  fmt,
  ... 
)
inline

A printf() that works on an Arduino Print object using the built-in vsnprintf().

Append a '\n' at the end of the string to print a newline. This is intended to be used on platforms without a built-in Print.printf() function, for example, the original Arduino UNO or Nano.

Definition at line 59 of file printfTo.h.