在C ++中的错误处理/错误日志记录库/应用程序组合 [英] Error handling / error logging in C++ for library/app combo

查看:157
本文介绍了在C ++中的错误处理/错误日志记录库/应用程序组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下问题频繁模式历年来:

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


  • 我在写复杂的code对于由一个独立的应用程序,也是核心的库版本,人们可以从其他应用程序内部使用的包。

  • I'm writing complex code for a package comprised of a standalone application and also a library version of the core that people can use from inside other apps.

这两个用户与核心库中创建自己的应用程序和presumably的人很可能都在批处理模式下运行(离线,脚本,远程,和/或命令行),如以及交互。

Both our own app and presumably ones that users create with the core library are likely to be run both in batch mode (off-line, scripted, remote, and/or from command line), as well as interactively.

库/应用​​程序需要复杂而庞大的运行时输入并有可能出现各种错误似的输出,包括严重的错误信息,输入语法警告,状态信息,并运行统计数据。注意,这些是所有的的输出,而不是这将被显示或保存在别处,并使用不同的方法的应用的主要目的

The library/app takes complex and large runtime input and there may be a variety of error-like outputs including severe error messages, input syntax warnings, status messages, and run statistics. Note that these are all incidental outputs, not the primary purpose of the application which would be displayed or saved elsewhere and using different methods.

其中的一些(大概只有极重度的),如果以交互方式运行,可能需要一个对话框;但它需要登录,如果没有在批处理模式下运行拖延用户输入;而如果运行的客户端程序显然想拦截和/或因为它们发生检查错误的库

Some of these (probably only the very severe ones) might require a dialog box if run interactively; but it needs to log without stalling for user input if run in batch mode; and if run as a library the client program obviously wants to intercept and/or examine the errors as they occur.

这一切都必须是跨平台​​:Linux下的Windows,OSX。我们希望解决没有任何平台上的怪异。例如,输出到stderr是好的为Linux,但联系到一个GUI应用程序时,Windows将无法工作。

It all needs to be cross-platform: Linux, Windows, OSX. And we want the solution to not be weird on any platform. For example, output to stderr is fine for Linux, but won't work on Windows when linked to a GUI app.

客户端库的程序可以创建主类的多个实例,这将是很好,如果客户端应用程序可以区分每个实例的单独错误流。

Client programs of the library may create multiple instances of the main class, and it would be nice if the client app could distinguish a separate error stream with each instance.

让我们假设每个人都同意这是该库的方法不够好,通过一个简单的调用(错误code和/或严重程度,然后类printf参数给出错误消息)记录错误。有争议的部分是如何记录或客户端应用程序检索。

Let's assume everybody agrees it's good enough for the library methods to log errors via a simple call (error code and/or severity, then printf-like arguments giving an error message). The contentious part is how this is recorded or retrieved by the client app.

我已经做了很多次,多年来,和上午没有完全满意的解决方案。此外,它是实际上不是对用户非常重要的(他们希望看到的错误日志,如果出现错误,但他们并不真正关心我们实现它的技术)的那种子问题,但主题得到程序员发射了他们不约而同地浪费在这个细节过多的时间和永远不会很高兴。

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.

任何人对如何将这个功能集成到一个C ++ API的智慧,或者是有一个公认的范式或一个很好的开源解决方案(而不是GPL,请,我想一个解决方案,我可以在商业封闭应用的使用以及OSS项目)?

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)?

推荐答案

Log4Cxx应该为你工作。您需要实现一个提供者,允许图书馆用户赶在回调日志输出。该库将导出功能来安装回调。这个函数应该在幕后,重新配置log4cxxx摆脱所有追加程序,并成立了自定义追加程序。

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.

当然,图书馆用户有一个选择不安装回调,并使用log4cxx原样。

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

这篇关于在C ++中的错误处理/错误日志记录库/应用程序组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆