/* 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; }
/* 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; }
/* 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; }
/* 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 }
/* 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; } }