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 */
| 136 | ** Returns MS_SUCCESS/MS_FAILURE |
| 137 | */ |
| 138 | int msSetErrorFile(const char *pszErrorFile, const char *pszRelToPath) |
| 139 | { |
| 140 | char extended_path[MS_MAXPATHLEN]; |
| 141 | debugInfoObj *debuginfo = msGetDebugInfoObj(); |
| 142 | |
| 143 | if (strcmp(pszErrorFile, "stderr") != 0 && |
| 144 | strcmp(pszErrorFile, "stdout") != 0 && |
| 145 | strcmp(pszErrorFile, "windowsdebug") != 0) |
| 146 | { |
| 147 | /* Try to make the path relative */ |
| 148 | if(msBuildPath(extended_path, pszRelToPath, pszErrorFile) == NULL) |
| 149 | return MS_FAILURE; |
| 150 | pszErrorFile = extended_path; |
| 151 | } |
| 152 | |
| 153 | if (debuginfo && debuginfo->errorfile && pszErrorFile && |
| 154 | strcmp(debuginfo->errorfile, pszErrorFile) == 0) |
| 155 | { |
| 156 | /* Nothing to do, already writing to the right place */ |
| 157 | return MS_SUCCESS; |
| 158 | } |
| 159 | |
| 160 | /* Close current output file if any */ |
| 161 | msCloseErrorFile(); |
| 162 | |
| 163 | /* NULL or empty target will just close current output and return */ |
| 164 | if (pszErrorFile == NULL || *pszErrorFile == '\0') |
| 165 | return MS_SUCCESS; |
| 166 | |
| 167 | if (strcmp(pszErrorFile, "stderr") == 0) |
| 168 | { |
| 169 | debuginfo->fp = stderr; |
| 170 | debuginfo->errorfile = msStrdup(pszErrorFile); |
| 171 | debuginfo->debug_mode = MS_DEBUGMODE_STDERR; |
| 172 | #if defined(NEED_NONBLOCKING_STDERR) && !defined(USE_MAPIO) && !defined(_WIN32) |
| 173 | fcntl(fileno(stderr), F_SETFL, O_NONBLOCK); |
| 174 | nonblocking_set = 1; |
| 175 | #endif |
| 176 | } |
| 177 | else if (strcmp(pszErrorFile, "stdout") == 0) |
| 178 | { |
| 179 | debuginfo->fp = stdout; |
| 180 | debuginfo->errorfile = msStrdup(pszErrorFile); |
| 181 | debuginfo->debug_mode = MS_DEBUGMODE_STDOUT; |
| 182 | } |
| 183 | else if (strcmp(pszErrorFile, "windowsdebug") == 0) |
| 184 | { |
| 185 | #ifdef _WIN32 |
| 186 | debuginfo->errorfile = msStrdup(pszErrorFile); |
| 187 | debuginfo->fp = NULL; |
| 188 | debuginfo->debug_mode = MS_DEBUGMODE_WINDOWSDEBUG; |
| 189 | #else |
| 190 | msSetError(MS_MISCERR, "'MS_ERRORFILE windowsdebug' is available only on Windows platforms.", "msSetErrorFile()"); |
| 191 | return MS_FAILURE; |
| 192 | #endif |
| 193 | } |
| 194 | else |
| 195 | { |
no test coverage detected