473,804 Members | 4,223 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Redirecting perror

Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?
--
Guillaume Dargaud
http://www.gdargaud.net/
Aug 7 '07 #1
10 4073
Guillaume Dargaud wrote:
Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?
reopen stderr to point to a file
Aug 7 '07 #2
"Guillaume Dargaud" <us************ *************** **@www.gdargaud .net>
writes:
Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?
strerror is standard:

#include <string.h>
char *strerror(int errnum);

The strerror function maps the number in errnum to a message
string. Typically, the values for errnum come from errno, but
strerror shall map any value of type int to a message.

Returns: The strerror function returns a pointer to the string,
the contents of which are locale- specific. The array pointed to
shall not be modified by the program, but may be overwritten by a
subsequent call to the strerror function.

--
Ben.
Aug 7 '07 #3
On Aug 7, 7:32 pm, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
"Guillaume Dargaud" <use_the_form_o n_my_contact_p. ..@www.gdargaud .net>
writes:
Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?

strerror is standard:

#include <string.h>
char *strerror(int errnum);

The strerror function maps the number in errnum to a message
string. Typically, the values for errnum come from errno, but
strerror shall map any value of type int to a message.

Returns: The strerror function returns a pointer to the string,
the contents of which are locale- specific. The array pointed to
shall not be modified by the program, but may be overwritten by a
subsequent call to the strerror function.

--
Ben.


just close(stderr)
and try to open a file immediately .......

Aug 7 '07 #4
alex wrote:
just close(stderr)
and try to open a file immediately .......
We all know that close() is not a standard C function, so
we cannot tell whether this advice is correct for the
OP's environment.

<OT>
Just out of curiosity: which extension are *you* using,
where close() accepts an argument of type FILE*?
</OT>

Ralf
Aug 7 '07 #5
alex wrote:
>>"Guillaume Dargaud" <use_the_form_o n_my_contact_p. ..@www.gdargaud .net>
writes:

>>>Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?
....
>
just close(stderr)
and try to open a file immediately .......
By no means guaranteed.

Ben's solution is the right one but doesn't mean Guillaume's requirement
not to replace all the perror calls.

Jacob got it right, IMHO - use freopen. Indeed the spec describes this
as the primary use for freopen. Naturally, all output written to stderr
will now go to this file.

The alternative, if only perror() is to be affected, is some fun with a
function and a macro, something like this (untested) :-

#include <errno.h>
#include <stdio.h>
static FILE *errorstream; /* opened somewhere */
void my_perror(char *string) {
char *buffer;
char *errtxt = strerror(errno) ;
int buflen = strlen(errtxt) + strlen(string) + 4;
buffer = malloc(buflen);
if (buffer == NULL) {
/* Your choice! */
}
sprintf(buffer, "%s: %s\n",string,er rtxt);
fputs(buffer,er rorstream);
free(buffer);
}

#define perror(x) my_perror(x)
....
Aug 7 '07 #6
Mark Bluemel <ma**********@p obox.comwrites:
alex wrote:
>>>"Guillaume Dargaud" <use_the_form_o n_my_contact_p. ..@www.gdargaud .net>
writes:
Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?
...
>>
just close(stderr)
and try to open a file immediately .......

By no means guaranteed.

Ben's solution is the right one but doesn't mean Guillaume's
requirement not to replace all the perror calls.
Perfectly true. I just (deliberately) ignored that part! It is rare
that one should pass up an opportunity to make ones code more
flexible, so while the reopen path is fast, it sends everything to the
file (which may, one day, be the wrong thing to do) and it passes up a
chance to make the error reporting more flexible (e.g. one might need
it to more configurable one day).

Of course, if the perror calls are not available (because the OP does
not have the source), then Jacob's solution is pretty much the ony way
to go.
Jacob got it right, IMHO - use freopen. Indeed the spec describes this
as the primary use for freopen. Naturally, all output written to
stderr will now go to this file.
If sending all stderr out to a file is OK, that is indeed the way to go.

--
Ben.
Aug 7 '07 #7
On Tue, 07 Aug 2007 17:06:27 +0100, Mark Bluemel wrote:
alex wrote:
>>>"Guillaume Dargaud" <use_the_form_o n_my_contact_p. ..@www.gdargaud .net>
writes:
Hello all,
I have some error checking using the function 'perror', which writes
messages on stderr. I'd like to send all error messages to a file instead.
Is there some way to do this, short of replacing all the perror calls with a
custom function ?
...
>>
just close(stderr)
and try to open a file immediately .......

By no means guaranteed.

Ben's solution is the right one but doesn't mean Guillaume's requirement
not to replace all the perror calls.

Jacob got it right, IMHO - use freopen. Indeed the spec describes this
as the primary use for freopen. Naturally, all output written to stderr
will now go to this file.

The alternative, if only perror() is to be affected, is some fun with a
function and a macro, something like this (untested) :-

#include <errno.h>
#include <stdio.h>
static FILE *errorstream; /* opened somewhere */
void my_perror(char *string) {
char *buffer;
char *errtxt = strerror(errno) ;
int buflen = strlen(errtxt) + strlen(string) + 4;
buffer = malloc(buflen);
if (buffer == NULL) {
/* Your choice! */
}
sprintf(buffer, "%s: %s\n",string,er rtxt);
Why not doing a fprintf on errorstream, so eliminating the need
for a malloc()ated buffer?
fputs(buffer,er rorstream);
free(buffer);
}

#define perror(x) my_perror(x)
#define perror(x) ( fprintf(errorst ream, "%s: %s\n", (x), \
strerror(errno) ), fflush(errorstr eam) )
This doesn't handle the case in which x == NULL || *x == '\0', in
which the "real" perror() simply writes strerror(errno) and a
newline. But neither does your function...

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 7 '07 #8
just close(stderr)
and try to open a file immediately .......
I didn't know you could do that... Seems like a good solution for me. Thanks
for all who contributed, I'd thought of the macro solution but it's not so
elegant as I also have custom error messages such as fprintf(stderr, ...)
which I want redirected along perror.
--
Guillaume Dargaud
http://www.gdargaud.net/Antarctica/
Aug 8 '07 #9
"Guillaume Dargaud" <us************ *************** **@www.gdargaud .net>
writes:
>just close(stderr)
and try to open a file immediately .......

I didn't know you could do that...
You can't! If re-directing all error output to the file is what you
want, ignore my (previous) answer and follow Jacob Navia's.
Seems like a good solution for me.
close is not a standard C function, and relying on what happens
"immediatel y" after a close is highly suspect. freopen is the way to go.

--
Ben.
Aug 8 '07 #10

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

Similar topics

3
2336
by: lozd | last post by:
Would appreciate any solutions people could offer for this. Basically I wan't to use a frameset with an aspx page as the contents rather than a htm page and I'd like to be able to redirect the main page from the code behind the contents page. I want to do this to allow the use of asp "linkbuttons" instead of hyperlinks so I can do a little processing before redirecting. The main reason for this to prevent hyperlinks of pages that have...
5
12658
by: L. Westmeier | last post by:
Reading the man pages and some code did not really help me in understanding the difference between - or better when I should use - perror("...") and fprintf(stderr, "...") Any hint or help is appreciated. L. Westmeier
7
1645
by: Martin | last post by:
My program cored and the following is the call stack in dbx. I would like to know what are the possibilities that when perror is being called, it will core? dbx: warning: cannot get thread count -- generic libthread_db.so error dbx: warning: thread related commands will not be available dbx: warning: see `help lwp', `help lwps' and `help where' (l@22) dbx: warning: cannot get thread count -- generic libthread_db.so error terminated by...
1
1945
by: Clunixchit | last post by:
Im writing a program in which i have to perform several mallocs in order to simply my code i have used void e_malloc(){ perror("malloc"); _exit(EXIT_FAILURE); } if ( !(phrase.T = malloc ( sizeof *(phrase.T) )))
4
1498
by: Greg Smalter | last post by:
Redirecting from page to page within a web project is pretty easy. However, all Redirect methods take strings as arguments, as if you mistype the string, you don't find out until run time that you are redirecting to somewhere that doesn't exist. Worse, if you do type it correctly, but then later the page name changes or the page moves, you still won't find out until run time. I have a framework that solves this problem and guarantees,...
10
2808
by: nuke1872 | last post by:
Hello guys, I have a file names network.txt which contains a matrix. I want to read this matrix as store it as an array. I am new to stuff like these...can anybody help me out !! Thanks nuke
8
5791
by: Morpheus | last post by:
I am trying to test a function that outputs text to the standard output, presumably using a cout call. I do not have access to the source, but need to test the output. Is this possible? I can technically create an executable that calls the function and then using a command prompt call the exexutable using the > output.txt redirector but I would prefer to do everything within the application itself. Is there a way for force an output...
17
9469
by: mansb2002 | last post by:
Hi, We recently moved our webserver from Win2K to Win2003. The application works fine. Only problem is that when user views a report as a PDF, IE does not show it. The following code is used to redirect: Response.Redirect("/website/pdffiles/myreport.pdf"); Response.End; IE opens a "File Download" box and if you click the "open" button then
116
4643
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however, when I run it, it stalls and does nothing. I'm wondering if there's something obvious that I'm missing. My code is below and any help would be appreciated. Thanks, Dave
0
10332
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
10320
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
9150
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
7620
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
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5521
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...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
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.