|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
redirect standard error and assert (how to?)
How do you redirect standard error and assert?
--- (Why? I am creating an web app and assert does not show on the webpage. I do not think standard error does either. Not sure, but fairly certain. These should be logged to an error file, right? ) |
|
#2
|
||||
|
||||
|
I assume you want to do this programmatically, because it's real easy to redirect stuff using the shell. In *nix, you can close stderr (always filehandle 2) and dup() the stdout handle (always filehandle 1). The following code works for me in RH Linux 7.2, FreeBSD 4.6-RELEASE and OpenBSD 3.2-CURRENT with no problems.
Code:
#include <stdio.h>
int main(void) {
close(2); // close stderr
dup(1); // dup stdout, so stderr now points to stdout
fprintf(stdout, "Hello there stdout\n");
fflush(stdout);
fprintf(stderr, "Hello there stderr\n");
return 0;
}
You can test this by redirecting the output to a file and checking that both lines appear in the file. If you remove the close() and dup() lines, then the second fprintf() line will appear on the console even if you redirect the output to a file (as expected). Note that the reason I'm flushing stdout is because if you remove the fflush(), the output of the stderr line might appear earlier than the stdout line. I don't think this is a serious issue for you and you can remove that line if you like. |
|
#3
|
|||
|
|||
|
Let me see if I have this right...
filehandle 1 is stdout, filehandle 2 is stderr, close(2); closes stderr, dup(1); duplicates stdout, ...but how does dup(1); know to duplicated stdout with stderr? Does it just use the first available handle, which is 2 because we just closed it? |
|
#4
|
||||
|
||||
|
Yes, that's how it works!
|
|
#5
|
|||
|
|||
|
Working with your example now, I'll post the code when I get it done. BTW, I had to include unistd.h to get dup to compile:
#include <unistd.h> |
|
#6
|
|||
|
|||
|
I can't redirect the streams to a file. I tried to duplicate a file pointer and can't. Am I suppose to be able to?
|
|
#7
|
|||
|
|||
|
Any ideas on this one, Scorpions4ever? (Recap: I want to redirect stderr to a file.)
1) This works: ----------------- close(2); // close stderr dup(1); // dup stdout, redirects stderr -> stdout ----------------- 2) This doesn't work: ----------------- close(2); // close stderr dup(fp); // dup file pointer, redirects stderr -> file ----------------- fp is a file pointer, I get "warning: passing arg 1 of `dup' makes integer from pointer without a cast" error on compile, and on execute the text "Hello stderr" does not go to the screen or to the file. Last edited by Doucette : February 27th, 2003 at 11:16 AM. |
|
#8
|
||||
|
||||
|
Yep, I think I have a very good idea what you're doing wrong. You haven't posted the declaration of fp, but I'm guessing you declared fp to be of type FILE *. dup() takes an argument of type int, not FILE *. What you want to do is something like this:
close(2); // close stderr dup(fileno(fp)); // dup file pointer, redirects stderr -> file See if that works ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > redirect standard error and assert (how to?) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|