473,804 Members | 3,475 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
16 6709
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,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)
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.co m 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**********@p obox.comwrites:
st****@gmail.co m 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_Keit h) 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...@p obox.comwrote:
std...@gmail.co m 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,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));
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' (nonparameteriz ed) 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.ne t
Aug 26 '07 #17

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
17630
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
4113
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
9708
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
9588
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
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10327
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
9161
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
7625
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
5527
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
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2999
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.