Programming Tips - C++: my own custom printf()-like function that accepts a variable number of arguments

Date: 2014sep26 Date: 2025jun15 Language: C/C++ Keywords: snprintf, vsprintf, vprintf, vargs, variadic Q. C++: my own custom printf()-like function that accepts a variable number of arguments A. Use the va_* family of functions like this:
#include <stdio.h> #include <cstdarg> void Log(const char *format, ...) { va_list ap; char buf[5 * 1024]; va_start(ap, format); vsnprintf(buf, sizeof(buf), format, ap); fputs(buf, fMyLogHandle); fputchar('\n', fMyLogHandle); fflush(fMyLogHandle); va_end(ap); }