windows-startup.cc 2.09 KB
Newer Older
Jan's avatar
Jan committed
1
2
3
4
5

#include "windows-startup.hh"
#include <windows.h>
#include <errhandlingapi.h>
#include <fstream>
Jan's avatar
Jan committed
6
#include <iostream>
Jan's avatar
Jan committed
7
8
9
10
11
12
13
14
15
16
17
#include <OpenFlipper/common/GlobalOptions.hh>

/* ==========================================================
*
* Stackwalker code. Used to get a backtrace if OpenFlipper
* crashes under windows
*
* ==========================================================*/

void StackWalkerToConsole::OnOutput(LPCSTR szText) 
{
Jan's avatar
Jan committed
18
    // Writes crash dump to .OpenFlipper config directory
Jan's avatar
Jan committed
19
20
21
22
23
24
    std::ofstream crashFile;
    QString crashName = OpenFlipper::Options::configDirStr() + QDir::separator() + "CrashDump.txt";
    crashFile.open(crashName.toLatin1(),std::ios::out | std::ios::app);
    crashFile << szText;
    crashFile.close();

Jan's avatar
Jan committed
25
    // Write crash dump to console as well
Jan's avatar
Jan committed
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    StackWalker::OnOutput(szText);
}



/* ==========================================================
 *
 * Console for Windows to get additional output written via
 * cerr, cout, ... that is not forwarded to log window
 *
 * ==========================================================*/

void connect_console()
{
  FILE* check = freopen("CONIN$", "r", stdin);
  if (check) {
    std::cerr << "Error reopening stdin" << std::endl;
  }
  check = freopen("CONOUT$", "w", stdout);
  if (check) {
    std::cerr << "Error reopening stdout" << std::endl;
  }
  check = freopen("CONOUT$", "w", stderr);
  if (check) {
    std::cerr << "Error reopening stderr" << std::endl;
  }
  std::cout.clear();
  std::cerr.clear();
  std::cin.clear();
  std::wcout.clear();
  std::wcerr.clear();
  std::wcin.clear();
}

  void attachConsole()
   {
Jan's avatar
Jan committed
62
     // try to attach the console of the parent process
Jan's avatar
Jan committed
63
64
     if (AttachConsole(-1))
     {
Jan's avatar
Jan committed
65
       // if the console was attached change stdinput and output
Jan's avatar
Jan committed
66
67
68
69
       connect_console();
     }
     else
     {
Jan's avatar
Jan committed
70
       // create and attach a new console if needed
Jan's avatar
Jan committed
71
 #ifndef NDEBUG
Jan's avatar
Jan committed
72
       // always open a console in debug mode
Jan's avatar
Jan committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
       AllocConsole();     
       connect_console();

       return;
 #endif
       if (OpenFlipper::Options::logToConsole())
       {
         AllocConsole();
         connect_console();
       }
     }
   }