forked from szekerest/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapdebug.c
381 lines (306 loc) · 10 KB
/
mapdebug.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Implementation of debug/logging, msDebug() and related functions.
* Author: Daniel Morissette
*
******************************************************************************
* Copyright (c) 1996-2007 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "mapserver.h"
#include "maperror.h"
#include "mapthread.h"
#include "maptime.h"
#include <time.h>
#ifndef _WIN32
#include <sys/time.h>
#include <unistd.h>
#endif
#include <stdarg.h>
#ifdef NEED_NONBLOCKING_STDERR
#include <fcntl.h>
#endif
#ifdef _WIN32
#include <windows.h> /* OutputDebugStringA() */
#endif
#ifndef USE_THREAD
debugInfoObj *msGetDebugInfoObj()
{
static debugInfoObj debuginfo = {MS_DEBUGLEVEL_ERRORSONLY,
MS_DEBUGMODE_OFF, NULL, NULL
};
return &debuginfo;
}
#else
static debugInfoObj *debuginfo_list = NULL;
debugInfoObj *msGetDebugInfoObj()
{
debugInfoObj *link;
int thread_id;
debugInfoObj *ret_obj;
msAcquireLock( TLOCK_DEBUGOBJ );
thread_id = msGetThreadId();
/* find link for this thread */
for( link = debuginfo_list;
link != NULL && link->thread_id != thread_id
&& link->next != NULL && link->next->thread_id != thread_id;
link = link->next ) {}
/* If the target thread link is already at the head of the list were ok */
if( debuginfo_list != NULL && debuginfo_list->thread_id == thread_id ) {
}
/* We don't have one ... initialize one. */
else if( link == NULL || link->next == NULL ) {
debugInfoObj *new_link;
new_link = (debugInfoObj *) malloc(sizeof(debugInfoObj));
if (new_link != NULL) {
new_link->next = debuginfo_list;
new_link->thread_id = thread_id;
new_link->global_debug_level = MS_DEBUGLEVEL_ERRORSONLY;
new_link->debug_mode = MS_DEBUGMODE_OFF;
new_link->errorfile = NULL;
new_link->fp = NULL;
} else
msSetError(MS_MEMERR, "Out of memory allocating %u bytes.\n", "msGetDebugInfoObj()", (unsigned int)sizeof(debugInfoObj));
debuginfo_list = new_link;
}
/* If the link is not already at the head of the list, promote it */
else if( link != NULL && link->next != NULL ) {
debugInfoObj *target = link->next;
link->next = link->next->next;
target->next = debuginfo_list;
debuginfo_list = target;
}
ret_obj = debuginfo_list;
msReleaseLock( TLOCK_DEBUGOBJ );
return ret_obj;
}
#endif
/* msSetErrorFile()
**
** Set output target, ready to write to it, open file if necessary
**
** If pszRelToPath != NULL then we will try to make the value relative to
** this path if it is not absolute already and it's not one of the special
** values (stderr, stdout, windowsdebug)
**
** Returns MS_SUCCESS/MS_FAILURE
*/
int msSetErrorFile(const char *pszErrorFile, const char *pszRelToPath)
{
char extended_path[MS_MAXPATHLEN];
debugInfoObj *debuginfo = msGetDebugInfoObj();
if (strcmp(pszErrorFile, "stderr") != 0 &&
strcmp(pszErrorFile, "stdout") != 0 &&
strcmp(pszErrorFile, "windowsdebug") != 0) {
/* Try to make the path relative */
if(msBuildPath(extended_path, pszRelToPath, pszErrorFile) == NULL)
return MS_FAILURE;
pszErrorFile = extended_path;
}
if (debuginfo && debuginfo->errorfile && pszErrorFile &&
strcmp(debuginfo->errorfile, pszErrorFile) == 0) {
/* Nothing to do, already writing to the right place */
return MS_SUCCESS;
}
/* Close current output file if any */
msCloseErrorFile();
/* NULL or empty target will just close current output and return */
if (pszErrorFile == NULL || *pszErrorFile == '\0')
return MS_SUCCESS;
if (strcmp(pszErrorFile, "stderr") == 0) {
debuginfo->fp = stderr;
debuginfo->errorfile = msStrdup(pszErrorFile);
debuginfo->debug_mode = MS_DEBUGMODE_STDERR;
} else if (strcmp(pszErrorFile, "stdout") == 0) {
debuginfo->fp = stdout;
debuginfo->errorfile = msStrdup(pszErrorFile);
debuginfo->debug_mode = MS_DEBUGMODE_STDOUT;
} else if (strcmp(pszErrorFile, "windowsdebug") == 0) {
#ifdef _WIN32
debuginfo->errorfile = msStrdup(pszErrorFile);
debuginfo->fp = NULL;
debuginfo->debug_mode = MS_DEBUGMODE_WINDOWSDEBUG;
#else
msSetError(MS_MISCERR, "'MS_ERRORFILE windowsdebug' is available only on Windows platforms.", "msSetErrorFile()");
return MS_FAILURE;
#endif
} else {
debuginfo->fp = fopen(pszErrorFile, "a");
if (debuginfo->fp == NULL) {
msSetError(MS_MISCERR, "Failed to open MS_ERRORFILE %s", "msSetErrorFile()", pszErrorFile);
return MS_FAILURE;
}
debuginfo->errorfile = msStrdup(pszErrorFile);
debuginfo->debug_mode = MS_DEBUGMODE_FILE;
}
return MS_SUCCESS;
}
/* msCloseErrorFile()
**
** Close current output file (if one is open) and reset related members
*/
void msCloseErrorFile()
{
debugInfoObj *debuginfo = msGetDebugInfoObj();
if (debuginfo && debuginfo->debug_mode != MS_DEBUGMODE_OFF) {
if (debuginfo->fp && debuginfo->debug_mode == MS_DEBUGMODE_FILE)
fclose(debuginfo->fp);
if (debuginfo->fp && (debuginfo->debug_mode == MS_DEBUGMODE_STDERR ||
debuginfo->debug_mode == MS_DEBUGMODE_STDOUT))
fflush(debuginfo->fp); /* just flush stderr or stdout */
debuginfo->fp = NULL;
msFree(debuginfo->errorfile);
debuginfo->errorfile = NULL;
debuginfo->debug_mode = MS_DEBUGMODE_OFF;
}
}
/* msGetErrorFile()
**
** Returns name of current error file
**
** Returns NULL if not set.
*/
const char *msGetErrorFile()
{
debugInfoObj *debuginfo = msGetDebugInfoObj();
if (debuginfo)
return debuginfo->errorfile;
return NULL;
}
/* Set/Get the current global debug level value, used as default value for
** new map and layer objects and to control msDebug() calls outside of
** the context of mapObj or layerObj.
**
*/
void msSetGlobalDebugLevel(int level)
{
debugInfoObj *debuginfo = msGetDebugInfoObj();
if (debuginfo)
debuginfo->global_debug_level = (debugLevel)level;
}
debugLevel msGetGlobalDebugLevel()
{
debugInfoObj *debuginfo = msGetDebugInfoObj();
if (debuginfo)
return debuginfo->global_debug_level;
return MS_DEBUGLEVEL_ERRORSONLY;
}
/* msDebugInitFromEnv()
**
** Init debug state from MS_ERRORFILE and MS_DEBUGLEVEL env vars if set
**
** Returns MS_SUCCESS/MS_FAILURE
*/
int msDebugInitFromEnv()
{
const char *val;
if( (val=getenv( "MS_ERRORFILE" )) != NULL ) {
if ( msSetErrorFile(val, NULL) != MS_SUCCESS )
return MS_FAILURE;
}
if( (val=getenv( "MS_DEBUGLEVEL" )) != NULL )
msSetGlobalDebugLevel(atoi(val));
return MS_SUCCESS;
}
/* msDebugCleanup()
**
** Called by msCleanup to remove info related to this thread.
*/
void msDebugCleanup()
{
/* make sure file is closed */
msCloseErrorFile();
#ifdef USE_THREAD
{
int thread_id = msGetThreadId();
debugInfoObj *link;
msAcquireLock( TLOCK_DEBUGOBJ );
/* find link for this thread */
for( link = debuginfo_list;
link != NULL && link->thread_id != thread_id
&& link->next != NULL && link->next->thread_id != thread_id;
link = link->next ) {}
if( link->thread_id == thread_id ) {
/* presumably link is at head of list. */
if( debuginfo_list == link )
debuginfo_list = link->next;
free( link );
} else if( link->next != NULL && link->next->thread_id == thread_id ) {
debugInfoObj *next_link = link->next;
link->next = link->next->next;
free( next_link );
}
msReleaseLock( TLOCK_DEBUGOBJ );
}
#endif
}
/* msDebug()
**
** Outputs/logs messages to the MS_ERRORFILE if one is set
** (see msSetErrorFile())
**
*/
void msDebug( const char * pszFormat, ... )
{
va_list args;
debugInfoObj *debuginfo = msGetDebugInfoObj();
if (debuginfo == NULL || debuginfo->debug_mode == MS_DEBUGMODE_OFF)
return; /* Don't waste time here! */
if (debuginfo->fp) {
/* Writing to a stdio file handle */
#if defined(USE_FASTCGI)
/* It seems the FastCGI stuff inserts a timestamp anyways, so */
/* we might as well skip this one if writing to stderr w/ FastCGI. */
if (debuginfo->debug_mode != MS_DEBUGMODE_STDERR)
#endif
{
struct mstimeval tv;
time_t t;
msGettimeofday(&tv, NULL);
t = tv.tv_sec;
msIO_fprintf(debuginfo->fp, "[%s].%ld ",
msStringChop(ctime(&t)), (long)tv.tv_usec);
}
va_start(args, pszFormat);
msIO_vfprintf(debuginfo->fp, pszFormat, args);
va_end(args);
}
#ifdef _WIN32
else if (debuginfo->debug_mode == MS_DEBUGMODE_WINDOWSDEBUG) {
/* Writing to Windows Debug Console */
char szMessage[MESSAGELENGTH];
va_start(args, pszFormat);
vsnprintf( szMessage, MESSAGELENGTH, pszFormat, args );
va_end(args);
szMessage[MESSAGELENGTH-1] = '\0';
OutputDebugStringA(szMessage);
}
#endif
}
/* msDebug2()
**
** Variadic function with no operation
**
*/
void msDebug2( int level, ... )
{
}