Error Handling Strategies in a Shared Library - C

I am writing a cross-platform shared library ( .so on linux and .dll on Windows) using C. Currently, when there is an error, the library functions return the correct error code and write the error information to stderr library functions also pass information and debugging messages on stdout . This works well for console clients.

Now this library will have client programs that use GUIs programmed using C ++ and wxWidgets. I am wondering what would be the best error handling methods and notification of this? Can a UI application access data on stdout and stderr on all platforms?

An alternative way that I was thinking about is to initialize a library that initializes a structure that will contain pointers to functions. All library functions will have an instance of this structure and call function pointers. Thus, the client can choose where to print messages.

I am wondering what would be the obvious way to solve this? Any help would be great.

+7
c ++ c error-handling shared-libraries
source share
4 answers

Best practice (IMHO) is that the library does not print anything for stderr (or stdout) because they may not even be present. In addition to the GUI situation, you also have the option of using a server application that does not have a “console” and may want to log errors using a function such as syslog ().

Some approaches to processing error information without direct printing:

  • returns a numerical error code and provides a function to turn it into a string

  • returns an error code of a structure / object that contains additional information

  • provides a "session" object function that returns information about the last error

  • allow the caller to register a callback that is called in case of an error

The only exception to the “do not write to stderr from the library” rule, in which I am quite convenient, is if the library has a parameter “debug mode” that allows you to register detailed information in stderr.

+14
source share

In general, you should not write to stdout at all from your library - even in a console application, which can damage the output that the application produces. stderr is a little more forgiving, but you shouldn't use it anyway unless the application requests it.

OpenSSL is a cross-platform shared library that solved the same problem. Their method contains detailed information about library write errors in the internal error queue, which the application may request when viewing the return value of the error, and then present it to the user, depending on which method is suitable. (It also provides a convenience function that passes the entire error queue to FILE * ).

+10
source share

For log messages, you must allow the client to provide a callback function to the library so that the client can decide what to do with them, for example. send to syslog or display in a window on the screen.

To return errors, you have three main strategies:

  • returns an error code and has a function that translates it into a message
  • pass a pointer parameter that points to the object that will contain the error information.
  • There is some kind of global library object containing error information from the last operation.

No matter what you do, you don’t just want to log an error message because the client may want to do something with it. for example, enter a dialog box.

I would probably go with 2 basically.

+2
source share

On linux, instead of printing the error on standard output (or standard error), you should log the error using rsyslog . Since you are dealing with a graphical interface, perhaps you can also open a message box (not always).

I have no idea about windows, but I think he has something similar.

+1
source share

All Articles