简体   繁体   English

C ++库中的控制调试级别-Linux

[英]Control Debug Level in C++ Library - Linux

I have a C++ library, which is used in both Linux and Windows. 我有一个C ++库,可在Linux和Windows中使用。 I want to enable the user to control the debug level (0 - no debug, 1 - only critical errors ... 5 - informative debug information). 我想使用户能够控制调试级别(0-不调试,1-仅严重错误... 5-信息性调试信息)。

The debug log is printed to a text file. 调试日志将打印到文本文件。

In Windows, I can do it using a registry value (DWORD DebugLevel). 在Windows中,我可以使用注册表值(DWORD DebugLevel)来实现。

What can be a good replacement which works also for Linux? 什么样的替代品对Linux也适用? (Without 3rd party tools, for example Linux "registry"). (没有第三方工具,例如Linux“注册表”)。

Thanks in advance! 提前致谢!

Does your library have some sort of initialisation function? 您的库是否具有某种初始化功能? Make the level a parameter to that function. 将级别作为该函数的参数。 Ideally store the passed-in value in a context structure or class if it makes sense for your API (ie if you require clients to always operate via a "context") - but if not, a global might be reasonable. 如果对API有意义,则理想情况下将传入的值存储在上下文结构或类中(即,如果您要求客户端始终通过“上下文”进行操作)-但是,如果不是,则全局变量可能是合理的。

If it's largely for development purposes (ie the "user" you refer to is a developer using your library, not the end user of that code), the quickest/easiest way is to use an environment variable. 如果主要是出于开发目的(例如,您指的“用户”是使用您的库的开发人员,而不是该代码的最终用户),则最快/最简单的方法是使用环境变量。

If it's to be controlled by the end-user, you probably need to extend your API so that the app developer can set the debug level in code, after reading his configuration files -- you wouldn't normally have a separate config file for just one library used by a program. 如果要由最终用户控制,则可能需要扩展API,以便应用程序开发人员在读取配置文件后可以在代码中设置调试级别-通常,您将没有单独的配置文件程序使用的一个库。

You can use the log4cxx framework. 您可以使用log4cxx框架。 This is configurable through a file. 这可以通过文件进行配置。 I haven't tried it yet, but it should work with Windows too. 我还没有尝试过,但是它也可以在Windows下使用。

您可以在/ etc / YOURAPP或〜/ .YOURAPP或〜/ .config / YOURAPP中使用配置文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM