Developer Documentation
Loading...
Searching...
No Matches
windows-startup.cc
1
2#include "windows-startup.hh"
3#include <windows.h>
4#include <errhandlingapi.h>
5#include <fstream>
6#include <iostream>
8
9/* ==========================================================
10*
11* Stackwalker code. Used to get a backtrace if OpenFlipper
12* crashes under windows
13*
14* ==========================================================*/
15
16void StackWalkerToConsole::OnOutput(LPCSTR szText)
17{
18 // Writes crash dump to .OpenFlipper config directory
19 std::ofstream crashFile;
20 QString crashName = OpenFlipper::Options::configDirStr() + QDir::separator() + "CrashDump.txt";
21 crashFile.open(crashName.toLatin1(),std::ios::out | std::ios::app);
22 crashFile << szText;
23 crashFile.close();
24
25 // Write crash dump to console as well
26 StackWalker::OnOutput(szText);
27}
28
29
30
31/* ==========================================================
32 *
33 * Console for Windows to get additional output written via
34 * cerr, cout, ... that is not forwarded to log window
35 *
36 * ==========================================================*/
37
38void connect_console()
39{
40 FILE* check = freopen("CONIN$", "r", stdin);
41 if (check) {
42 std::cerr << "Error reopening stdin" << std::endl;
43 }
44 check = freopen("CONOUT$", "w", stdout);
45 if (check) {
46 std::cerr << "Error reopening stdout" << std::endl;
47 }
48 check = freopen("CONOUT$", "w", stderr);
49 if (check) {
50 std::cerr << "Error reopening stderr" << std::endl;
51 }
52 std::cout.clear();
53 std::cerr.clear();
54 std::cin.clear();
55 std::wcout.clear();
56 std::wcerr.clear();
57 std::wcin.clear();
58}
59
60 void attachConsole()
61 {
62 // try to attach the console of the parent process
63 if (AttachConsole(-1))
64 {
65 // if the console was attached change stdinput and output
66 connect_console();
67 }
68 else
69 {
70 // create and attach a new console if needed
71 #ifndef NDEBUG
72 // always open a console in debug mode
73 AllocConsole();
74 connect_console();
75
76 return;
77 #endif
78 if (OpenFlipper::Options::logToConsole())
79 {
80 AllocConsole();
81 connect_console();
82 }
83 }
84 }
85
86