Larry Gritz
Larry Gritz

Reputation: 13690

Error handling / error logging in C++ for library/app combo

I've encountered the following problem pattern frequently over the years:

I've done this many times over the years, and am never fully satisfied with the solution. Furthermore, it's the kind of subproblem that's actually not very important to users (they want to see the error log if something goes wrong, but they don't really care about our technique for implementing it), but the topic gets the programmers fired up and they invariably waste inordinate time on this detail and are never quite happy.

Anybody have any wisdom for how to integrate this functionality into a C++ API, or is there an accepted paradigm or a good open source solution (not GPL, please, I'd like a solution I can use in commercial closed apps as well as OSS projects)?

Upvotes: 4

Views: 2071

Answers (2)

user3458
user3458

Reputation:

Log4Cxx should work for you. You need to implement a provider that allows the library user to catch the log output in callbacks. The library would export a function to install the callbacks. That function should, behind the scenes, reconfigure log4cxxx to get rid of all appenders and set up the "custom" appender.

Of course, the library user has an option to not install the callbacks and use log4cxx as is.

Upvotes: 1

Rob Walker
Rob Walker

Reputation: 47462

We use Apache's Log4cxx for logging which isn't perfect, but provides a lot of infrastructure and a consistent approach across projects. I believe it is cross-platform, though we only use it on Windows.

It provides for run time configuration via an ini file which allows you to control how the log file is output, and you could write your own appenders if you want specific behaviour (e.g. an error dialog under the UI).

If clients of your library also adopt it then it would integrate their logging output into the same log file(s).

Differentiation between instances of the main class could be supported using the nested diagnostic context (NDC) feature.

Upvotes: 1

Related Questions