Fixes to Windows console interaction.

Thanks To Jasin Colegrove for helping me understand the issues here!
This commit is contained in:
James Turner
2016-06-09 15:11:44 +01:00
parent 1c25d343a0
commit daa10503e6
2 changed files with 96 additions and 71 deletions
+54 -33
View File
@@ -38,9 +38,9 @@
#include <simgear/misc/sg_path.hxx> #include <simgear/misc/sg_path.hxx>
#ifdef SG_WINDOWS #if defined (SG_WINDOWS)
// for AllocConsole, OutputDebugString // for AllocConsole, OutputDebugString
#include "windows.h" #include <windows.h>
#endif #endif
const char* debugClassToString(sgDebugClass c) const char* debugClassToString(sgDebugClass c)
@@ -129,14 +129,15 @@ public:
StderrLogCallback(sgDebugClass c, sgDebugPriority p) : StderrLogCallback(sgDebugClass c, sgDebugPriority p) :
simgear::LogCallback(c, p) simgear::LogCallback(c, p)
{ {
#ifdef SG_WINDOWS
AllocConsole(); // but only if we want a console
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
#endif
} }
#if defined (SG_WINDOWS)
~StderrLogCallback()
{
FreeConsole();
}
#endif
virtual void operator()(sgDebugClass c, sgDebugPriority p, virtual void operator()(sgDebugClass c, sgDebugPriority p,
const char* file, int line, const std::string& aMessage) const char* file, int line, const std::string& aMessage)
{ {
@@ -220,22 +221,42 @@ public:
LogStreamPrivate() : LogStreamPrivate() :
m_logClass(SG_ALL), m_logClass(SG_ALL),
m_logPriority(SG_ALERT), m_logPriority(SG_ALERT),
m_isRunning(false), m_isRunning(false)
m_consoleRequested(false)
{ {
bool addStderr = true;
#if !defined(SG_WINDOWS) #if defined (SG_WINDOWS)
m_callbacks.push_back(new StderrLogCallback(m_logClass, m_logPriority)); // Check for stream redirection, has to be done before we call
m_consoleCallbacks.push_back(m_callbacks.back()); // Attach / AllocConsole
m_consoleRequested = true; const bool isFile = (GetFileType(GetStdHandle(STD_OUTPUT_HANDLE)) == FILE_TYPE_DISK); // Redirect to file?
if (AttachConsole(ATTACH_PARENT_PROCESS) == 0) {
// attach failed, don't install the callback
addStderr = false;
}
#endif #endif
if (addStderr) {
#if defined (SG_WINDOWS)
if (!isFile) {
// No - OK! now set streams to attached console
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
}
#endif
m_callbacks.push_back(new StderrLogCallback(m_logClass, m_logPriority));
m_consoleCallbacks.push_back(m_callbacks.back());
}
#if defined (SG_WINDOWS) && !defined(NDEBUG) #if defined (SG_WINDOWS) && !defined(NDEBUG)
m_callbacks.push_back(new WinDebugLogCallback(m_logClass, m_logPriority)); m_callbacks.push_back(new WinDebugLogCallback(m_logClass, m_logPriority));
m_consoleCallbacks.push_back(m_callbacks.back()); m_consoleCallbacks.push_back(m_callbacks.back());
#endif #endif
} }
~LogStreamPrivate()
{
BOOST_FOREACH(simgear::LogCallback* cb, m_callbacks) {
delete cb;
}
}
SGMutex m_lock; SGMutex m_lock;
SGBlockingQueue<LogEntry> m_entries; SGBlockingQueue<LogEntry> m_entries;
@@ -248,7 +269,6 @@ public:
sgDebugClass m_logClass; sgDebugClass m_logClass;
sgDebugPriority m_logPriority; sgDebugPriority m_logPriority;
bool m_isRunning; bool m_isRunning;
bool m_consoleRequested;
void startLog() void startLog()
{ {
@@ -329,24 +349,13 @@ public:
LogEntry entry(c, p, fileName, line, msg); LogEntry entry(c, p, fileName, line, msg);
m_entries.push(entry); m_entries.push(entry);
} }
void requestConsole()
{
PauseThread pause(this);
if (m_consoleRequested) {
return;
}
m_consoleRequested = true;
m_callbacks.push_back(new StderrLogCallback(m_logClass, m_logPriority));
m_consoleCallbacks.push_back(m_callbacks.back());
}
}; };
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
static logstream* global_logstream = NULL; static logstream* global_logstream = NULL;
static LogStreamPrivate* global_privateLogstream = NULL; static LogStreamPrivate* global_privateLogstream = NULL;
static SGMutex global_logStreamLock;
logstream::logstream() logstream::logstream()
{ {
@@ -354,6 +363,12 @@ logstream::logstream()
global_privateLogstream->startLog(); global_privateLogstream->startLog();
} }
logstream::~logstream()
{
global_privateLogstream->stop();
delete global_privateLogstream;
}
void void
logstream::setLogLevels( sgDebugClass c, sgDebugPriority p ) logstream::setLogLevels( sgDebugClass c, sgDebugPriority p )
{ {
@@ -409,6 +424,7 @@ logstream::set_log_classes( sgDebugClass c)
global_privateLogstream->setLogLevels(c, global_privateLogstream->m_logPriority); global_privateLogstream->setLogLevels(c, global_privateLogstream->m_logPriority);
} }
logstream& logstream&
sglog() sglog()
{ {
@@ -418,8 +434,7 @@ sglog()
// http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf // http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf
// in the absence of portable memory barrier ops in Simgear, // in the absence of portable memory barrier ops in Simgear,
// let's keep this correct & safe // let's keep this correct & safe
static SGMutex m; SGGuard<SGMutex> g(global_logStreamLock);
SGGuard<SGMutex> g(m);
if( !global_logstream ) if( !global_logstream )
global_logstream = new logstream(); global_logstream = new logstream();
@@ -437,8 +452,14 @@ namespace simgear
void requestConsole() void requestConsole()
{ {
sglog(); // force creation // this is a no-op now, stub exists for compatability for the moment.
global_privateLogstream->requestConsole(); }
void shutdownLogging()
{
SGGuard<SGMutex> g(global_logStreamLock);
delete global_logstream;
global_logstream = 0;
} }
} // of namespace simgear } // of namespace simgear
+4
View File
@@ -60,6 +60,8 @@ private:
*/ */
void requestConsole(); void requestConsole();
void shutdownLogging();
} // of namespace simgear } // of namespace simgear
/** /**
@@ -68,6 +70,8 @@ void requestConsole();
class logstream class logstream
{ {
public: public:
~logstream();
static void initGlobalLogstream(); static void initGlobalLogstream();
/** /**
* Set the global log class and priority level. * Set the global log class and priority level.