diff --git a/job.cpp b/job.cpp index 00334ab..bf1db88 100644 --- a/job.cpp +++ b/job.cpp @@ -186,8 +186,10 @@ int Job::Execute() #ifdef __WIN32__ wxString filename = dirname + wxT("\\") + jobid + wxT("_") + stepid + wxT(".bat"); + wxString errorFile = dirname + wxT("\\") + jobid + wxT("_") + stepid + wxT("_error.txt"); #else wxString filename = dirname + wxT("/") + jobid + wxT("_") + stepid + wxT(".scr"); + wxString errorFile = dirname + wxT("/") + jobid + wxT("_") + stepid + wxT("_error.txt"); #endif // Write the script @@ -233,6 +235,10 @@ int Job::Execute() file->Close(); LogMessage(wxString::Format(_("Executing script file: %s"), filename.c_str()), LOG_DEBUG); + // freopen function is used to redirect output of stream (stderr in our case) + // into the specified file. + FILE *fpError = freopen(errorFile.mb_str(), "w", stderr); + // Execute the file and capture the output #ifdef __WIN32__ // The Windows way @@ -304,6 +310,42 @@ int Job::Execute() if (rc == 0) succeeded = true; + // If output is empty then either script did not return any output + // or script threw some error into stderr. + if (output == wxEmptyString) + { + // Check script threw some error into stderr + if (fpError) + { + fclose(fpError); + fpError = fopen(errorFile.mb_str(), "r"); + if (fpError) + { + char buffer[4098]; + wxString errorMsg = wxEmptyString; + while (!feof(fpError)) + { + if (fgets(buffer, 4096, fpError) != NULL) + errorMsg += wxString(buffer, wxConvLibc); + } + + if (errorMsg != wxEmptyString) + LogMessage(wxString::Format(_("Script Error: \n%s\n"), errorMsg.c_str()), LOG_WARNING); + + fclose(fpError); + } + wxRemoveFile(errorFile); + } + } + else + { + if (fpError) + { + fclose(fpError); + wxRemoveFile(errorFile); + } + } + // Delete the file/directory. If we fail, don't overwrite the script output in the log, just throw warnings. if (!wxRemoveFile(filename)) {