Advertisement
Guest User

xc

a guest
Feb 26th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.         AllocConsole();
  3.  
  4.         char* path = toChar(System::AppDomain::CurrentDomain->RelativeSearchPath + "\\agfs_stderr.log");
  5.         FILE* file;
  6.         HANDLE hLogFile;
  7.         hLogFile = CreateFileA(path, GENERIC_WRITE | GENERIC_READ,
  8.             FILE_SHARE_READ, NULL, CREATE_ALWAYS,
  9.             FILE_ATTRIBUTE_NORMAL, NULL);
  10.  
  11.         bool isStdErrorHandleSet = SetStdHandle(STD_ERROR_HANDLE, hLogFile);
  12.         bool isStdOutputHandleSet = SetStdHandle(STD_OUTPUT_HANDLE, hLogFile);
  13.  
  14.         if (isStdErrorHandleSet && isStdOutputHandleSet && hLogFile != INVALID_HANDLE_VALUE)
  15.         {
  16.             int fileDescriptor = _open_osfhandle((intptr_t)hLogFile, _O_TEXT);
  17.             if (fileDescriptor != -1)
  18.             {
  19.                 file = _fdopen(fileDescriptor, "w");
  20.  
  21.                 if (file != NULL)
  22.                 {
  23.                     FILE* notused, *notused2;
  24.                     freopen_s(&notused, "CONOUT$", "w", stderr);
  25.                     freopen_s(&notused2, "CONOUT$", "w", stdout);
  26.                     fflush(stderr);
  27.                     fflush(stdout);
  28.  
  29.                     if (_dup2(_fileno(file), _fileno(stderr)) == 0)
  30.                     {
  31.                         setvbuf(stderr, NULL, _IONBF, 0);
  32.                     }
  33.  
  34.                     if (_dup2(_fileno(file), _fileno(stdout)) == 0)
  35.                     {
  36.                         setvbuf(stdout, NULL, _IONBF, 0);
  37.                     }
  38.                 }
  39.             }
  40.         }
  41.  
  42.         std::ios::sync_with_stdio();
  43.  
  44.         fprintf(stderr, "Redirecting stderr into file succesfully!\n");
  45.         printf("Redirecting stdout into file succesfully!\n");
  46.         //Set error and abort behavior
  47.         //int winapi_prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
  48.        
  49.         //Set new handler to signal abort
  50.         //void(__cdecl* prev_abort_handler)(int) =
  51.  
  52.         for (int i = 0; i < NSIG; i++)
  53.         {
  54.             signal(i, signal_handler);
  55.         }
  56.  
  57.         /* Windows GPF errors */
  58.         /*int winapi_prev_error_mode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX |
  59.             SEM_NOOPENFILEERRORBOX);*/
  60.  
  61.         /* Runtime library */
  62.         //int prev_error_mode = _set_error_mode(_OUT_TO_STDERR);
  63.  
  64.         //Disable abort or Dr. Watson
  65.         //unsigned int prev_abort_behavior = _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT);
  66.         //SetUnhandledExceptionFilter(unhandled_exception_handler);
  67.  
  68.         try
  69.         {
  70.             status = ro->agfm_simulate(
  71.                 &context,
  72.                 site_def,
  73.                 ro->inputData->Count,
  74.                 input,
  75.                 output,
  76.                 warning_fun,
  77.                 sizeof(struct simulation_input_record),
  78.                 sizeof(struct simulation_output_record),
  79.                 log_fun,
  80.                 ro->unitStateInfos->Count,
  81.                 unitStateInfos,
  82.                 sizeof(struct unit_state_info),
  83.                 pipe_connection_fun);
  84.         }
  85.         catch(int i)
  86.         {
  87.             fprintf(stderr, toChar("Signal " + i + " was caught at " + System::DateTime::Now));
  88.             status = -1;
  89.         }
  90.  
  91.         //fclose(fp);
  92.         CloseHandle(hLogFile);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement