如何控制 glib 中调试信息的级别?

发布于 2024-12-06 04:11:24 字数 171 浏览 0 评论 0原文

我有一个用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沒落の蓅哖 2024-12-13 04:11:24

您可以尝试设置 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 值。
希望下面的代码示例能够提供一些指导

#include <glib.h>
#include <stdio.h>
#include <string.h>

#define G_LOG_DOMAIN    ((gchar*) 0)

static void _dummy(const gchar *log_domain,
                     GLogLevelFlags log_level,
                     const gchar *message,
                     gpointer user_data )

{
  /* Dummy does nothing */ 
  return ;      
}

int main(int argc, char **argv)
{
    /* Set dummy for all levels */
    g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, _dummy, NULL);
    /* Set default handler based on argument for appropriate log level */
    if ( argc > 1)
    {
         /* If -vv passed set to ONLY debug */
         if(!strncmp("-vv", argv[1], 3))
         {
            g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,  g_log_default_handler, NULL);
         }
         /* If -v passed set to ONLY info */
         else if(!strncmp("-v", argv[1], 2))
         {
             g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, g_log_default_handler, NULL);
         }
        /* For everything else, set to back to default*/
         else
         {
              g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, g_log_default_handler, NULL);
         }

    }
    else /* If no arguments then set to ONLY warning & critical levels */
    {
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING| G_LOG_LEVEL_CRITICAL, g_log_default_handler, NULL);
    }

    g_warning("This is warning\n");
    g_message("This is message\n");
    g_debug("This is debug\n");
    g_critical("This is critical\n");
    g_log(NULL, G_LOG_LEVEL_INFO , "This is info\n");
    return 0;
}

希望这会有所帮助!

You could try setting G_DEBUG environment variable as mentioned in the GLib developer site. Please refer to Environment variable section under Running 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 to g_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 manipulate GLogLevelFlags values as per your need.
Hope the below code sample will provide some pointers

#include <glib.h>
#include <stdio.h>
#include <string.h>

#define G_LOG_DOMAIN    ((gchar*) 0)

static void _dummy(const gchar *log_domain,
                     GLogLevelFlags log_level,
                     const gchar *message,
                     gpointer user_data )

{
  /* Dummy does nothing */ 
  return ;      
}

int main(int argc, char **argv)
{
    /* Set dummy for all levels */
    g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, _dummy, NULL);
    /* Set default handler based on argument for appropriate log level */
    if ( argc > 1)
    {
         /* If -vv passed set to ONLY debug */
         if(!strncmp("-vv", argv[1], 3))
         {
            g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,  g_log_default_handler, NULL);
         }
         /* If -v passed set to ONLY info */
         else if(!strncmp("-v", argv[1], 2))
         {
             g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, g_log_default_handler, NULL);
         }
        /* For everything else, set to back to default*/
         else
         {
              g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, g_log_default_handler, NULL);
         }

    }
    else /* If no arguments then set to ONLY warning & critical levels */
    {
        g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING| G_LOG_LEVEL_CRITICAL, g_log_default_handler, NULL);
    }

    g_warning("This is warning\n");
    g_message("This is message\n");
    g_debug("This is debug\n");
    g_critical("This is critical\n");
    g_log(NULL, G_LOG_LEVEL_INFO , "This is info\n");
    return 0;
}

Hope this helps!

春庭雪 2024-12-13 04:11:24

我实现了自定义日志处理程序,结果如下:

void custom_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
    gint debug_level = GPOINTER_TO_INT (user_data);

    /* filter out messages depending on debugging level */
    if ((log_level & G_LOG_LEVEL_DEBUG) && debug_level < MyLogLevel_DEBUG) {
        return;
    }
    else if ((log_level & G_LOG_LEVEL_INFO) && debug_level < MyLogLevel_INFO) {
        return;
    }

    g_printf ("%s\n", message);

}

int main(int argc, char *argv[])
{
    ...
    if (verbose) {
        g_log_set_handler (NULL, G_LOG_LEVEL_MASK, custom_log_handler, GINT_TO_POINTER (MyLogLevel_DEBUG));
    }
    else {
        g_log_set_handler (NULL, G_LOG_LEVEL_MASK, custom_log_handler, GINT_TO_POINTER (MyLogLevel_NORMAL));
    }
    ...
}

我希望这对某人有帮助:-)

I implemented custom log handler and here is how it turned out:

void custom_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
{
    gint debug_level = GPOINTER_TO_INT (user_data);

    /* filter out messages depending on debugging level */
    if ((log_level & G_LOG_LEVEL_DEBUG) && debug_level < MyLogLevel_DEBUG) {
        return;
    }
    else if ((log_level & G_LOG_LEVEL_INFO) && debug_level < MyLogLevel_INFO) {
        return;
    }

    g_printf ("%s\n", message);

}

int main(int argc, char *argv[])
{
    ...
    if (verbose) {
        g_log_set_handler (NULL, G_LOG_LEVEL_MASK, custom_log_handler, GINT_TO_POINTER (MyLogLevel_DEBUG));
    }
    else {
        g_log_set_handler (NULL, G_LOG_LEVEL_MASK, custom_log_handler, GINT_TO_POINTER (MyLogLevel_NORMAL));
    }
    ...
}

I hope it will be helpful to somebody :-)

奢华的一滴泪 2024-12-13 04:11:24

您可以通过设置 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, predefine G_LOG_DOMAIN as a separate custom log domain string for your application and set G_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文