How to control the level of debugging information in glib?

I have a library written in C with glib / gobject. It produces a significant amount of debugging information through a call g_debug(). This information is very useful for troubleshooting, however I do not want it to be displayed when the library is included in the actual application. So basically I need a way to control / filter the amount of debugging information, and I could not figure out how it should work with glib. Can someone point me in the right direction please?

+5
source share
2 answers

You can try setting the environment variable G_DEBUGas indicated on the developer's site GLib. See the Environment variablesection Running and debugging GLib Applicationsat http://developer.gnome.org/glib/2.28/glib-running.html .

EDIT: , . 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;
}

, !

+10

:

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));
    }
    ...
}

, -: -)

+1

All Articles