LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 08-26-2008, 11:07 PM   #1
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Rep: Reputation: 30
Question Help needed for error handling in C


Here's my current code status:

I have 2 complete different modules: a server and a client.

Client sends messages to the server, if server returns an error, client displays it on the output. For e.g. if some API say "sendto()" or "fopen" fails on the server side, i send the perror output to the client and it is displayed. But the error doesn't have any details like function name in which the call failed etc...
Similarly, on the client side, if any API fails, i just display the perror output.

Here's what i want to do:
1) Have a proper error module for showing/logging errors. This can be a single error handling API which will handle all errors.
2) Show a proper description of errors occuring at client side as well as server side. The errors should include function names and maybe some other details (not sure what else to show).
2) The error logging should be optional i.e. based on some environment variable or compilation flag, i should be able to disable the detailed error logging.

Now, i guess i can simply use "__FUNCTION__" macro for getting the current function name and "__LINE__" macro for getting the current line number.

Please suggest something...basically a sample structure of how i can achieve the above will be of great help and how can i disable error handling based on some flag/variable value?

Thanks in advance...

Last edited by montylee; 08-26-2008 at 11:10 PM.
 
Old 08-26-2008, 11:28 PM   #2
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
In most *NIX programs, informational messages are sent to stdout and error messages are sent to stderr. This makes it easier for people to send these different streams to different logs.

Identifying the particular file/function/line is the job of the coder as he (or she) writes the error or informational messages. A typical message:

fprintf(stderr, __FILE__ ": "%d: blah blah blah\n", __LINE__);

Note that there isn't even a function name - but you can provide one if you wish. Also note that the file name is provided, not the program name. You can put some short name in to identify your program if you wish; otherwise if you look at a log file shared by different programs you can become a little confused. The exampe above also assumes sane filenames; interesting things happen if you give strange names to your .c files such as "%s%dSome_ridiculous_filename%x"
 
Old 08-27-2008, 12:27 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Use syslog(3) if the client or server will not be attached to a terminal.
 
Old 08-27-2008, 12:48 AM   #4
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
thanks for the comments. Mr. C, the client will be attached to the terminal, the server might or might not be...

I was thinking that maybe i can also use assert() for my purpose.

Is it correct to have a single error display/logging function which will accept either string or error code as argument and display the error?
 
Old 08-27-2008, 12:52 AM   #5
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
How would the function differentiate between an error code and a string? You'll be passing either a pointer to a string, or an integer. Would the function guess as to whether it should dereference the pointer or assume an error code?

If you are going to have various data types being passed, you need to encapsulate pass a type argument to indicate what type of data is being passed.

Last edited by Mr. C.; 08-27-2008 at 12:54 AM.
 
Old 08-27-2008, 03:57 AM   #6
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
no no. I'll use either string or error code. I wanted to know which approach is better. I think using error code is better.
 
Old 08-27-2008, 04:32 AM   #7
pinniped
Senior Member
 
Registered: May 2008
Location: planet earth
Distribution: Debian
Posts: 1,732

Rep: Reputation: 50
Quote:
Originally Posted by montylee View Post
I was thinking that maybe i can also use assert() for my purpose.
Isn't assert() and ASSERT() used only for test/debugging but stripped for the release version?

Also it is very bad practice to simply kill the process whenever something goes wrong. There are very few situations where killing the process is the most sensible thing to do.
 
Old 08-27-2008, 06:37 AM   #8
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
ya, assert is used for the debug purposes. I need to access where and when to use assert.
And you are right that killing a process should only be done when it is required.
 
Old 08-27-2008, 07:13 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
Quote:
Originally Posted by montylee View Post
no no. I'll use either string or error code. I wanted to know which approach is better. I think using error code is better.
Why not do what HTTP does - return a code and a description... e.g. 404 Page not found.
 
Old 08-27-2008, 07:52 AM   #10
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
but the code will not be meaningful to the user. So, i think it's best to give a normal error message to the user and if debug flag is enabled, detailed error message can be displayed.
 
Old 08-27-2008, 08:09 AM   #11
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 62
The drawback is that if you ever translate the error messages, you will have to re-write any software which parses the error messages and decides what to do. By including a numerical code first, you can check that programatically, and append a human-readable description after it.
 
Old 08-27-2008, 10:44 PM   #12
montylee
Member
 
Registered: May 2003
Location: India
Distribution: Ubuntu 7.04, Fedora Core 9
Posts: 168

Original Poster
Rep: Reputation: 30
i was thinking of using error codes in a header file. So, the error handling function will read the error code and display the appropriate message. I can always add more error codes and strings to the header later...
 
  


Reply

Tags
error


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hardware Error Handling with C or C++ benz Programming 1 10-26-2007 10:18 AM
Error Handling Techniques ( C ) Centinul Programming 6 10-12-2006 10:49 AM
c++ opendir() handling error bendeco13 Programming 1 11-06-2005 02:37 AM
libjpeg error handling luigi Programming 1 04-19-2005 04:43 AM
xinetd error handling iftiuk Linux - Networking 0 04-21-2004 02:32 PM


All times are GMT -5. The time now is 04:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration