2008-08-01 12:26:18 +01:00
|
|
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2007-2008 Richard Hughes <richard@hughsie.com>
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the GNU General Public License Version 2
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* SECTION:egg-debug
|
2008-08-01 12:26:18 +01:00
|
|
|
* @short_description: Debugging functions
|
|
|
|
|
*
|
|
|
|
|
* This file contains functions that can be used for debugging.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
|
#include <glib/gprintf.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <execinfo.h>
|
|
|
|
|
|
2008-08-28 15:49:19 +01:00
|
|
|
#include "egg-debug.h"
|
2008-08-01 12:26:18 +01:00
|
|
|
|
|
|
|
|
#define CONSOLE_RESET 0
|
|
|
|
|
#define CONSOLE_BLACK 30
|
|
|
|
|
#define CONSOLE_RED 31
|
|
|
|
|
#define CONSOLE_GREEN 32
|
|
|
|
|
#define CONSOLE_YELLOW 33
|
|
|
|
|
#define CONSOLE_BLUE 34
|
|
|
|
|
#define CONSOLE_MAGENTA 35
|
|
|
|
|
#define CONSOLE_CYAN 36
|
|
|
|
|
#define CONSOLE_WHITE 37
|
|
|
|
|
|
2008-08-28 15:49:19 +01:00
|
|
|
static gint fd = -1;
|
|
|
|
|
|
2008-08-01 12:26:18 +01:00
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* pk_set_console_mode:
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_set_console_mode (guint console_code)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
gchar command[13];
|
|
|
|
|
|
|
|
|
|
/* don't put extra commands into logs */
|
2008-10-23 12:03:35 +01:00
|
|
|
if (!egg_debug_is_console ())
|
2008-08-01 12:26:18 +01:00
|
|
|
return;
|
2008-10-23 12:03:35 +01:00
|
|
|
|
2008-08-01 12:26:18 +01:00
|
|
|
/* Command is the control command to the terminal */
|
|
|
|
|
g_snprintf (command, 13, "%c[%dm", 0x1B, console_code);
|
|
|
|
|
printf ("%s", command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* egg_debug_backtrace:
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
void
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_debug_backtrace (void)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
void *call_stack[512];
|
|
|
|
|
int call_stack_size;
|
|
|
|
|
char **symbols;
|
|
|
|
|
int i = 1;
|
|
|
|
|
|
|
|
|
|
call_stack_size = backtrace (call_stack, G_N_ELEMENTS (call_stack));
|
|
|
|
|
symbols = backtrace_symbols (call_stack, call_stack_size);
|
|
|
|
|
if (symbols != NULL) {
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_set_console_mode (CONSOLE_RED);
|
2008-08-01 12:26:18 +01:00
|
|
|
g_print ("Traceback:\n");
|
|
|
|
|
while (i < call_stack_size) {
|
|
|
|
|
g_print ("\t%s\n", symbols[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_set_console_mode (CONSOLE_RESET);
|
2008-08-01 12:26:18 +01:00
|
|
|
free (symbols);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* pk_log_line:
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
static void
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_log_line (const gchar *buffer)
|
|
|
|
|
{
|
|
|
|
|
ssize_t count;
|
|
|
|
|
/* open a file */
|
|
|
|
|
if (fd == -1) {
|
|
|
|
|
/* ITS4: ignore, /var/log/foo is owned by root, and this is just debug text */
|
|
|
|
|
fd = open (EGG_LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0777);
|
2008-10-23 12:03:35 +01:00
|
|
|
if (fd == -1)
|
2008-08-28 15:49:19 +01:00
|
|
|
g_error ("could not open log: '%s'", EGG_LOG_FILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ITS4: ignore, debug text always NULL terminated */
|
|
|
|
|
count = write (fd, buffer, strlen (buffer));
|
2008-10-23 12:03:35 +01:00
|
|
|
if (count == -1)
|
2008-08-28 15:49:19 +01:00
|
|
|
g_warning ("could not write %s", buffer);
|
|
|
|
|
/* newline */
|
|
|
|
|
count = write (fd, "\n", 1);
|
2008-10-23 12:03:35 +01:00
|
|
|
if (count == -1)
|
2008-08-28 15:49:19 +01:00
|
|
|
g_warning ("could not write newline");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* pk_print_line:
|
|
|
|
|
**/
|
|
|
|
|
static void
|
|
|
|
|
pk_print_line (const gchar *func, const gchar *file, const int line, const gchar *buffer, guint color)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
gchar *str_time;
|
|
|
|
|
gchar *header;
|
|
|
|
|
time_t the_time;
|
|
|
|
|
GThread *thread;
|
|
|
|
|
|
|
|
|
|
time (&the_time);
|
|
|
|
|
str_time = g_new0 (gchar, 255);
|
|
|
|
|
strftime (str_time, 254, "%H:%M:%S", localtime (&the_time));
|
|
|
|
|
thread = g_thread_self ();
|
|
|
|
|
|
|
|
|
|
/* generate header text */
|
|
|
|
|
header = g_strdup_printf ("TI:%s\tTH:%p\tFI:%s\tFN:%s,%d", str_time, thread, file, func, line);
|
|
|
|
|
g_free (str_time);
|
|
|
|
|
|
|
|
|
|
/* always in light green */
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_set_console_mode (CONSOLE_GREEN);
|
2008-08-01 12:26:18 +01:00
|
|
|
printf ("%s\n", header);
|
|
|
|
|
|
2008-11-06 09:22:29 +00:00
|
|
|
/* different colors according to the severity */
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_set_console_mode (color);
|
2008-08-01 12:26:18 +01:00
|
|
|
printf (" - %s\n", buffer);
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_set_console_mode (CONSOLE_RESET);
|
|
|
|
|
|
|
|
|
|
/* log to a file */
|
2008-10-23 12:03:35 +01:00
|
|
|
if (egg_debug_is_logging ()) {
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_log_line (header);
|
|
|
|
|
pk_log_line (buffer);
|
|
|
|
|
}
|
2008-08-01 12:26:18 +01:00
|
|
|
|
|
|
|
|
/* flush this output, as we need to debug */
|
|
|
|
|
fflush (stdout);
|
|
|
|
|
|
|
|
|
|
g_free (header);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* egg_debug_real:
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
void
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_debug_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
gchar *buffer = NULL;
|
|
|
|
|
|
2008-10-23 12:03:35 +01:00
|
|
|
if (!egg_debug_enabled ())
|
2008-08-01 12:26:18 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
|
g_vasprintf (&buffer, format, args);
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_print_line (func, file, line, buffer, CONSOLE_BLUE);
|
2008-08-01 12:26:18 +01:00
|
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* egg_warning_real:
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
void
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_warning_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
gchar *buffer = NULL;
|
|
|
|
|
|
2008-10-23 12:03:35 +01:00
|
|
|
if (!egg_debug_enabled ())
|
2008-08-01 12:26:18 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
|
g_vasprintf (&buffer, format, args);
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
|
|
/* do extra stuff for a warning */
|
2008-10-23 12:03:35 +01:00
|
|
|
if (!egg_debug_is_console ())
|
2008-08-01 12:26:18 +01:00
|
|
|
printf ("*** WARNING ***\n");
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_print_line (func, file, line, buffer, CONSOLE_RED);
|
2008-08-01 12:26:18 +01:00
|
|
|
|
|
|
|
|
g_free(buffer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* egg_error_real:
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
void
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_error_real (const gchar *func, const gchar *file, const int line, const gchar *format, ...)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
va_list args;
|
|
|
|
|
gchar *buffer = NULL;
|
|
|
|
|
|
|
|
|
|
va_start (args, format);
|
|
|
|
|
g_vasprintf (&buffer, format, args);
|
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
|
|
/* do extra stuff for a warning */
|
2008-10-23 12:03:35 +01:00
|
|
|
if (!egg_debug_is_console ())
|
2008-08-01 12:26:18 +01:00
|
|
|
printf ("*** ERROR ***\n");
|
2008-08-28 15:49:19 +01:00
|
|
|
pk_print_line (func, file, line, buffer, CONSOLE_RED);
|
2008-08-01 12:26:18 +01:00
|
|
|
g_free(buffer);
|
|
|
|
|
|
|
|
|
|
/* we want to fix this! */
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_debug_backtrace ();
|
2008-08-01 12:26:18 +01:00
|
|
|
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* egg_debug_enabled:
|
2008-08-01 12:26:18 +01:00
|
|
|
*
|
|
|
|
|
* Returns: TRUE if we have debugging enabled
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_debug_enabled (void)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
2008-10-23 12:03:35 +01:00
|
|
|
const gchar *env;
|
|
|
|
|
env = g_getenv (EGG_VERBOSE);
|
|
|
|
|
return (g_strcmp0 (env, "1") == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* egg_debug_is_logging:
|
|
|
|
|
*
|
|
|
|
|
* Returns: TRUE if we have logging enabled
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
egg_debug_is_logging (void)
|
|
|
|
|
{
|
|
|
|
|
const gchar *env;
|
|
|
|
|
env = g_getenv (EGG_LOGGING);
|
|
|
|
|
return (g_strcmp0 (env, "1") == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* egg_debug_is_console:
|
|
|
|
|
*
|
|
|
|
|
* Returns: TRUE if we have debugging enabled
|
|
|
|
|
**/
|
|
|
|
|
gboolean
|
|
|
|
|
egg_debug_is_console (void)
|
|
|
|
|
{
|
|
|
|
|
const gchar *env;
|
|
|
|
|
env = g_getenv (EGG_CONSOLE);
|
|
|
|
|
return (g_strcmp0 (env, "1") == 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* egg_debug_set_logging:
|
|
|
|
|
**/
|
|
|
|
|
void
|
|
|
|
|
egg_debug_set_logging (gboolean enabled)
|
|
|
|
|
{
|
|
|
|
|
if (enabled)
|
2008-10-23 14:39:44 +01:00
|
|
|
g_setenv (EGG_LOGGING, "1", TRUE);
|
2008-10-23 12:03:35 +01:00
|
|
|
else
|
2008-10-23 14:39:44 +01:00
|
|
|
g_setenv (EGG_LOGGING, "0", TRUE);
|
2008-10-23 12:03:35 +01:00
|
|
|
|
|
|
|
|
if (egg_debug_is_logging ())
|
|
|
|
|
egg_debug ("logging to %s", EGG_LOG_FILE);
|
2008-08-01 12:26:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2008-08-28 15:49:19 +01:00
|
|
|
* egg_debug_init:
|
|
|
|
|
* @debug: If we should print out verbose logging
|
2008-08-01 12:26:18 +01:00
|
|
|
**/
|
|
|
|
|
void
|
2008-08-28 15:49:19 +01:00
|
|
|
egg_debug_init (gboolean debug)
|
2008-08-01 12:26:18 +01:00
|
|
|
{
|
|
|
|
|
/* check if we are on console */
|
2008-10-23 12:03:35 +01:00
|
|
|
if (isatty (fileno (stdout)) == 1)
|
|
|
|
|
g_setenv (EGG_CONSOLE, "1", FALSE);
|
|
|
|
|
else
|
|
|
|
|
g_setenv (EGG_CONSOLE, "0", FALSE);
|
|
|
|
|
if (debug)
|
|
|
|
|
g_setenv (EGG_VERBOSE, "1", FALSE);
|
|
|
|
|
else
|
|
|
|
|
g_setenv (EGG_VERBOSE, "0", FALSE);
|
|
|
|
|
egg_debug ("Verbose debugging %i (on console %i)%s", egg_debug_enabled (), egg_debug_is_console (), EGG_VERBOSE);
|
2008-08-01 12:26:18 +01:00
|
|
|
}
|
|
|
|
|
|