void CPL_DLL CPL_STDCALL CPLLoggingHandler( CPLErr, int, const char * );//头文件中,作为全局函数 CPLSetErrorHandler(CPLLoggingHandler);//在刚开始的GDAL调用时 //自定义的函数 void CPL_STDCALL CPLLoggingHandler( CPLErr eErrClass, int nError, const char * pszErrorMsg ) { static int bLogInit = FALSE; static FILE * fpLog = stderr; if( !bLogInit ) { const char *cpl_log = NULL; CPLSetConfigOption( "CPL_TIMESTAMP", "ON" ); bLogInit = TRUE; cpl_log = CPLGetConfigOption("CPL_LOG", NULL ); cpl_log = "d://testcpp.txt";//输出位置 fpLog = stderr; if( cpl_log != NULL && EQUAL(cpl_log,"OFF") ) { fpLog = NULL; } else if( cpl_log != NULL ) { char path[5000]; int i = 0; strncpy( path, cpl_log, sizeof(path) - 10 ); path[sizeof(path)-1] = '/0'; while( (fpLog = fopen( path, "rt" )) != NULL ) { fclose( fpLog ); /* generate sequenced log file names, inserting # before ext.*/ if (strrchr(cpl_log, '.') == NULL) { sprintf( path, "%s_%d%s", cpl_log, i++, ".log" ); } else { size_t pos = 0; char *cpl_log_base = strdup(cpl_log); pos = strcspn(cpl_log_base, "."); if (pos > 0) { cpl_log_base[pos] = '/0'; } sprintf( path, "%s_%d%s", cpl_log_base, i++, ".log" ); } } fpLog = fopen( path, "wt" ); } } if( fpLog == NULL ) return; if( eErrClass == CE_Debug ) fprintf( fpLog, "%s/n", pszErrorMsg ); else if( eErrClass == CE_Warning ) fprintf( fpLog, "Warning %d: %s/n", nError, pszErrorMsg ); else fprintf( fpLog, "ERROR %d: %s/n", nError, pszErrorMsg ); fflush( fpLog ); }