Solved

How do I make a wrapper function for printf ?

Posted on 2004-08-01
2
350 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

735 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