Commit 0bb0ac83 authored by Jan's avatar Jan

Cleanup Windows console startup code

parent 79e76650
......@@ -169,7 +169,9 @@ set( sources
if (WIN32)
LIST(APPEND sources "../Windows/StackWalker/StackWalker.cc")
LIST(APPEND sources "../Windows/windows-startup.cc")
LIST(APPEND headers "../Windows/StackWalker/StackWalker.hh")
LIST(APPEND headers "../Windows/windows-startup.hh")
endif(WIN32)
......
#include "windows-startup.hh"
#include <windows.h>
#include <errhandlingapi.h>
#include <fstream>
#include <OpenFlipper/common/GlobalOptions.hh>
/* ==========================================================
*
* Stackwalker code. Used to get a backtrace if OpenFlipper
* crashes under windows
*
* ==========================================================*/
void StackWalkerToConsole::OnOutput(LPCSTR szText)
{
Writes crash dump to .OpenFlipper config directory
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();
Write crash dump to console as well
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()
{
try to attach the console of the parent process
if (AttachConsole(-1))
{
if the console was attached change stdinput and output
connect_console();
}
else
{
create and attach a new console if needed
#ifndef NDEBUG
always open a console in debug mode
AllocConsole();
connect_console();
return;
#endif
if (OpenFlipper::Options::logToConsole())
{
AllocConsole();
connect_console();
}
}
}
#ifdef WIN32
# include <windows.h>
# include <errhandlingapi.h>
#include "StackWalker/StackWalker.hh"
/* ==========================================================
*
......@@ -11,25 +8,12 @@
*
* ==========================================================*/
#include "StackWalker/StackWalker.hh"
#include <fstream>
class StackWalkerToConsole : public StackWalker
{
protected:
virtual void OnOutput(LPCSTR szText) override
{
// Writes crash dump to .OpenFlipper config directory
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();
// Write crash dump to console as well
StackWalker::OnOutput(szText);
}
};
class StackWalkerToConsole : public StackWalker
{
protected:
virtual void OnOutput(LPCSTR szText) override;
};
/* ==========================================================
......@@ -39,52 +23,5 @@
*
* ==========================================================*/
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()
{
//try to attach the console of the parent process
if (AttachConsole(-1))
{
//if the console was attached change stdinput and output
connect_console();
}
else
{
//create and attach a new console if needed
#ifndef NDEBUG
//always open a console in debug mode
AllocConsole();
connect_console();
return;
#endif
if (OpenFlipper::Options::logToConsole())
{
AllocConsole();
connect_console();
}
}
}
#endif
void connect_console();
void attachConsole();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment