1 |
/* Copyright (C) 2003 Stephane Thiell |
2 |
* |
3 |
* This file is part of pxyservd (from pxys) |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
* |
19 |
*/ |
20 |
#define RCSID "$Id: debug.c,v 1.1.1.1 2003/12/30 17:09:31 mbuna Exp $" |
21 |
|
22 |
#include "debug.h" |
23 |
|
24 |
#ifdef DEBUG |
25 |
|
26 |
#ifndef DEBUG_FILE |
27 |
#define DEBUG_FILE "debug.log" |
28 |
#endif |
29 |
|
30 |
#include <assert.h> |
31 |
#include <stdarg.h> |
32 |
#include <stdio.h> |
33 |
#include <stdlib.h> |
34 |
#include <string.h> |
35 |
#include <unistd.h> |
36 |
|
37 |
#include <peak/peak.h> |
38 |
|
39 |
static FILE *fp; |
40 |
static int debug_level; |
41 |
static int output_stdout; |
42 |
|
43 |
static void vprint_debug(int level, const char *pattern, va_list vl); |
44 |
|
45 |
void |
46 |
debug_init(int max_level, int out_stdout) |
47 |
{ |
48 |
time_t t; |
49 |
char *ct; |
50 |
|
51 |
debug_level = max_level; |
52 |
output_stdout = out_stdout; |
53 |
|
54 |
if ((fp = fopen(DEBUG_FILE, "a")) == NULL) |
55 |
exit(1); |
56 |
|
57 |
t = peak_time(); |
58 |
ct = ctime(&t); |
59 |
ct[strlen(ct) - 1] = '\0'; |
60 |
debug(DL_BASIC, "[%s] Debug service started", ct); |
61 |
} |
62 |
|
63 |
void |
64 |
debug_dispose(void) |
65 |
{ |
66 |
time_t t = peak_time(); |
67 |
debug(DL_BASIC, "[%s] Stopping debug service", ctime(&t)); |
68 |
fclose(fp); |
69 |
} |
70 |
|
71 |
void |
72 |
debug(int level, const char *fmt, ...) |
73 |
{ |
74 |
va_list vl; |
75 |
va_start(vl, fmt); |
76 |
vprint_debug(level, fmt, vl); |
77 |
va_end(vl); |
78 |
} |
79 |
|
80 |
static void |
81 |
vprint_debug(int level, const char *fmt, va_list vl) |
82 |
{ |
83 |
char buffer[1024]; |
84 |
|
85 |
if (level <= debug_level) |
86 |
{ |
87 |
vsnprintf(buffer, sizeof(buffer), fmt, vl); |
88 |
fprintf(fp, "%s\n", buffer); |
89 |
if (output_stdout) |
90 |
fprintf(stdout, "%s\n", buffer); |
91 |
} |
92 |
} |
93 |
|
94 |
#endif /* DEBUG */ |