Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control level of debugging info in glib?

Tags:

c

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?

like image 791
dtoux Avatar asked Sep 22 '11 17:09

dtoux


1 Answers

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!

like image 91
another.anon.coward Avatar answered Sep 28 '22 09:09

another.anon.coward