开发者

How to control level of debugging info in glib?

开发者 https://www.devze.com 2023-04-06 14:40 出处:网络
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

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?


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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消