473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Redefine fprintf for debugging purposes

Hi,

I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.

I tried something like
#define fprintf (x,y,z) my_fprintf(x,y,z)

and the compiler gave about 100 errors. Can anyone help me with this.
I have even tried using variadic macros but it seems too complicated.
Can anyone just help with such a defination.

Thanks and Regards,
Prayag Narula

Jul 23 '07 #1
16 6650
Prayag Narula wrote:
Hi,

I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.
<snip>

You cannot portably redefine Standard library functions and symbols. fprintf
takes a FILE * argument. Why do you feel that must be stdout. Just pass it
a FILE * set to a file, presumably opened by fopen.

FILE *log;
if((log = fopen("filename", "a")) == NULL) { deal_with_error(); }
/* ... */
if(fprintf(log, "format string", ...) < 0) { deal_with_error(); }
Jul 23 '07 #2
On Jul 23, 1:23 pm, Prayag Narula <prayag.nar...@gmail.comwrote:
#define fprintf (x,y,z) my_fprintf(x,y,z)
If there is space between fprintf & (x,y,z) that could be the reason
for those errors. You need to have it without space "fprintf(x,y,z)".
Even better would be "#define fprintf my_fprintf" if x y z remain same
which I think is highly unlikely as u would be changing replacing
stdout with your file pointer.

I feel using variadic macros fit better in your scenario as fprintf
has variable arguments. I would to thank you to introduce me this
concept as was looking for something like this for a long time.. :)

Jul 23 '07 #3

"Prayag Narula" <pr***********@gmail.comwrote in message
news:11**********************@i38g2000prf.googlegr oups.com...
Hi,

I want to redefine fprintf for debugging purposes. That is I want that
all the output that is going to the stdout should be logged in a
file.

I tried something like
#define fprintf (x,y,z) my_fprintf(x,y,z)

and the compiler gave about 100 errors. Can anyone help me with this.
I have even tried using variadic macros but it seems too complicated.
Can anyone just help with such a defination.
If all of your printing to stdout is done with:
fprintf( stdout, ...);
(that is, using fprintf rather than printf)
then it is really easy:

#ifdef DEBUG
#define OUTFILE myfile
#else
#define OUTFILE stdout
#endif

Then all print statements become:
fprintf( OUTFILE, ... );

Make sure you successfully open myfile before
any of the fprintf statements are executed.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing

Jul 23 '07 #4
>You cannot portably redefine Standard library functions and symbols. fprintf
>takes a FILE * argument. Why do you feel that must be stdout. Just pass it
a FILE * set to a file, presumably opened by fopen.
I do not want to redefine fprintf portably. Just for debugging
purposes. Thats why I need to use macros.Actually I am working on an
embedded device and cannot see the stdout. I want to define a macro
that redirects output from stdout to a file.
>fprintf
takes a FILE * argument. Why do you feel that must be stdout. Just pass it
a FILE * set to a file, presumably opened by fopen.

FILE *log;
if((log = fopen("filename", "a")) == NULL) { deal_with_error(); }
/* ... */
if(fprintf(log, "format string", ...) < 0) { deal_with_error(); }
I cannot keep changing the function call at every call. Thats why a
macro.
>If there is space between fprintf & (x,y,z) that could be the reason
for those errors. You need to have it without space "fprintf(x,y,z)".
Even better would be "#define fprintf my_fprintf" if x y z remain same
which I think is highly unlikely as u would be changing replacing
stdout with your file pointer
There is no space between fprintf and (x,y,z). My fault. I did not
copy paste the codes. :(
>I feel using variadic macros fit better in your scenario as fprintf
has variable arguments.
I have been fiddling around with variadic macros too. But the thing
is, the error messages I get are more than just too many or too few
parameters. For example:

undefined identifier 'NULL' in config.c
(this is being returned by fprintf, so something is going on with the
function that I wote)

I am not sure if this would be solved using variadic macros. But I
would try again with a full fledged rewritten fprintf.
>fprintf( stdout, ...);
(that is, using fprintf rather than printf)
then it is really easy:

#ifdef DEBUG
#define OUTFILE myfile
#else
#define OUTFILE stdout
#endif

Then all print statements become:
fprintf( OUTFILE, ... );
I do not want to change the code thats already written. But this
option would be my last resort if nothing else works out.

Looking forward to more comments.

Prayag Narula

Jul 24 '07 #5
On 24 Jul, 03:48, Prayag Narula <prayag.nar...@gmail.comwrote:

please leave attributions in
You cannot portably redefine Standard library functions and symbols. fprintf
takes a FILE * argument. Why do you feel that must be stdout. Just pass it
a FILE * set to a file, presumably opened by fopen.

I do not want to redefine fprintf portably.
around here "portably" means "as defined by the C standard". This news
group
discusses standard C. So if you don't want to do it portably you're in
the wrong ng!
Just for debugging
purposes. Thats why I need to use macros.Actually I am working on an
embedded device and cannot see the stdout. I want to define a macro
that redirects output from stdout to a file.
fprintf
takes a FILE * argument. Why do you feel that must be stdout. Just pass it
a FILE * set to a file, presumably opened by fopen.
FILE *log;
if((log = fopen("filename", "a")) == NULL) { deal_with_error(); }
/* ... */
if(fprintf(log, "format string", ...) < 0) { deal_with_error(); }

I cannot keep changing the function call at every call. Thats why a
macro
why not. With a decent editor that's dead easy. So globally replace
all

fprintf (stdout, ...

with

fprintf (LOG_STREAM, ...

then define LOG_STREAM depending where you are. What are you doing in
non-debug mode? It seems odd to me that your embedded device has files
but not standard out. Oh wait! is your embedded device a Windows
machine?
:-)
<snip>

--
Nick Keighley

Jul 24 '07 #6
On Jul 24, 5:58 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
>
around here "portably" means "as defined by the C standard". This news
group
discusses standard C. So if you don't want to do it portably you're in
the wrong ng!
Ok ! As they say, "ma bad!" :)
>
why not. With a decent editor that's dead easy. So globally replace
all

fprintf (stdout, ...

with

fprintf (LOG_STREAM, ...

I still do not thing that its such a good idea. I have a feeling it is
bound to create more problems than it would solve.
then define LOG_STREAM depending where you are. What are you doing in
non-debug mode? It seems odd to me that your embedded device has files
but not standard out.
In non-debug mode, fprintf is being called with upto 5 parameters. It
does everything from creating html and js files to printing out error
messages. I am interested in these error messages that go to stdout.
>Oh wait! is your embedded device a Windows
machine?
:-)
:-D Its a Nokia s60 emulator. It has a pseudo terminal but it seems to
crash whenever there is a lot of output to stdout. Thats why I am
trying to log it.

Regards,
Prayag Narula

Jul 25 '07 #7
So basically, this is what I did.

<debug.h>
#ifndef __DEBUG_H__
#define __DEBUG_H__

#define fprintf(fp,...) my_fprintf(fp, __VA_ARGS__)

#endif

</debug.h>

<debug.c>
#include <stdio.h>
#include<string.h>
#include <stdarg.h>
#include "debug.h"

int my_fprintf(FILE *fo, ...)
{
va_list ap;
va_start(ap,fo);
FILE *fp;
char *buffer;
buffer = va_arg(ap,char*);
fp = fopen("log","a");

//printf("%s %d",buffer,strlen(buffer));

if(fo == stdout || fo == stderr)
{
fp = fopen("log","a");
if (fp == NULL)
return(-1);
if(fwrite(buffer,sizeof(char),strlen(buffer),fp)0)
return(strlen(buffer));
}
else
{
return (-1);
}
}
</debug.c>

Works in gcc but doesn't work in the embedded IDE. I guess, the
embedded compiler does not support variable arguments macro. Though it
definitely supports variable argument functions.

Though I am still testing.

Anyways, another option that I can think of is

#define fprintf(x,y) my_fprintf_1(x,y)
#define fprintf(x,y,z) my_fprintf_2(x,y,z)
....
and so on. A dirty way of doing this. What do you guys think ?

On Jul 24, 10:59 pm, Prayag Narula <prayag.nar...@gmail.comwrote:
On Jul 24, 5:58 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
around here "portably" means "as defined by the C standard". This news
group
discusses standard C. So if you don't want to do it portably you're in
the wrong ng!

Ok ! As they say, "ma bad!" :)
why not. With a decent editor that's dead easy. So globally replace
all
fprintf (stdout, ...
with
fprintf (LOG_STREAM, ...

I still do not thing that its such a good idea. I have a feeling it is
bound to create more problems than it would solve.
then define LOG_STREAM depending where you are. What are you doing in
non-debug mode? It seems odd to me that your embedded device has files
but not standard out.

In non-debug mode, fprintf is being called with upto 5 parameters. It
does everything from creating html and js files to printing out error
messages. I am interested in these error messages that go to stdout.
Oh wait! is your embedded device a Windows
machine?
:-)

:-D Its a Nokia s60 emulator. It has a pseudo terminal but it seems to
crash whenever there is a lot of output to stdout. Thats why I am
trying to log it.

Regards,
Prayag Narula

Jul 25 '07 #8
So basically, this is what I did.

<debug.h>
#ifndef __DEBUG_H__
#define __DEBUG_H__

#define fprintf(fp,...) my_fprintf(fp, __VA_ARGS__)

#endif

</debug.h>

<debug.c>
#include <stdio.h>
#include<string.h>
#include <stdarg.h>
#include "debug.h"

int my_fprintf(FILE *fo, ...)
{
va_list ap;
va_start(ap,fo);
FILE *fp;
char *buffer;
buffer = va_arg(ap,char*);
fp = fopen("log","a");

//printf("%s %d",buffer,strlen(buffer));

if(fo == stdout || fo == stderr)
{
fp = fopen("log","a");
if (fp == NULL)
return(-1);
if(fwrite(buffer,sizeof(char),strlen(buffer),fp)0)
return(strlen(buffer));
}
else
{
return (-1);
}
}
</debug.c>

Works in gcc but doesn't work in the embedded IDE. I guess, the
embedded compiler does not support variable arguments macro. Though it
definitely supports variable argument functions.

Though I am still testing.

Anyways, another option that I can think of is

#define fprintf(x,y) my_fprintf_1(x,y)
#define fprintf(x,y,z) my_fprintf_2(x,y,z)
....
and so on. A dirty way of doing this. What do you guys think ?

On Jul 24, 10:59 pm, Prayag Narula <prayag.nar...@gmail.comwrote:
On Jul 24, 5:58 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
around here "portably" means "as defined by the C standard". This news
group
discusses standard C. So if you don't want to do it portably you're in
the wrong ng!

Ok ! As they say, "ma bad!" :)
why not. With a decent editor that's dead easy. So globally replace
all
fprintf (stdout, ...
with
fprintf (LOG_STREAM, ...

I still do not thing that its such a good idea. I have a feeling it is
bound to create more problems than it would solve.
then define LOG_STREAM depending where you are. What are you doing in
non-debug mode? It seems odd to me that your embedded device has files
but not standard out.

In non-debug mode, fprintf is being called with upto 5 parameters. It
does everything from creating html and js files to printing out error
messages. I am interested in these error messages that go to stdout.
Oh wait! is your embedded device a Windows
machine?
:-)

:-D Its a Nokia s60 emulator. It has a pseudo terminal but it seems to
crash whenever there is a lot of output to stdout. Thats why I am
trying to log it.

Regards,
Prayag Narula

Jul 25 '07 #9
Prayag Narula wrote:
On Jul 24, 5:58 pm, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:
>>... With a decent editor that's dead easy. So globally replace
all

fprintf (stdout, ...

with

fprintf (LOG_STREAM, ...
[Replace something Prayag snipped
>then define LOG_STREAM depending where you are

I still do not thing that its such a good idea. I have a feeling it is
bound to create more problems than it would solve.
It's a fairly obvious solution to your problem. What problems do you
foresee?
Jul 25 '07 #10
On Jul 23, 10:23 am, Prayag Narula <prayag.nar...@gmail.comwrote:
Hi,
all the output that is going to the stdout should be logged in a
file.
/* .... */
fclose(stdout);
stdout = fopen("....", "w+");
/* ... error checking ... */

What about something like this ?

Jul 25 '07 #11
Prayag Narula <pr***********@gmail.comwrites:
So basically, this is what I did.

<debug.h>
#ifndef __DEBUG_H__
#define __DEBUG_H__
You are liable to run into name problems. Implementations are allowed
to define __DEBUG_H__ at their leisure. I think the usual "best
practice" is to use H_DEBUG.
>
#define fprintf(fp,...) my_fprintf(fp, __VA_ARGS__)

#endif

</debug.h>

<debug.c>
#include <stdio.h>
#include<string.h>
#include <stdarg.h>
#include "debug.h"

int my_fprintf(FILE *fo, ...)
{
va_list ap;
va_start(ap,fo);
FILE *fp;
char *buffer;
buffer = va_arg(ap,char*);
fp = fopen("log","a");

//printf("%s %d",buffer,strlen(buffer));

if(fo == stdout || fo == stderr)
{
fp = fopen("log","a");
if (fp == NULL)
return(-1);
if(fwrite(buffer,sizeof(char),strlen(buffer),fp)0)
I don't see the value of using variable argument lists if you just
print the format string (i.e. one argument).

IMO your best bet is to:

(1) re-write all the debugging call to make them call a new function
of your own with no FILE *. It will be a pain the first time, but you
will have freed yourself from having rather inflexible fprintfs all
over the place.

(2) write this debug function to use vfprintf either to stdout,
stderr, a log file or all (or none) of them under the control of some
other variables. (These can be global, or you can pass a pointer to a
debug state structure if you are worried about that.)

You will avoid the need for variable argument macros this way, too.

--
Ben.
Jul 25 '07 #12
st****@gmail.com wrote:
On Jul 23, 10:23 am, Prayag Narula <prayag.nar...@gmail.comwrote:
>>Hi,

>>all the output that is going to the stdout should be logged in a
file.

/* .... */
fclose(stdout);
stdout = fopen("....", "w+");
/* ... error checking ... */

What about something like this ?
Nope - stdout may not be an lvalue...

freopen() may be appropriate, I guess.
Jul 25 '07 #13
Mark Bluemel <ma**********@pobox.comwrites:
st****@gmail.com wrote:
>On Jul 23, 10:23 am, Prayag Narula <prayag.nar...@gmail.comwrote:
>>>Hi,
>>>all the output that is going to the stdout should be logged in a
file.
/* .... */
fclose(stdout);
stdout = fopen("....", "w+");
/* ... error checking ... */
What about something like this ?

Nope - stdout may not be an lvalue...
Meaning that stdout isn't necessarily an lvalue. (A reasonable, but
incorrect, parsing of your statement is that stdout is not allowed to
be an lvalue.)

And even if stdout happens to be an lvalue, assigning a value to it
may not necessarily work.
freopen() may be appropriate, I guess.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 26 '07 #14
On Jul 25, 9:53 pm, Mark Bluemel <mark_blue...@pobox.comwrote:
std...@gmail.com wrote:
On Jul 23, 10:23 am, Prayag Narula <prayag.nar...@gmail.comwrote:
>Hi,
>all the output that is going to the stdout should be logged in a
file.
/* .... */
fclose(stdout);
stdout = fopen("....", "w+");
/* ... error checking ... */
What about something like this ?

Nope - stdout may not be an lvalue...

freopen() may be appropriate, I guess.
Works!! Thanks a lot!!

Jul 31 '07 #15
>
#define fprintf(x,y) my_fprintf_1(x,y)
#define fprintf(x,y,z) my_fprintf_2(x,y,z)
...
I dont think this works... I tried in gcc and it gave redefinition
error..

Aug 5 '07 #16
On Wed, 25 Jul 2007 01:44:14 -0700, Prayag Narula
<pr***********@gmail.comwrote:
#define fprintf(fp,...) my_fprintf(fp, __VA_ARGS__)
int my_fprintf(FILE *fo, ...)
{
va_list ap;
va_start(ap,fo);
Minor: va_start() counts as code, and having code before declarations
within a block is not standard in C90, although it is supported as an
extension in several popular compilers and is standard in C99.
FILE *fp;
char *buffer;
buffer = va_arg(ap,char*);
fp = fopen("log","a");

//printf("%s %d",buffer,strlen(buffer));

if(fo == stdout || fo == stderr)
{
fp = fopen("log","a");
if (fp == NULL)
return(-1);
if(fwrite(buffer,sizeof(char),strlen(buffer),fp)0)
return(strlen(buffer));
This will only print the format string, not any converted data that it
calls for, which means the only valid invocations are with exactly two
arguments (fp and str) and you don't need variadic. If you really want
fprintf functionality, use vfprintf, or sprintf or better snprintf
(also a common extension in C90, standard in C99) plus fwrite.
}
else
{
return (-1);
From your description elsethread I thought you do want to
allow/support output to files other than stdout/err, you just don't
want the redirection to apply to those other files.
Works in gcc but doesn't work in the embedded IDE. I guess, the
embedded compiler does not support variable arguments macro. Though it
definitely supports variable argument functions.
vararg functions have been standard since C90, which is almost
universally implemented (modulo bugs), and were fairly common even
before standardization. variadic macros are new in C99, not yet widely
implemented, and were/are not common outside of C99 and gcc.
Though I am still testing.

Anyways, another option that I can think of is

#define fprintf(x,y) my_fprintf_1(x,y)
#define fprintf(x,y,z) my_fprintf_2(x,y,z)
...
and so on. A dirty way of doing this. What do you guys think ?
That can't work at all; Cpp allows only one definition for a macro.

What does work is
#define fprintf0(f,s), my_fprintf_0(f,s)
#define fprintf1(f,s,a) my_fprintf_1(f,s,a)
#define fprintf2(f,s,a,b) my_fprintf_2(f,s,a,b)
(where I count the number of variable/data args only) but this
requires changing each invocation, which you wanted to avoid.

Using an 'object-like' (nonparameterized) macro instead can work:

#undef fprintf
#define fprintf my_fprintf

void my_fprintf (FILE * f, /*restrict*/ const char * s, ...)
/* note signature EXACTLY SAME as real fprintf() */
{ blah blah blah }

and then usage as fprintf (fp, "foo", x, y, z) just expands to
my_fprintf (fp, "foo", x, y, z) and your function can do its thing.

Caveat: this is officially not supported by the standard -- overriding
any standard-library feature is not -- but it is likely to work on all
real implementations, at least as long as you do it only for source
i.e. NOT before #include'ing system or even third-party headers. And
of course it only applies to calls within code you write or modify and
compile, not within any object libraries that you call.

- formerly david.thompson1 || achar(64) || worldnet.att.net
Aug 26 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: BillD | last post by:
I'm trying to derive a schema from a base schema. I want to redefine a "group" from the base schema in my derived schema in order to add more options to the "choice" aggregate (see schema1.xsd...
1
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm getting a validation error when I try to restrict the content of nested groups with xs:redefine whereas the same restriction on xs:element's...
2
by: Arti Potnis | last post by:
Hi, I have an application with a function "myfunction" that opens a file and writes to it using fprintf. This application runs on a unix (sun solaris 5.8) system. I connect to this application...
3
by: junlia | last post by:
We are using ACORD xml schema standard, and we need to add to it, so we choose to redefine ACORD xml schema. One of the problems that I ran into is how to add some values to an emumerated list. ...
2
by: noleander | last post by:
Sorry if this is a trivial question :-) I've got several print statements in my code: fprintf ( stderr, ....); but when I run my program, I cannot find the output text in any window. My...
6
by: gerry | last post by:
I am almost at the end of my rope - I have to go through this process everytime a new version of studio is released or it is installed on a new machine - what a (^%&$^& pain. I am trying to...
25
by: paytam | last post by:
hi all I want to redefine a function getchar() in header stdio.h ,but I don't know what should I do.
3
by: vamsi | last post by:
Hi, I have a program, where involves creation of a thread with stack size of 16k(minimum stack size). There is an fprintf statement in the created thread code. I see that there is a core dump...
7
by: GaryDean | last post by:
(this was also posted on the MSDN WCF forum but the answers over there are not so good) I have a WCF Library hosted by IIS 6 and it all works fine. However I need to step through the code in the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.