turn off the log file

P

poison.summer

Hello

I am writing a big project, where I generate a log file.
However, I wonder whether there is some easy way that I could turn off
the log file.
Do I need to specify in every fprintf line like this
if(DEBUG)
fprintf(...);
end

Thanks a lot!
 
B

Ben Pfaff

I am writing a big project, where I generate a log file.
However, I wonder whether there is some easy way that I could turn off
the log file.
Do I need to specify in every fprintf line like this
if(DEBUG)
fprintf(...);
end

Write a function that does that for you, then call it in place of
fprintf(). If you need help wrapping fprintf(), refer to the C
FAQ.

15.5: How can I write a function that takes a format string and a
variable number of arguments, like printf(), and passes them to
printf() to do most of the work?

A: Use vprintf(), vfprintf(), or vsprintf().

Here is an error() function which prints an error message,
preceded by the string "error: " and terminated with a newline:

#include <stdio.h>
#include <stdarg.h>

void error(char *fmt, ...)
{
va_list argp;
fprintf(stderr, "error: ");
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
fprintf(stderr, "\n");
}

See also question 15.7.

References: K&R2 Sec. 8.3 p. 174, Sec. B1.2 p. 245; ISO
Secs. 7.9.6.7,7.9.6.8,7.9.6.9; H&S Sec. 15.12 pp. 379-80; PCS
Sec. 11 pp. 186-7.
 
R

Randy Howard

(e-mail address removed) wrote
(in article
Hello

I am writing a big project, where I generate a log file.
However, I wonder whether there is some easy way that I could turn off
the log file.
Do I need to specify in every fprintf line like this
if(DEBUG)
fprintf(...);
end

Thanks a lot!

That's a good place to use a wrapper function. Simply take all
the conditional code for your enable/disable option (or better
yet, you might find long term that having levels of logging,
i.e. verbosity might be useful), then every place that you have
the above code, simply replace it with a call to the wrapper
function, then the behavior can be modified in only one place by
changing the setting(s) for log output.

there are also quite a few existing libraries to handle error
logging in creative ways if you don't want to reinvent the
wheel. Sourceforge is a good place to look.
 
K

Kenneth Brody

Hello

I am writing a big project, where I generate a log file.
However, I wonder whether there is some easy way that I could turn off
the log file.
Do I need to specify in every fprintf line like this
if(DEBUG)
fprintf(...);
end

If this is to be done at compile-time, you can use a macro:

#if DEBUG
#define dbgprintf(args) fprintf args
#else
#define dbgprintf(args)
#endif

And then call it with:

dbgprintf((logfile,format,arg1,arg2,and_so_on));

If it's to be determined at runtime, check out vfprintf() as others have
pointed out.

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <mailto:[email protected]>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top