]>
Commit | Line | Data |
---|---|---|
30a4f2a8 | 1 | /* |
396c5745 | 2 | * $Id: debug.cc,v 1.22 1996/09/11 22:39:24 wessels Exp $ |
30a4f2a8 | 3 | * |
4 | * DEBUG: section 0 Debug Routines | |
5 | * AUTHOR: Harvest Derived | |
6 | * | |
7 | * SQUID Internet Object Cache http://www.nlanr.net/Squid/ | |
8 | * -------------------------------------------------------- | |
9 | * | |
10 | * Squid is the result of efforts by numerous individuals from the | |
11 | * Internet community. Development is led by Duane Wessels of the | |
12 | * National Laboratory for Applied Network Research and funded by | |
13 | * the National Science Foundation. | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or modify | |
16 | * it under the terms of the GNU General Public License as published by | |
17 | * the Free Software Foundation; either version 2 of the License, or | |
18 | * (at your option) any later version. | |
19 | * | |
20 | * This program is distributed in the hope that it will be useful, | |
21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 | * GNU General Public License for more details. | |
24 | * | |
25 | * You should have received a copy of the GNU General Public License | |
26 | * along with this program; if not, write to the Free Software | |
27 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
28 | * | |
29 | */ | |
899a9fca | 30 | |
30a4f2a8 | 31 | /* |
32 | * Copyright (c) 1994, 1995. All rights reserved. | |
33 | * | |
34 | * The Harvest software was developed by the Internet Research Task | |
35 | * Force Research Group on Resource Discovery (IRTF-RD): | |
36 | * | |
37 | * Mic Bowman of Transarc Corporation. | |
38 | * Peter Danzig of the University of Southern California. | |
39 | * Darren R. Hardy of the University of Colorado at Boulder. | |
40 | * Udi Manber of the University of Arizona. | |
41 | * Michael F. Schwartz of the University of Colorado at Boulder. | |
42 | * Duane Wessels of the University of Colorado at Boulder. | |
43 | * | |
44 | * This copyright notice applies to software in the Harvest | |
45 | * ``src/'' directory only. Users should consult the individual | |
46 | * copyright notices in the ``components/'' subdirectories for | |
47 | * copyright information about other software bundled with the | |
48 | * Harvest source code distribution. | |
49 | * | |
50 | * TERMS OF USE | |
51 | * | |
52 | * The Harvest software may be used and re-distributed without | |
53 | * charge, provided that the software origin and research team are | |
54 | * cited in any use of the system. Most commonly this is | |
55 | * accomplished by including a link to the Harvest Home Page | |
56 | * (http://harvest.cs.colorado.edu/) from the query page of any | |
57 | * Broker you deploy, as well as in the query result pages. These | |
58 | * links are generated automatically by the standard Broker | |
59 | * software distribution. | |
60 | * | |
61 | * The Harvest software is provided ``as is'', without express or | |
62 | * implied warranty, and with no support nor obligation to assist | |
63 | * in its use, correction, modification or enhancement. We assume | |
64 | * no liability with respect to the infringement of copyrights, | |
65 | * trade secrets, or any patents, and are not responsible for | |
66 | * consequential damages. Proper use of the Harvest software is | |
67 | * entirely the responsibility of the user. | |
68 | * | |
69 | * DERIVATIVE WORKS | |
70 | * | |
71 | * Users may make derivative works from the Harvest software, subject | |
72 | * to the following constraints: | |
73 | * | |
74 | * - You must include the above copyright notice and these | |
75 | * accompanying paragraphs in all forms of derivative works, | |
76 | * and any documentation and other materials related to such | |
77 | * distribution and use acknowledge that the software was | |
78 | * developed at the above institutions. | |
79 | * | |
80 | * - You must notify IRTF-RD regarding your distribution of | |
81 | * the derivative work. | |
82 | * | |
83 | * - You must clearly notify users that your are distributing | |
84 | * a modified version and not the original Harvest software. | |
85 | * | |
86 | * - Any derivative product is also subject to these copyright | |
87 | * and use restrictions. | |
88 | * | |
89 | * Note that the Harvest software is NOT in the public domain. We | |
90 | * retain copyright, as specified above. | |
91 | * | |
92 | * HISTORY OF FREE SOFTWARE STATUS | |
93 | * | |
94 | * Originally we required sites to license the software in cases | |
95 | * where they were going to build commercial products/services | |
96 | * around Harvest. In June 1995 we changed this policy. We now | |
97 | * allow people to use the core Harvest software (the code found in | |
98 | * the Harvest ``src/'' directory) for free. We made this change | |
99 | * in the interest of encouraging the widest possible deployment of | |
100 | * the technology. The Harvest software is really a reference | |
101 | * implementation of a set of protocols and formats, some of which | |
102 | * we intend to standardize. We encourage commercial | |
103 | * re-implementations of code complying to this set of standards. | |
104 | */ | |
ed43818f | 105 | |
44a47c6e | 106 | #include "squid.h" |
090089c4 | 107 | |
090089c4 | 108 | char *_db_file = __FILE__; |
109 | int _db_line = 0; | |
110 | ||
090089c4 | 111 | FILE *debug_log = NULL; |
112 | static char *debug_log_file = NULL; | |
090089c4 | 113 | |
12b9e9b1 | 114 | #define MAX_DEBUG_SECTIONS 50 |
115 | static int debugLevels[MAX_DEBUG_SECTIONS]; | |
116 | ||
090089c4 | 117 | #if defined(__STRICT_ANSI__) |
abafbe26 | 118 | void _db_print(int section, int level, char *format,...) |
090089c4 | 119 | { |
090089c4 | 120 | va_list args; |
121 | #else | |
122 | void _db_print(va_alist) | |
123 | va_dcl | |
124 | { | |
090089c4 | 125 | va_list args; |
12b9e9b1 | 126 | int section; |
12b9e9b1 | 127 | int level; |
128 | char *format = NULL; | |
97f61b08 | 129 | #endif |
95d659f0 | 130 | LOCAL_ARRAY(char, f, BUFSIZ); |
131 | LOCAL_ARRAY(char, tmpbuf, BUFSIZ); | |
090089c4 | 132 | |
133 | if (debug_log == NULL) | |
134 | return; | |
135 | ||
136 | #if defined(__STRICT_ANSI__) | |
97f61b08 | 137 | va_start(args, format); |
090089c4 | 138 | #else |
139 | va_start(args); | |
12b9e9b1 | 140 | section = va_arg(args, int); |
090089c4 | 141 | level = va_arg(args, int); |
142 | format = va_arg(args, char *); | |
97f61b08 | 143 | #endif |
090089c4 | 144 | |
f25418de | 145 | if (level > debugLevels[section]) { |
12b9e9b1 | 146 | va_end(args); |
147 | return; | |
148 | } | |
396c5745 | 149 | #ifdef LOG_FILE_AND_LINE |
150 | sprintf(f, "%s %-10.10s %4d| %s", | |
151 | accessLogTime(squid_curtime), | |
12b9e9b1 | 152 | _db_file, |
153 | _db_line, | |
154 | format); | |
396c5745 | 155 | #else |
156 | sprintf(f, "%s| %s", | |
157 | accessLogTime(squid_curtime), | |
158 | format); | |
159 | #endif | |
090089c4 | 160 | |
db40ae20 | 161 | #if HAVE_SYSLOG |
12b9e9b1 | 162 | /* level 0 go to syslog */ |
6e40f263 | 163 | if ((level == 0) && opt_syslog_enable) { |
12b9e9b1 | 164 | tmpbuf[0] = '\0'; |
165 | vsprintf(tmpbuf, f, args); | |
166 | syslog(LOG_ERR, tmpbuf); | |
167 | } | |
db40ae20 | 168 | #endif /* HAVE_SYSLOG */ |
ccff9601 | 169 | |
12b9e9b1 | 170 | /* write to log file */ |
171 | vfprintf(debug_log, f, args); | |
172 | if (unbuffered_logs) | |
173 | fflush(debug_log); | |
090089c4 | 174 | va_end(args); |
175 | } | |
176 | ||
12b9e9b1 | 177 | static void debugArg(arg) |
c9f06944 | 178 | char *arg; |
12b9e9b1 | 179 | { |
c9f06944 | 180 | int s = 0; |
181 | int l = 0; | |
182 | int i; | |
12b9e9b1 | 183 | |
c9f06944 | 184 | if (!strncasecmp(arg, "ALL", 3)) { |
185 | s = -1; | |
186 | arg += 4; | |
187 | } else { | |
188 | s = atoi(arg); | |
189 | while (*arg && *arg++ != ','); | |
190 | } | |
191 | l = atoi(arg); | |
192 | ||
193 | if (s >= 0) { | |
194 | debugLevels[s] = l; | |
195 | return; | |
196 | } | |
197 | for (i = 0; i < MAX_DEBUG_SECTIONS; i++) | |
198 | debugLevels[i] = l; | |
12b9e9b1 | 199 | } |
200 | ||
ccff9601 | 201 | static void debugOpenLog(logfile) |
202 | char *logfile; | |
203 | { | |
204 | if (logfile == NULL) { | |
205 | debug_log = stderr; | |
206 | return; | |
207 | } | |
208 | if (debug_log_file) | |
5c46528b | 209 | xfree(debug_log_file); |
ccff9601 | 210 | debug_log_file = xstrdup(logfile); /* keep a static copy */ |
f2cc1af5 | 211 | if (debug_log && debug_log != stderr) |
a0bbd6c8 | 212 | fclose(debug_log); |
ccff9601 | 213 | debug_log = fopen(logfile, "a+"); |
214 | if (!debug_log) { | |
215 | fprintf(stderr, "WARNING: Cannot write log file: %s\n", logfile); | |
216 | perror(logfile); | |
217 | fprintf(stderr, " messages will be sent to 'stderr'.\n"); | |
218 | fflush(stderr); | |
219 | debug_log = stderr; | |
220 | } | |
221 | } | |
222 | ||
30a4f2a8 | 223 | void _db_init(logfile, options) |
090089c4 | 224 | char *logfile; |
30a4f2a8 | 225 | char *options; |
090089c4 | 226 | { |
12b9e9b1 | 227 | int i; |
228 | char *p = NULL; | |
229 | char *s = NULL; | |
090089c4 | 230 | |
12b9e9b1 | 231 | for (i = 0; i < MAX_DEBUG_SECTIONS; i++) |
232 | debugLevels[i] = -1; | |
090089c4 | 233 | |
30a4f2a8 | 234 | if (options) { |
235 | p = xstrdup(options); | |
236 | for (s = strtok(p, w_space); s; s = strtok(NULL, w_space)) | |
12b9e9b1 | 237 | debugArg(s); |
12b9e9b1 | 238 | xfree(p); |
239 | } | |
ccff9601 | 240 | debugOpenLog(logfile); |
090089c4 | 241 | |
30a4f2a8 | 242 | #if HAVE_SYSLOG && defined(LOG_LOCAL4) |
6e40f263 | 243 | if (opt_syslog_enable) |
b8de7ebe | 244 | openlog(appname, LOG_PID | LOG_NDELAY | LOG_CONS, LOG_LOCAL4); |
db40ae20 | 245 | #endif /* HAVE_SYSLOG */ |
246 | ||
090089c4 | 247 | } |
248 | ||
090089c4 | 249 | void _db_rotate_log() |
250 | { | |
251 | int i; | |
95d659f0 | 252 | LOCAL_ARRAY(char, from, MAXPATHLEN); |
253 | LOCAL_ARRAY(char, to, MAXPATHLEN); | |
090089c4 | 254 | |
255 | if (debug_log_file == NULL) | |
256 | return; | |
257 | ||
258 | /* Rotate numbers 0 through N up one */ | |
b6f794d6 | 259 | for (i = Config.Log.rotateNumber; i > 1;) { |
090089c4 | 260 | i--; |
261 | sprintf(from, "%s.%d", debug_log_file, i - 1); | |
262 | sprintf(to, "%s.%d", debug_log_file, i); | |
263 | rename(from, to); | |
264 | } | |
265 | /* Rotate the current log to .0 */ | |
b6f794d6 | 266 | if (Config.Log.rotateNumber > 0) { |
090089c4 | 267 | sprintf(to, "%s.%d", debug_log_file, 0); |
268 | rename(debug_log_file, to); | |
269 | } | |
270 | /* Close and reopen the log. It may have been renamed "manually" | |
271 | * before HUP'ing us. */ | |
0a8020d1 | 272 | if (debug_log != stderr) |
b6f794d6 | 273 | debugOpenLog(Config.Log.log); |
090089c4 | 274 | } |