Solved

How do I make a wrapper function for printf ?

Posted on 2004-08-01
2
344 Views
Last Modified: 2012-06-27
Hi,

I wish to add a wrapper function for printf, to allow multiple debug levels for my program. For example, I could pass it "-v 5" to be very verbose, or "-v 0" to print nothing at all.

The prototype function for printf is:

int printf (const char *format, ...);

How would I make my own function, my_printf, that takes into account the verbosity setting, and calls the real printf?
0
Comment
Question by:Risky101
2 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 125 total points
ID: 11688694
To write a wrapper to printf, you will want to us va_list, va_arg, va_start... described here:
http://www.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html


However, when faced with the same problem many years ago, I chose to actually
write a wrapper macro rather than function.  This helped performance by avoiding
the function/call return overhead for messages that would be discarded at the
current verbosity level.

0
 

Author Comment

by:Risky101
ID: 11689390
One word: brilliant! Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question