473,795 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6708
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.goo glegroups.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,strl en(buffer));

if(fo == stdout || fo == stderr)
{
fp = fopen("log","a" );
if (fp == NULL)
return(-1);
if(fwrite(buffe r,sizeof(char), strlen(buffer), fp)0)
return(strlen(b uffer));
}
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,strl en(buffer));

if(fo == stdout || fo == stderr)
{
fp = fopen("log","a" );
if (fp == NULL)
return(-1);
if(fwrite(buffe r,sizeof(char), strlen(buffer), fp)0)
return(strlen(b uffer));
}
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

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

Similar topics

2
3358
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 sample). schema1.xsd sample: <!-- Here is a clipped down version of the group I want to redefine. --> <xsd:group name="INSURANCESVCRQMSGS"> <xsd:choice> <xsd:element ref="HomePolicyAddRq" minOccurs="1" maxOccurs="1"/>
1
2236
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 validates. ============== BASE XMLSCHEMA ================= <?xml version="1.0"?> <xs:schema targetNamespace="test" xmlns="test" xmlns:xs="http://www.w3.org/2001/XMLSchema">
2
17627
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 from a VB GUi on a windows 2000 system. The GUI invokes "myfunction". I get the following error:-
3
3613
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. For an emumerated list like this: <xsd:complexType name="AttachmentType"> <xsd:simpleContent> <xsd:restriction base="OpenEnum"> <xsd:enumeration value="040"/> <xsd:enumeration value="041"/>
2
3651
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 program is in Visual C++. The program is windows-based (not a command-line program. The program is not managed. I'm running within the
6
1822
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 debug a web app from within vs.net 2005 locally on windows 2003 server I just cannot get debugging to work anywhere other than localhost I completely uninstalled the ie enhanced security I've been through all of the doc's i can google
25
22179
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
3802
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 occuring at fprintf. code snippet : ********************************************************************************* #include<pthread.h>
7
4111
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 service. But when I hit F5 in VS2008 I get the following error message. . . A binding instance has already been associated to listen URI 'http://localhost:8877/VtaxDataService'. If two endpoints want to share the same ListenUri, they must also...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.