forked from dbmail/dbmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdm_debug.c
175 lines (149 loc) · 4.34 KB
/
dm_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright (C) 1999-2004 IC & S dbmail@ic-s.nl
Copyright (c) 2004-2011 NFG Net Facilities Group BV support@nfg.nl
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
*
* Debugging and memory checking functions */
#include "dbmail.h"
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(BSD4_4)
extern char *__progname; /* Program name, from crt0. */
#else
char *__progname = NULL;
#endif
char hostname[16];
/* the debug variables */
static trace_t TRACE_SYSLOG = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING; /* default: emerg, alert, crit, err, warning */
static trace_t TRACE_STDERR = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING; /* default: emerg, alert, crit, err, warning */
/*
* configure the debug settings
*/
void configure_debug(trace_t trace_syslog, trace_t trace_stderr)
{
TRACE_SYSLOG = trace_syslog;
TRACE_STDERR = trace_stderr;
}
/* Make sure that these match trace_t. */
void null_logger(const char UNUSED *log_domain, GLogLevelFlags UNUSED log_level, const char UNUSED *message, gpointer UNUSED data)
{
// ignore glib messages
return;
}
static const char * trace_to_text(trace_t level)
{
const char * const trace_text[] = {
"EMERGENCY",
"Alert",
"Critical",
"Error",
"Warning",
"Notice",
"Info",
"Debug",
"Database"
};
return trace_text[ilogb((double) level)];
}
/* Call me like this:
*
* TRACE(TRACE_ERR, "Something happened with error code [%d]", resultvar);
*
* Please #define THIS_MODULE "mymodule" at the top of each file.
* Arguments for __FILE__ and __func__ are added by the TRACE macro.
*
* trace() and TRACE() are macros in debug.h
*
*/
#define SYSLOGFORMAT "[%p] %s:[%s] %s(+%d): %s"
#define STDERRFORMAT "%s %s %s[%d]: [%p] %s:[%s] %s(+%d): %s"
void trace(trace_t level, const char * module, const char * function, int line, const char *formatstring, ...)
{
trace_t syslog_level;
va_list ap, cp;
gchar *message = NULL;
static int configured=0;
size_t l, maxlen=120;
/* Return now if we're not logging anything. */
if ( !(level & TRACE_STDERR) && !(level & TRACE_SYSLOG))
return;
va_start(ap, formatstring);
va_copy(cp, ap);
message = g_strdup_vprintf(formatstring, cp);
va_end(cp);
l = strlen(message);
if (message[l] == '\n')
message[l] = '\0';
if (level & TRACE_STDERR) {
time_t now = time(NULL);
struct tm tmp;
char date[32];
if (! configured) {
memset(hostname,'\0',sizeof(hostname));
gethostname(hostname,15);
configured=1;
}
memset(date,0,sizeof(date));
localtime_r(&now, &tmp);
strftime(date,32,"%b %d %H:%M:%S", &tmp);
fprintf(stderr, STDERRFORMAT, date, hostname, __progname?__progname:"", getpid(),
g_thread_self(), trace_to_text(level), module, function, line, message);
if (message[l] != '\n')
fprintf(stderr, "\n");
fflush(stderr);
}
if (level & TRACE_SYSLOG) {
/* Convert our extended log levels (>128) to syslog levels */
switch((int)ilogb((double) level))
{
case 0:
syslog_level = LOG_EMERG;
break;
case 1:
syslog_level = LOG_ALERT;
break;
case 2:
syslog_level = LOG_CRIT;
break;
case 3:
syslog_level = LOG_ERR;
break;
case 4:
syslog_level = LOG_WARNING;
break;
case 5:
syslog_level = LOG_NOTICE;
break;
case 6:
syslog_level = LOG_INFO;
break;
case 7:
syslog_level = LOG_DEBUG;
break;
case 8:
syslog_level = LOG_DEBUG;
break;
default:
syslog_level = LOG_DEBUG;
break;
}
size_t w = min(l,maxlen);
message[w] = '\0';
syslog(syslog_level, SYSLOGFORMAT, g_thread_self(), trace_to_text(level), module, function, line, message);
}
g_free(message);
/* Bail out on fatal errors. */
if (level == TRACE_EMERG)
exit(EX_TEMPFAIL);
}