XMMS2

src/xmms/log.c

Go to the documentation of this file.
00001 /*  XMMS2 - X Music Multiplexer System
00002  *  Copyright (C) 2003-2009 XMMS2 Team
00003  *
00004  *  PLUGINS ARE NOT CONSIDERED TO BE DERIVED WORK !!!
00005  *
00006  *  This library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License as published by the Free Software Foundation; either
00009  *  version 2.1 of the License, or (at your option) any later version.
00010  *
00011  *  This library is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *  Lesser General Public License for more details.
00015  */
00016 
00017 /** @file
00018  * Logging functions.
00019  *
00020  */
00021 
00022 #include <time.h>
00023 #include <stdio.h>
00024 #include <string.h>
00025 #include <stdlib.h>
00026 #include <glib.h>
00027 #include "xmmspriv/xmms_log.h"
00028 #include "xmmspriv/xmms_localtime.h"
00029 
00030 static gchar *logts_format = NULL;
00031 static void xmms_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data);
00032 
00033 
00034 void
00035 xmms_log_set_format (const gchar *format)
00036 {
00037     /* copying the string ensures the formatting directives will
00038      * last until log_shutdown */
00039     g_free (logts_format);
00040     logts_format = g_strdup (format);
00041 }
00042 
00043 void
00044 xmms_log_init (gint verbosity)
00045 {
00046     g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL, xmms_log_handler,
00047                        GINT_TO_POINTER (verbosity));
00048 
00049     xmms_log_info ("Initialized logging system :)");
00050 }
00051 
00052 void
00053 xmms_log_shutdown ()
00054 {
00055     xmms_log_info ("Logging says bye bye :)");
00056     g_free (logts_format);
00057     logts_format = NULL;
00058 }
00059 
00060 
00061 static void
00062 xmms_log_handler (const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data)
00063 {
00064     gchar logts_buf[256];
00065     time_t tv = 0;
00066     struct tm st;
00067     const char *level = "??";
00068     gint verbosity = GPOINTER_TO_INT (user_data);
00069 
00070     tv = time (NULL);
00071 
00072     if (log_level & G_LOG_LEVEL_CRITICAL) {
00073         level = " FAIL";
00074     } else if (log_level & G_LOG_LEVEL_ERROR) {
00075         level = "FATAL";
00076     } else if (log_level & G_LOG_LEVEL_WARNING) {
00077         level = "ERROR";
00078     } else if (log_level & G_LOG_LEVEL_MESSAGE) {
00079         level = " INFO";
00080         if (verbosity < 1)
00081             return;
00082     } else if (log_level & G_LOG_LEVEL_DEBUG) {
00083         level = "DEBUG";
00084         if (verbosity < 2)
00085             return;
00086     }
00087 
00088     if (!logts_format ||
00089         !xmms_localtime (&tv, &st) ||
00090         !strftime (logts_buf, sizeof(logts_buf), logts_format, &st)) {
00091         logts_buf[0] = '\0';
00092     }
00093 
00094     printf ("%s%s: %s\n", logts_buf, level, message);
00095 
00096     fflush (stdout);
00097 
00098     if (log_level & G_LOG_LEVEL_ERROR) {
00099         exit (EXIT_FAILURE);
00100     }
00101 }