forked from sdlime/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maperror.c
622 lines (544 loc) · 17.5 KB
/
maperror.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Implementation of msSetError(), msDebug() and related functions.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 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>
static char *ms_errorCodes[MS_NUMERRORCODES] = {"",
"Unable to access file.",
"Memory allocation error.",
"Incorrect data type.",
"Symbol definition error.",
"Regular expression error.",
"TrueType Font error.",
"DBASE file error.",
"GD library error.",
"Unknown identifier.",
"Premature End-of-File.",
"Projection library error.",
"General error message.",
"CGI error.",
"Web application error.",
"Image handling error.",
"Hash table error.",
"Join error.",
"Search returned no results.",
"Shapefile error.",
"Expression parser error.",
"SDE error.",
"OGR error.",
"Query error.",
"WMS server error.",
"WMS connection error.",
"OracleSpatial error.",
"WFS server error.",
"WFS connection error.",
"WMS Map Context error.",
"HTTP request error.",
"Child array error.",
"WCS server error.",
"GEOS library error.",
"Invalid rectangle.",
"Date/time error.",
"GML encoding error.",
"SOS server error.",
"NULL parent pointer error.",
"AGG library error.",
"OWS error.",
"OpenGL renderer error.",
"Renderer error.",
"V8 engine error."
};
#ifndef USE_THREAD
errorObj *msGetErrorObj()
{
static errorObj ms_error = {MS_NOERR, "", "", MS_FALSE, 0, NULL};
return &ms_error;
}
#endif
#ifdef USE_THREAD
typedef struct te_info {
struct te_info *next;
void* thread_id;
errorObj ms_error;
} te_info_t;
static te_info_t *error_list = NULL;
errorObj *msGetErrorObj()
{
te_info_t *link;
void* thread_id;
errorObj *ret_obj;
msAcquireLock( TLOCK_ERROROBJ );
thread_id = msGetThreadId();
/* find link for this thread */
for( link = error_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( error_list != NULL && error_list->thread_id == thread_id ) {
}
/* We don't have one ... initialize one. */
else if( link == NULL || link->next == NULL ) {
te_info_t *new_link;
errorObj error_obj = { MS_NOERR, "", "", 0 };
new_link = (te_info_t *) malloc(sizeof(te_info_t));
new_link->next = error_list;
new_link->thread_id = thread_id;
new_link->ms_error = error_obj;
error_list = new_link;
}
/* If the link is not already at the head of the list, promote it */
else if( link != NULL && link->next != NULL ) {
te_info_t *target = link->next;
link->next = link->next->next;
target->next = error_list;
error_list = target;
}
ret_obj = &(error_list->ms_error);
msReleaseLock( TLOCK_ERROROBJ );
return ret_obj;
}
#endif
/* msInsertErrorObj()
**
** We maintain a chained list of errorObj in which the first errorObj is
** the most recent (i.e. a stack). msErrorReset() should be used to clear
** the list.
**
** Note that since some code in MapServer will fetch the head of the list and
** keep a handle on it for a while, the head of the chained list is static
** and never changes.
** A new errorObj is always inserted after the head, and only if the
** head of the list already contains some information. i.e. If the static
** errorObj at the head of the list is empty then it is returned directly,
** otherwise a new object is inserted after the head and the data that was in
** the head is moved to the new errorObj, freeing the head errorObj to receive
** the new error information.
*/
static errorObj *msInsertErrorObj(void)
{
errorObj *ms_error;
ms_error = msGetErrorObj();
if (ms_error->code != MS_NOERR) {
/* Head of the list already in use, insert a new errorObj after the head
* and move head contents to this new errorObj, freeing the errorObj
* for reuse.
*/
errorObj *new_error;
new_error = (errorObj *)malloc(sizeof(errorObj));
/* Note: if malloc() failed then we simply do nothing and the head will
* be overwritten by the caller... we cannot produce an error here
* since we are already inside a msSetError() call.
*/
if (new_error) {
new_error->next = ms_error->next;
new_error->code = ms_error->code;
new_error->isreported = ms_error->isreported;
strlcpy(new_error->routine, ms_error->routine, sizeof(new_error->routine));
strlcpy(new_error->message, ms_error->message, sizeof(new_error->message));
new_error->errorcount = ms_error->errorcount;
ms_error->next = new_error;
ms_error->code = MS_NOERR;
ms_error->isreported = MS_FALSE;
ms_error->routine[0] = '\0';
ms_error->message[0] = '\0';
ms_error->errorcount = 0;
}
}
return ms_error;
}
/* msResetErrorList()
**
** Clear the list of error objects.
*/
void msResetErrorList()
{
errorObj *ms_error, *this_error;
ms_error = msGetErrorObj();
this_error = ms_error->next;
while( this_error != NULL) {
errorObj *next_error;
next_error = this_error->next;
msFree(this_error);
this_error = next_error;
}
ms_error->next = NULL;
ms_error->code = MS_NOERR;
ms_error->routine[0] = '\0';
ms_error->message[0] = '\0';
ms_error->errorcount = 0;
/* -------------------------------------------------------------------- */
/* Cleanup our entry in the thread list. This is mainly */
/* imprortant when msCleanup() calls msResetErrorList(). */
/* -------------------------------------------------------------------- */
#ifdef USE_THREAD
{
void* thread_id = msGetThreadId();
te_info_t *link;
msAcquireLock( TLOCK_ERROROBJ );
/* find link for this thread */
for( link = error_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( error_list == link )
error_list = link->next;
free( link );
} else if( link->next != NULL && link->next->thread_id == thread_id ) {
te_info_t *next_link = link->next;
link->next = link->next->next;
free( next_link );
}
msReleaseLock( TLOCK_ERROROBJ );
}
#endif
}
char *msGetErrorCodeString(int code)
{
if(code<0 || code>MS_NUMERRORCODES-1)
return("Invalid error code.");
return(ms_errorCodes[code]);
}
/* -------------------------------------------------------------------- */
/* Adding the displayable error string to a given string */
/* and reallocates the memory enough to hold the characters. */
/* If source is null returns a newly allocated string */
/* -------------------------------------------------------------------- */
char *msAddErrorDisplayString(char *source, errorObj *error)
{
if((source = msStringConcatenate(source, error->routine)) == NULL) return(NULL);
if((source = msStringConcatenate(source, ": ")) == NULL) return(NULL);
if((source = msStringConcatenate(source, ms_errorCodes[error->code])) == NULL) return(NULL);
if((source = msStringConcatenate(source, " ")) == NULL) return(NULL);
if((source = msStringConcatenate(source, error->message)) == NULL) return(NULL);
if (error->errorcount > 0) {
char* pszTmp;
if((source = msStringConcatenate(source, " (message repeated ")) == NULL) return(NULL);
pszTmp = msIntToString(error->errorcount);
if((source = msStringConcatenate(source, pszTmp)) == NULL) {
msFree(pszTmp);
return(NULL);
}
msFree(pszTmp);
if((source = msStringConcatenate(source, " times)")) == NULL) return(NULL);
}
return source;
}
char *msGetErrorString(char *delimiter)
{
char *errstr=NULL;
errorObj *error = msGetErrorObj();
if(!delimiter || !error) return(NULL);
while(error && error->code != MS_NOERR) {
if((errstr = msAddErrorDisplayString(errstr, error)) == NULL) return(NULL);
if(error->next && error->next->code != MS_NOERR) { /* (peek ahead) more errors, use delimiter */
if((errstr = msStringConcatenate(errstr, delimiter)) == NULL) return(NULL);
}
error = error->next;
}
return(errstr);
}
void msSetError(int code, const char *message_fmt, const char *routine, ...)
{
errorObj *ms_error;
va_list args;
char message[MESSAGELENGTH];
if(!message_fmt)
strcpy(message, "");
else {
va_start(args, routine);
vsnprintf( message, MESSAGELENGTH, message_fmt, args );
va_end(args);
}
ms_error = msGetErrorObj();
/* Insert the error to the list if it is not the same as the previous error*/
if (ms_error->code != code || !EQUAL(message, ms_error->message) ||
!EQUAL(routine, ms_error->routine)) {
ms_error = msInsertErrorObj();
if(!routine)
strcpy(ms_error->routine, "");
else {
strlcpy(ms_error->routine, routine, sizeof(ms_error->routine));
}
strlcpy(ms_error->message, message, sizeof(ms_error->message));
ms_error->code = code;
ms_error->errorcount = 0;
}
else
++ms_error->errorcount;
/* Log a copy of errors to MS_ERRORFILE if set (handled automatically inside msDebug()) */
msDebug("%s: %s %s\n", ms_error->routine, ms_errorCodes[ms_error->code], ms_error->message);
}
void msWriteError(FILE *stream)
{
errorObj *ms_error = msGetErrorObj();
while (ms_error && ms_error->code != MS_NOERR) {
msIO_fprintf(stream, "%s: %s %s <br>\n", ms_error->routine, ms_errorCodes[ms_error->code], ms_error->message);
ms_error->isreported = MS_TRUE;
ms_error = ms_error->next;
}
}
void msWriteErrorXML(FILE *stream)
{
char *message;
errorObj *ms_error = msGetErrorObj();
while (ms_error && ms_error->code != MS_NOERR) {
message = msEncodeHTMLEntities(ms_error->message);
msIO_fprintf(stream, "%s: %s %s\n", ms_error->routine,
ms_errorCodes[ms_error->code], message);
ms_error->isreported = MS_TRUE;
ms_error = ms_error->next;
msFree(message);
}
}
void msWriteErrorImage(mapObj *map, char *filename, int blank)
{
imageObj *img;
int width=400, height=300;
int nMargin =5;
int nTextLength = 0;
int nUsableWidth = 0;
int nMaxCharsPerLine = 0;
int nLines = 0;
int i = 0;
int nStart = 0;
int nEnd = 0;
int nLength = 0;
char **papszLines = NULL;
pointObj pnt;
int nWidthTxt = 0;
outputFormatObj *format = NULL;
char *errormsg = msGetErrorString("; ");
errorObj *error = msGetErrorObj();
char *imagepath = NULL, *imageurl = NULL;
colorObj imagecolor, *imagecolorptr=NULL;
textSymbolObj ts;
labelObj label;
int charWidth = 5, charHeight = 8; /* hardcoded, should be looked up from ft face */
if(!errormsg) {
errormsg = msStrdup("No error found sorry. This is likely a bug");
}
if (map) {
if( map->width > 0 && map->height > 0 ) {
width = map->width;
height = map->height;
}
format = map->outputformat;
imagepath = map->web.imagepath;
imageurl = map->web.imageurl;
}
/* Default to GIF if no suitable GD output format set */
if (format == NULL || !MS_RENDERER_PLUGIN(format))
format = msCreateDefaultOutputFormat( NULL, "AGG/PNG8", "png" );
if(!format->transparent) {
if(map && MS_VALID_COLOR(map->imagecolor)) {
imagecolorptr = &map->imagecolor;
} else {
MS_INIT_COLOR(imagecolor,255,255,255,255);
imagecolorptr = &imagecolor;
}
}
img = msImageCreate(width,height,format,imagepath,imageurl,MS_DEFAULT_RESOLUTION,MS_DEFAULT_RESOLUTION,imagecolorptr);
nTextLength = strlen(errormsg);
nWidthTxt = nTextLength * charWidth;
nUsableWidth = width - (nMargin*2);
/* Check to see if it all fits on one line. If not, split the text on several lines. */
if(!blank) {
if (nWidthTxt > nUsableWidth) {
nMaxCharsPerLine = nUsableWidth/charWidth;
nLines = (int) ceil ((double)nTextLength / (double)nMaxCharsPerLine);
if (nLines > 0) {
papszLines = (char **)malloc(nLines*sizeof(char *));
for (i=0; i<nLines; i++) {
papszLines[i] = (char *)malloc((nMaxCharsPerLine+1)*sizeof(char));
papszLines[i][0] = '\0';
}
}
for (i=0; i<nLines; i++) {
nStart = i*nMaxCharsPerLine;
nEnd = nStart + nMaxCharsPerLine;
if (nStart < nTextLength) {
if (nEnd > nTextLength)
nEnd = nTextLength;
nLength = nEnd-nStart;
strncpy(papszLines[i], errormsg+nStart, nLength);
papszLines[i][nLength] = '\0';
}
}
} else {
nLines = 1;
papszLines = (char **)malloc(nLines*sizeof(char *));
papszLines[0] = msStrdup(errormsg);
}
initLabel(&label);
MS_INIT_COLOR(label.color,0,0,0,255);
MS_INIT_COLOR(label.outlinecolor,255,255,255,255);
label.outlinewidth = 1;
label.size = MS_SMALL;
MS_REFCNT_INCR((&label));
for (i=0; i<nLines; i++) {
pnt.y = charHeight * ((i*2) +1);
pnt.x = charWidth;
initTextSymbol(&ts);
msPopulateTextSymbolForLabelAndString(&ts,&label,papszLines[i],1,1,0);
if(LIKELY(MS_SUCCESS == msComputeTextPath(map,&ts))) {
if(MS_SUCCESS!=msDrawTextSymbol(NULL,img,pnt,&ts)) {
/* an error occured, but there's nothing much we can do about it here as we are already handling an error condition */
}
freeTextSymbol(&ts);
}
}
if (papszLines) {
free(papszLines);
}
}
/* actually write the image */
if(!filename) {
msIO_setHeader("Content-Type","%s", MS_IMAGE_MIME_TYPE(format));
msIO_sendHeaders();
}
msSaveImage(NULL,img,filename);
msFreeImage(img);
/* the errors are reported */
while(error && error->code != MS_NOERR) {
error->isreported = MS_TRUE;
error = error->next;
}
if (format->refcount == 0)
msFreeOutputFormat(format);
msFree(errormsg);
}
char *msGetVersion()
{
static char version[1024];
sprintf(version, "MapServer version %s", MS_VERSION);
#if (defined USE_PNG)
strcat(version, " OUTPUT=PNG");
#endif
#if (defined USE_JPEG)
strcat(version, " OUTPUT=JPEG");
#endif
#ifdef USE_KML
strcat(version, " OUTPUT=KML");
#endif
#ifdef USE_PROJ
strcat(version, " SUPPORTS=PROJ");
#endif
strcat(version, " SUPPORTS=AGG");
strcat(version, " SUPPORTS=FREETYPE");
#ifdef USE_CAIRO
strcat(version, " SUPPORTS=CAIRO");
#endif
#if defined(USE_SVG_CAIRO) || defined(USE_RSVG)
strcat(version, " SUPPORTS=SVG_SYMBOLS");
#ifdef USE_SVG_CAIRO
strcat(version, " SUPPORTS=SVGCAIRO");
#else
strcat(version, " SUPPORTS=RSVG");
#endif
#endif
#ifdef USE_OGL
strcat(version, " SUPPORTS=OPENGL");
#endif
#ifdef USE_ICONV
strcat(version, " SUPPORTS=ICONV");
#endif
#ifdef USE_EXEMPI
strcat(version, " SUPPORTS=XMP");
#endif
#ifdef USE_FRIBIDI
strcat(version, " SUPPORTS=FRIBIDI");
#endif
#ifdef USE_WMS_SVR
strcat(version, " SUPPORTS=WMS_SERVER");
#endif
#ifdef USE_WMS_LYR
strcat(version, " SUPPORTS=WMS_CLIENT");
#endif
#ifdef USE_WFS_SVR
strcat(version, " SUPPORTS=WFS_SERVER");
#endif
#ifdef USE_WFS_LYR
strcat(version, " SUPPORTS=WFS_CLIENT");
#endif
#ifdef USE_WCS_SVR
strcat(version, " SUPPORTS=WCS_SERVER");
#endif
#ifdef USE_SOS_SVR
strcat(version, " SUPPORTS=SOS_SERVER");
#endif
#ifdef USE_FASTCGI
strcat(version, " SUPPORTS=FASTCGI");
#endif
#ifdef USE_THREAD
strcat(version, " SUPPORTS=THREADS");
#endif
#ifdef USE_GEOS
strcat(version, " SUPPORTS=GEOS");
#endif
#ifdef USE_POINT_Z_M
strcat(version, " SUPPORTS=POINT_Z_M");
#endif
#ifdef USE_V8_MAPSCRIPT
strcat(version, " SUPPORTS=V8");
#endif
#ifdef USE_JPEG
strcat(version, " INPUT=JPEG");
#endif
#ifdef USE_SDE
strcat(version, " INPUT=SDE");
#endif
#ifdef USE_POSTGIS
strcat(version, " INPUT=POSTGIS");
#endif
#ifdef USE_ORACLESPATIAL
strcat(version, " INPUT=ORACLESPATIAL");
#endif
#ifdef USE_OGR
strcat(version, " INPUT=OGR");
#endif
#ifdef USE_GDAL
strcat(version, " INPUT=GDAL");
#endif
strcat(version, " INPUT=SHAPEFILE");
return(version);
}
int msGetVersionInt()
{
return MS_VERSION_NUM;
}