C Error Reporting
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the
C Error Function
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions c error codes Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million error reporting -1 php programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Error reporting in a C library up vote 17 down vote favorite 4 I am looking for a robust way
Error In C
to report errors in a C library. Consider the simple example of a queue: struct queue *q = malloc(sizeof(*q)); if (NULL == q) { /* malloc failed. now what ? */ return NULL; /* maybe ? */ } Okay, so for that example returning NULL isn't otherwise valid so it makes sense to return it to signal an error. But void *get_data() { /* stuff */ /* Error detected. NULL is a valid
Errno
return, now what ? */ /* stuff */ } What's more, once we signal an error, how to signal what is the error ? I have thought about it and don't have a satisfying solution. Using errno or some other global object isn't something I would like to do (perhaps the functions may be called from multiple threads etc). I thought of making the client supply some "status" object that can be inspected after the call, but that would make the API quite ugly. So what's your take on the subject ? How do you report errors in a clean way ? c design-patterns error-reporting share|improve this question edited Jul 1 '11 at 13:49 asked Jul 1 '11 at 13:42 nc3b 8,42312948 2 Another option is to make the return value have different meanings (or be just the return status) and pass objects (pointers) in parameters ... like, for example, system does –pmg Jul 1 '11 at 13:55 @pmg That too is a sensible approach. –nc3b Jul 1 '11 at 14:03 Honestly, I'm about to just make a error code bitmask and put all of the error fields for my bit reading/writing/etc in it's own struct that I check everytime it's ran. –MarcusJ Dec 12 '15 at 17:16 add a comment| 5 A
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Is this a good error reporting solution http://stackoverflow.com/questions/6548749/error-reporting-in-a-c-library for an embedded C app? up vote 2 down vote favorite 1 Based on the answers to a previous question, here's a design for a simple error reporting system for my embedded C app. I'd appreciate some feedback. My app has code forming an overall program plus several layers of lower-level libraries. At the lowest level, the library gets a byte to use for error codes, so http://stackoverflow.com/questions/1188321/is-this-a-good-error-reporting-solution-for-an-embedded-c-app 255 total errors. They'll be encoded in an enum, like: enum lib1ErrorCodes { ERR_NO_ERROR, ERR_NO_CONNECTION, ... ERR_MISC }; This is passed up the chain either through a global variable: unsigned char lib1ErrNo; The next higher level contains error codes for the libraries it uses: enum lib2ErrorCodes { ERR_NO_ERROR, ERR_LIB1, ERR_FILE_EXISTS, ... ERR_MISC } Lib1's error is detected and is flagged in this level's error variable: unsigned char lib2ErrNo = ERR_LIB1; At the top level, when it becomes time to report all this to the user, these are detected: if (lib3ErrNo == ERR_LIB2) if (lib2ErrNo == ERR_LIB1) printf("Error %d: %s", lib1ErrNo, lib1ErrDesc); The only negatives I can think of in this scheme is the need to set aside a few error codes in each library to point to the libraries under it, and for the top-level program to include all these levels for their error codes. What's the appropriate way to do this, if this isn't it? I want: Unique error propagation all the way up to the top level so I can report it. Preferably not passing around large structures, as this is an embedded app. c embedded share|improve this question asked Jul 27 '09 at 14:01 jparker 6892915
Functions GError * g_error_new () GError * g_error_new_literal () GError * g_error_new_valist () void g_error_free () GError * g_error_copy () gboolean g_error_matches () void g_set_error () void g_set_error_literal () void g_propagate_error () void g_clear_error () void g_prefix_error () void g_propagate_prefixed_error () Types and Values struct GError Includes #include