These days, I used glib for coding and g_log to output debug logs. Several problems encoutered.
1) How to make eclipse support glib building.
$ sudo apt-get install glib2-*
eclipse -> project -> Properties -> Settings
Using below command to get include/lib path
$ pkg-config --cflags glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include
$ pkg-config --libs glib-2.0
-lglib-2.0
2) Source code example
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/sockios.h>
#include <linux/netdevice.h>
#include <glib.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);
#if 1
/* 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);
//}
#else
//g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING| G_LOG_LEVEL_CRITICAL, g_log_default_handler, NULL);
#endif
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;
}
$ gcc -g -o g_log_x64 g_log.c -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
$ export G_MESSAGES_DEBUG=all
$ ./g_log_x64 -v
** INFO: This is info
$ ./g_log_x64 -vv
** (process:8573): DEBUG: This is debug
$ ./g_log_x64 -d
** (process:8574): WARNING **: This is warning
** Message: This is message
** (process:8574): DEBUG: This is debug
** (process:8574): CRITICAL **: This is critical
** INFO: This is info
If we have to use sudo to execute the process,
$ sudo ./g_log_x64 -vv
[sudo] password for xxx:
Nothing outputed !!!
This is to say, we have to keep the export when using sudo:
$ sudo -E bash -c './g_log_x64 -vv'
** (process:8581): DEBUG: This is debug
Reference articals:
http://www.agongdai.com/index.php/programming/cc/111-install-glib-and-compile-eclipse
http://stackoverflow.com/questions/7518620/how-to-control-level-of-debugging-info-in-glib
http://stackoverflow.com/questions/4473129/eclipse-cdt-glib-libraries-inclusion-error
http://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo