如何控制 glib 中调试信息的级别?
我有一个用 glib/gobject 用 C 编写的库。它通过 g_debug()
调用生成大量调试信息。此信息对于故障排除非常有帮助,但是当库包含在实际应用程序中时,我不希望显示它。所以,基本上我需要一种方法来控制/过滤调试信息量,但我不知道它如何与 glib 一起工作。有人能给我指出正确的方向吗?
I have a library written in C with glib/gobject. It produces significant number of debugging information via g_debug()
call. This info is very helpful for troubleshooting, however I do not want it to be shown, when library is included with actual application. So, basically I need a way to control/filter amount of debugging information and I could not figure out how it supposed to work with glib. Could someone point me in the right direction, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试设置
G_DEBUG
环境变量,如GLib
开发人员网站中所述。请参阅http://developer.gnome.org/glib/2.28/glib-running.html。编辑:更新以在代码中设置记录器。
您可以使用
g_log_set_handler
(http: //developer.gnome.org/glib/2.29/glib-Message-Logging.html#g-log-set-handler )在代码中执行此操作。最初,您可以将日志处理程序设置为一个虚拟函数,该函数显示无内容和内容。然后您可以根据传递的参数将日志处理程序设置为 g_log_default_handler 以设置适当的日志级别。要将日志级别设置为高于设定级别,您需要根据需要操作GLogLevelFlags
值。希望下面的代码示例能够提供一些指导
希望这会有所帮助!
You could try setting
G_DEBUG
environment variable as mentioned in theGLib
developer site. Please refer toEnvironment variable
section underRunning and debugging GLib Applications
in http://developer.gnome.org/glib/2.28/glib-running.html.EDIT: Update to set the logger in the code.
You can use
g_log_set_handler
( http://developer.gnome.org/glib/2.29/glib-Message-Logging.html#g-log-set-handler ) to do this in your code. Initially you can set the log handler to a dummy function which display nothings & then you can set the log handler tog_log_default_handler
based on the argument passed for set appropriate log levels. To set the log levels above a set level you will need to manipulateGLogLevelFlags
values as per your need.Hope the below code sample will provide some pointers
Hope this helps!
我实现了自定义日志处理程序,结果如下:
我希望这对某人有帮助:-)
I implemented custom log handler and here is how it turned out:
I hope it will be helpful to somebody :-)
您可以通过设置
G_MESSAGES_DEBUG
运行应用程序时的环境变量。最简单的方法是当您需要打印调试(日志)消息时使用
$ G_MESSAGES_DEBUG=all ./your-app
。但是,当 G_MESSAGES_DEBUG=all glib 库本身也打印您可能不需要的调试(日志)消息时。因此,预定义
G_LOG_DOMAIN
作为单独的自定义 记录应用程序的域字符串,并在运行时将G_MESSAGES_DEBUG
设置为同一字符串。例如,在编译器标志中使用-DG_LOG_DOMAIN=\"my-app\"
并使用$ G_MESSAGES_DEBUG="my-app" ./your-app
运行应用程序。You can control whether to print debug (log) messages with setting the
G_MESSAGES_DEBUG
environment variable when running your application.The easiest is to use
$ G_MESSAGES_DEBUG=all ./your-app
when you need debugging (log) messages to be printed.However, when
G_MESSAGES_DEBUG=all
glib libraries itself print debug (log) messages too which you may not need. So, predefineG_LOG_DOMAIN
as a separate custom log domain string for your application and setG_MESSAGES_DEBUG
to the same string when running. For example, use-DG_LOG_DOMAIN=\"my-app\"
among compiler flags and run application with$ G_MESSAGES_DEBUG="my-app" ./your-app
.