C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 19th, 2003, 12:29 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
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? )

Reply With Quote
  #2  
Old February 19th, 2003, 01:59 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
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.

Reply With Quote
  #3  
Old February 19th, 2003, 02:21 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
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?

Reply With Quote
  #4  
Old February 19th, 2003, 02:22 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
Yes, that's how it works!

Reply With Quote
  #5  
Old February 19th, 2003, 02:31 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
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>

Reply With Quote
  #6  
Old February 19th, 2003, 03:00 PM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 7
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?

Reply With Quote
  #7  
Old February 27th, 2003, 11:13 AM
Matthew Doucette Matthew Doucette is offline
matthewdoucette.com
Dev Shed Novice (500 - 999 posts)
 
Join Date: May 2002
Posts: 635 Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level)Matthew Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 9 h 59 m 37 sec
Reputation Power: 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.

Reply With Quote
  #8  
Old February 27th, 2003, 01:49 PM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,430 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 4 Weeks 1 Day 21 h 41 m 55 sec
Reputation Power: 784
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > redirect standard error and assert (how to?)


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway