move ConsoleWriterFactory implementation in util.cpp

This commit is contained in:
Volker Fischer 2019-04-12 18:24:35 +02:00
parent feaea570f2
commit 3348056253
3 changed files with 42 additions and 42 deletions

View file

@ -31,45 +31,16 @@
#include "serverdlg.h"
#include "settings.h"
#include "testbench.h"
#include "util.h"
/******************************************************************************\
* Console logging *
\******************************************************************************/
// Try for a portable console --------------------------------------------------
QTextStream* ConsoleWriterFactory::get()
{
if ( ptsConsole == nullptr )
{
#if _WIN32
if ( !AttachConsole ( ATTACH_PARENT_PROCESS ) )
{
// Not run from console, dump logging to nowhere
static QString conout;
ptsConsole = new QTextStream ( &conout );
}
else
{
freopen ( "CONOUT$", "w", stdout );
ptsConsole = new QTextStream ( stdout );
}
#else
ptsConsole = new QTextStream ( stdout );
#endif
}
return ptsConsole;
}
// Implementation **************************************************************
int main ( int argc, char** argv )
{
QTextStream& tsConsole = *((new ConsoleWriterFactory())->get());
QString strArgument;
double rDbleArgument;
QTextStream& tsConsole = *( ( new ConsoleWriterFactory() )->get() );
QString strArgument;
double rDbleArgument;
// initialize all flags and string which might be changed by command line
// arguments

View file

@ -1209,6 +1209,31 @@ QString CCountyFlagIcons::GetResourceReference ( const QLocale::Country eCountry
}
// Console writer factory ------------------------------------------------------
QTextStream* ConsoleWriterFactory::get()
{
if ( ptsConsole == nullptr )
{
#if _WIN32
if ( !AttachConsole ( ATTACH_PARENT_PROCESS ) )
{
// Not run from console, dump logging to nowhere
static QString conout;
ptsConsole = new QTextStream ( &conout );
}
else
{
freopen ( "CONOUT$", "w", stdout );
ptsConsole = new QTextStream ( stdout );
}
#else
ptsConsole = new QTextStream ( stdout );
#endif
}
return ptsConsole;
}
/******************************************************************************\
* Global Functions Implementation *
\******************************************************************************/

View file

@ -482,6 +482,19 @@ public slots:
};
// Console writer factory ------------------------------------------------------
// this class was written by pljones
class ConsoleWriterFactory
{
public:
ConsoleWriterFactory() : ptsConsole ( nullptr ) { }
QTextStream* get();
private:
QTextStream* ptsConsole;
};
/******************************************************************************\
* Other Classes/Enums *
\******************************************************************************/
@ -1275,13 +1288,4 @@ protected:
bool bPreviousState;
};
class ConsoleWriterFactory
{
public:
ConsoleWriterFactory() : ptsConsole ( nullptr ) { }
QTextStream* get();
private:
QTextStream* ptsConsole;
};
#endif /* !defined ( UTIL_HOIH934256GEKJH98_3_43445KJIUHF1912__INCLUDED_ ) */