Skip to content

Commit

Permalink
Added the ability to log all debug messages to a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
allinurl committed Jan 3, 2014
1 parent f2b5607 commit 5a7145f
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ goaccess_SOURCES = \
xmalloc.h

if DEBUG
AM_CFLAGS = -O0 -g
AM_CFLAGS = -DDEBUG -O0 -g
else
AM_CFLAGS = -O2
endif
Expand Down
6 changes: 6 additions & 0 deletions commons.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ extern size_t real_size_y;
extern size_t term_h;
extern size_t term_w;

#ifdef DEBUG
#define LOG_DEBUG(x, ...) do { dbg_fprintf x; } while (0)
#else
#define LOG_DEBUG(x, ...) do { } while (0)
#endif

#endif
34 changes: 34 additions & 0 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@

#include "error.h"
#include "commons.h"
#include "util.h"

static FILE *log_file;

void
error_handler (const char *func, const char *file, int line, const char *msg)
Expand All @@ -47,3 +50,34 @@ error_handler (const char *func, const char *file, int line, const char *msg)
fprintf (stderr, "\nMessage: %s\n\n", msg);
exit (EXIT_FAILURE);
}

void
dbg_log_open (const char *path)
{
if (path != NULL) {
log_file = fopen (path, "w");
if (log_file == NULL)
return;
}
}

void
dbg_log_close (void)
{
if (log_file != NULL)
fclose (log_file);
}

void
dbg_fprintf (const char *fmt, ...)
{
va_list args;

if (!log_file)
return;

va_start (args, fmt);
vfprintf (log_file, fmt, args);
fflush (log_file);
va_end (args);
}
4 changes: 4 additions & 0 deletions error.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@
void error_handler (const char *func, const char *file, int line,
const char *msg);

void dbg_fprintf (const char *fmt, ...);
void dbg_log_open (const char *file);
void dbg_log_close (void);

#endif
22 changes: 22 additions & 0 deletions goaccess.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,20 @@
#include "util.h"

static WINDOW *header_win, *main_win;

#ifdef HAVE_LIBGEOIP
#ifdef DEBUG
static char short_options[] = "f:e:p:o:l:acrmMghHqd";
#else
static char short_options[] = "f:e:p:o:acrmMghHqd";
#endif
#else
#ifdef DEBUG
static char short_options[] = "f:e:p:o:l:acrmMhHqd";
#else
static char short_options[] = "f:e:p:o:acrmMhHqd";
#endif
#endif

GConf conf = { 0 };

Expand Down Expand Up @@ -152,6 +161,9 @@ cmd_help (void)
printf (" HOST module. Disabled by default.\n");
#ifdef HAVE_LIBGEOIP
printf (" -g - Standard GeoIP database for less memory usage.\n");
#endif
#ifdef DEBUG
printf (" -l - Send all debug messages to the specified file.\n");
#endif
printf (" -h - This help.\n");
printf (" -H - Include the HTTP request protocol.\n");
Expand Down Expand Up @@ -217,6 +229,12 @@ house_keeping (void)
g_hash_table_destroy (ht_status_code);
g_hash_table_destroy (ht_unique_vis);
g_hash_table_destroy (ht_unique_visitors);

if (conf.debug_log) {
LOG_DEBUG (("Bye.\n"));
dbg_log_close ();
free (conf.debug_log);
}
}

/* allocate memory for an instance of holder */
Expand Down Expand Up @@ -790,6 +808,10 @@ main (int argc, char *argv[])
case 'h':
cmd_help ();
break;
case 'l':
conf.debug_log = alloc_string (optarg);
dbg_log_open (conf.debug_log);
break;
case 'H':
conf.append_protocol = 1;
break;
Expand Down
3 changes: 2 additions & 1 deletion settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ typedef struct GConfKeyword_
typedef struct GConf_
{
char *date_format;
char *debug_log;
char *iconfigfile;
char *ifile;
char *ignore_host;
char *log_format;
char *output_format;
int append_method;
int append_protocol;
int bandwidth;
int color_scheme;
int enable_html_resolver;
int geo_db;
int ignore_qstr;
int append_protocol;
int list_agents;
int load_conf_dlg;
int mouse_support;
Expand Down

0 comments on commit 5a7145f

Please sign in to comment.