Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AllocConsole();
- char* path = toChar(System::AppDomain::CurrentDomain->RelativeSearchPath + "\\agfs_stderr.log");
- FILE* file;
- HANDLE hLogFile;
- hLogFile = CreateFileA(path, GENERIC_WRITE | GENERIC_READ,
- FILE_SHARE_READ, NULL, CREATE_ALWAYS,
- FILE_ATTRIBUTE_NORMAL, NULL);
- bool isStdErrorHandleSet = SetStdHandle(STD_ERROR_HANDLE, hLogFile);
- bool isStdOutputHandleSet = SetStdHandle(STD_OUTPUT_HANDLE, hLogFile);
- if (isStdErrorHandleSet && isStdOutputHandleSet && hLogFile != INVALID_HANDLE_VALUE)
- {
- int fileDescriptor = _open_osfhandle((intptr_t)hLogFile, _O_TEXT);
- if (fileDescriptor != -1)
- {
- file = _fdopen(fileDescriptor, "w");
- if (file != NULL)
- {
- FILE* notused, *notused2;
- freopen_s(¬used, "CONOUT$", "w", stderr);
- freopen_s(¬used2, "CONOUT$", "w", stdout);
- fflush(stderr);
- fflush(stdout);
- if (_dup2(_fileno(file), _fileno(stderr)) == 0)
- {
- setvbuf(stderr, NULL, _IONBF, 0);
- }
- if (_dup2(_fileno(file), _fileno(stdout)) == 0)
- {
- setvbuf(stdout, NULL, _IONBF, 0);
- }
- }
- }
- }
- std::ios::sync_with_stdio();
- fprintf(stderr, "Redirecting stderr into file succesfully!\n");
- printf("Redirecting stdout into file succesfully!\n");
- //Set error and abort behavior
- //int winapi_prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
- //Set new handler to signal abort
- //void(__cdecl* prev_abort_handler)(int) =
- for (int i = 0; i < NSIG; i++)
- {
- signal(i, signal_handler);
- }
- /* Windows GPF errors */
- /*int winapi_prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
- SEM_NOOPENFILEERRORBOX);*/
- /* Runtime library */
- //int prev_error_mode = _set_error_mode(_OUT_TO_STDERR);
- //Disable abort or Dr. Watson
- //unsigned int prev_abort_behavior = _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
- //SetUnhandledExceptionFilter(unhandled_exception_handler);
- try
- {
- status = ro->agfm_simulate(
- &context,
- site_def,
- ro->inputData->Count,
- input,
- output,
- warning_fun,
- sizeof(struct simulation_input_record),
- sizeof(struct simulation_output_record),
- log_fun,
- ro->unitStateInfos->Count,
- unitStateInfos,
- sizeof(struct unit_state_info),
- pipe_connection_fun);
- }
- catch(int i)
- {
- fprintf(stderr, toChar("Signal " + i + " was caught at " + System::DateTime::Now));
- status = -1;
- }
- //fclose(fp);
- CloseHandle(hLogFile);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement