summaryrefslogtreecommitdiffstats
path: root/client/windows
diff options
context:
space:
mode:
authorYaniv Kamay <ykamay@redhat.com>2010-01-11 19:57:29 +0200
committerYaniv Kamay <ykamay@redhat.com>2010-01-11 19:57:29 +0200
commit8ceb531958cb649b7cc7f16f2f504125cb430fe4 (patch)
tree847f4325a37ce7bfccf6a469659b11f775cc3e62 /client/windows
parent3c1ff6448d847e6e96425fa166446b99bac5e592 (diff)
downloadspice-8ceb531958cb649b7cc7f16f2f504125cb430fe4.tar.gz
spice-8ceb531958cb649b7cc7f16f2f504125cb430fe4.tar.xz
spice-8ceb531958cb649b7cc7f16f2f504125cb430fe4.zip
client: add Platform::term_printf
Platform::term_printf is a variant of printf that on windows dynamically opens console in order to have visible output during command line processing.
Diffstat (limited to 'client/windows')
-rw-r--r--client/windows/main.cpp28
-rw-r--r--client/windows/platform.cpp64
2 files changed, 66 insertions, 26 deletions
diff --git a/client/windows/main.cpp b/client/windows/main.cpp
index a1575e90..afc98c97 100644
--- a/client/windows/main.cpp
+++ b/client/windows/main.cpp
@@ -22,12 +22,6 @@ extern "C" {
#include "pthread.h"
}
-//#define OPEN_CONSOLE
-#ifdef OPEN_CONSOLE
-#include <io.h>
-#include <conio.h>
-#endif
-
#include "application.h"
#include "debug.h"
#include "utils.h"
@@ -83,23 +77,6 @@ int WINAPI WinMain(HINSTANCE hInstance,
try {
init_version_string();
-#ifdef OPEN_CONSOLE
- AllocConsole();
- HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
- int hConHandle = _open_osfhandle((intptr_t)h, _O_TEXT);
- FILE * fp = _fdopen(hConHandle, "w");
- *stdout = *fp;
-
- h = GetStdHandle(STD_INPUT_HANDLE);
- hConHandle = _open_osfhandle((intptr_t)h, _O_TEXT);
- fp = _fdopen(hConHandle, "r");
- *stdin = *fp;
-
- h = GetStdHandle(STD_ERROR_HANDLE);
- hConHandle = _open_osfhandle((intptr_t)h, _O_TEXT);
- fp = _fdopen(hConHandle, "w");
- *stderr = *fp;
-#endif
pthread_win32_process_attach_np();
init_winsock();
exit_val = Application::main(__argc, __argv, version_string);
@@ -114,11 +91,10 @@ int WINAPI WinMain(HINSTANCE hInstance,
LOG_ERROR("unhandled exception");
exit_val = SPICEC_ERROR_CODE_ERROR;
}
+
log4cpp::Category::shutdown();
-#ifdef OPEN_CONSOLE
- _getch();
-#endif
pthread_win32_process_detach_np();
+
return exit_val;
}
diff --git a/client/windows/platform.cpp b/client/windows/platform.cpp
index 67bb1a02..c364b35d 100644
--- a/client/windows/platform.cpp
+++ b/client/windows/platform.cpp
@@ -18,6 +18,8 @@
#include "common.h"
#include <shlobj.h>
+#include <io.h>
+#include <conio.h>
#include "platform.h"
#include "win_platform.h"
@@ -745,3 +747,65 @@ void WinPlatform::exit_modal_loop()
KillTimer(paltform_win, MODAL_LOOP_TIMER_ID);
modal_loop_active = false;
}
+
+static bool has_console = false;
+
+static void create_console()
+{
+ static Mutex console_mutex;
+
+ Lock lock(console_mutex);
+
+ if (has_console) {
+ return;
+ }
+
+ AllocConsole();
+ HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
+ int hConHandle = _open_osfhandle((intptr_t)h, _O_TEXT);
+ FILE * fp = _fdopen(hConHandle, "w");
+ *stdout = *fp;
+
+ h = GetStdHandle(STD_INPUT_HANDLE);
+ hConHandle = _open_osfhandle((intptr_t)h, _O_TEXT);
+ fp = _fdopen(hConHandle, "r");
+ *stdin = *fp;
+
+ h = GetStdHandle(STD_ERROR_HANDLE);
+ hConHandle = _open_osfhandle((intptr_t)h, _O_TEXT);
+ fp = _fdopen(hConHandle, "w");
+ *stderr = *fp;
+
+ has_console = true;
+
+ HWND consol_window = GetConsoleWindow();
+
+ if (consol_window) {
+ SetForegroundWindow(consol_window);
+ }
+}
+
+class ConsoleWait {
+public:
+ ~ConsoleWait()
+ {
+ if (has_console) {
+ Platform::term_printf("\n\nPress any key to exit...");
+ _getch();
+ }
+ }
+
+} console_wait;
+
+
+void Platform::term_printf(const char* format, ...)
+{
+ if (!has_console) {
+ create_console();
+ }
+
+ va_list ap;
+ va_start(ap, format);
+ vprintf(format, ap);
+ va_end(ap);
+}