Re: Using printf in C++
Robert Wessel <robertwessel2@yahoo.com> writes:
On Wed, 23 May 2012 16:40:34 +0200, jacob navia <jacob@spamsink.net>
wrote:
In gcc you can declare a function "printf-like" if I remember
correctly.
Interesting. Any references? It wouldn't help much for us, since it
would obviously be a GCC extension, but I'd be curious to see how that
worked. Format strings are not always completely defined in one
place, or in what nominally C code, or defined with their parameters
in the same form as passed to *printf.
See the definitions for the log() and trace() methods below.
class c_logger {
bool l_debug;
public:
c_logger(bool d) { l_debug = d; }
virtual ~c_logger(void) {};
void log(const char *, ...)
__attribute__((format(printf, 2, 3)));
size_t trace(const char *, ...)
__attribute__((format(printf, 2, 3)));
virtual void log(const char *, va_list) = 0;
virtual size_t trace(const char *, va_list) = 0;
void set_tracing(bool d=false) { l_debug = d; }
bool is_tracing(void) { return l_debug; }
};
$ info gcc -> C Extensions -> Function Attributes -> format
Note that you need to count the implicit 'this' argument in the format() attribute parameters