2010-08-31 03:50:41 +02:00
|
|
|
/******************************************************************************
|
2010-09-01 10:52:02 +02:00
|
|
|
libprozilla - a download accelerator library
|
|
|
|
Copyright (C) 2001 Kalum Somaratna
|
|
|
|
|
|
|
|
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
|
2010-08-31 03:50:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/* Debugging routines. */
|
|
|
|
|
2010-09-01 10:52:02 +02:00
|
|
|
/* $Id$ */
|
2010-08-31 03:50:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "prozilla.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
|
|
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2010-09-01 10:52:02 +02:00
|
|
|
Initialises the debug system, deletes any prior debug.log file if present
|
2010-08-31 03:50:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
void debug_init()
|
|
|
|
{
|
|
|
|
proz_debug_delete_log();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void proz_debug_delete_log()
|
|
|
|
{
|
|
|
|
char logfile_name[PATH_MAX];
|
2010-09-01 10:52:02 +02:00
|
|
|
|
2010-08-31 03:50:41 +02:00
|
|
|
snprintf(logfile_name, PATH_MAX, "%s/debug.log", libprozrtinfo.log_dir);
|
|
|
|
|
|
|
|
|
|
|
|
if (unlink(logfile_name) == -1)
|
|
|
|
{
|
2010-09-01 10:52:02 +02:00
|
|
|
/*
|
|
|
|
* if the file is not present the continue silently
|
|
|
|
*/
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
proz_debug(_("unable to delete the file %s. Reason-: %s"),
|
|
|
|
strerror(errno));
|
|
|
|
}
|
2010-08-31 03:50:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2010-09-01 10:52:02 +02:00
|
|
|
Write a message to the debug-file.
|
2010-08-31 03:50:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
void proz_debug(const char *format, ...)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
va_list args;
|
|
|
|
char message[MAX_MSG_SIZE + 1 + 1];
|
|
|
|
|
|
|
|
char logfile_name[PATH_MAX];
|
2010-09-01 10:52:02 +02:00
|
|
|
|
2010-08-31 03:50:41 +02:00
|
|
|
snprintf(logfile_name, PATH_MAX, "%s/debug.log", libprozrtinfo.log_dir);
|
|
|
|
|
|
|
|
|
|
|
|
pthread_mutex_lock(&debug_mutex);
|
|
|
|
|
|
|
|
if (libprozrtinfo.debug_mode == TRUE)
|
|
|
|
{
|
2010-09-01 10:52:02 +02:00
|
|
|
va_start(args, format);
|
|
|
|
vsnprintf((char *)&message, MAX_MSG_SIZE, format, args);
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
/* Remove all newlines from the end of the string. */
|
|
|
|
while ((message[strlen(message) - 1] == '\r')
|
|
|
|
|| (message[strlen(message) - 1] == '\n'))
|
|
|
|
message[strlen(message) - 1] = '\0';
|
|
|
|
|
|
|
|
/* Append a newline. */
|
|
|
|
message[strlen(message) + 1] = '\0';
|
|
|
|
message[strlen(message)] = '\n';
|
|
|
|
|
|
|
|
if (!(fp = fopen(logfile_name, "at")))
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&debug_mutex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite(message, 1, strlen(message), fp) != strlen(message))
|
|
|
|
{
|
|
|
|
pthread_mutex_unlock(&debug_mutex);
|
|
|
|
fclose(fp);
|
|
|
|
return;
|
|
|
|
}
|
2010-08-31 03:50:41 +02:00
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
pthread_mutex_unlock(&debug_mutex);
|
|
|
|
}
|