Some clean-up and simplification for JobObject class.
[MUtilities.git] / src / Terminal_Win32.cpp
blobdc142fd11420eca302b971a0ac46ff5308f34c9a
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version, but always including the *additional*
9 // restrictions defined in the "License.txt" file.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 //Windows includes
24 #define NOMINMAX
25 #define WIN32_LEAN_AND_MEAN 1
26 #include <Windows.h>
28 //Internal
29 #include <MUtils/Terminal.h>
30 #include <MUtils/Global.h>
31 #include <MUtils/OSSupport.h>
32 #include "Utils_Win32.h"
33 #include "CriticalSection_Win32.h"
35 //Qt
36 #include <QFile>
37 #include <QStringList>
38 #include <QIcon>
40 //CRT
41 #include <iostream>
42 #include <fstream>
43 #include <ctime>
44 #include <stdarg.h>
45 #include <io.h>
46 #include <fcntl.h>
48 #ifdef _MSC_VER
49 #define stricmp(X,Y) _stricmp((X),(Y))
50 #endif
52 #define VALID_HANLDE(X) (((X) != NULL) && ((X) != INVALID_HANDLE_VALUE))
54 ///////////////////////////////////////////////////////////////////////////////
55 // TERMINAL VARIABLES
56 ///////////////////////////////////////////////////////////////////////////////
58 //Critical section
59 static MUtils::Internal::CriticalSection g_terminal_lock;
61 //Is terminal attached?
62 static volatile bool g_terminal_attached = false;
64 //Terminal output buffer
65 static const size_t BUFF_SIZE = 8192;
66 static char g_conOutBuff[BUFF_SIZE] = { '\0' };
68 //Buffer objects
69 static QScopedPointer<std::filebuf> g_fileBuf_stdout;
70 static QScopedPointer<std::filebuf> g_fileBuf_stderr;
72 //The log file
73 static QScopedPointer<QFile> g_terminal_log_file;
75 //Terminal icon
76 static HICON g_terminal_icon = NULL;
78 ///////////////////////////////////////////////////////////////////////////////
79 // HELPER FUNCTIONS
80 ///////////////////////////////////////////////////////////////////////////////
82 static inline void make_timestamp(char *timestamp, const size_t &buffsize)
84 time_t rawtime;
85 struct tm timeinfo;
87 time(&rawtime);
88 if(localtime_s(&timeinfo, &rawtime) == 0)
90 strftime(timestamp, buffsize, "%H:%M:%S", &timeinfo);
92 else
94 timestamp[0] = '\0';
98 static inline bool null_or_whitespace(const char *const str)
100 if (str)
102 size_t pos = 0;
103 while (str[pos])
105 if (!(isspace(str[pos]) || iscntrl(str[pos])))
107 return false;
109 pos++;
112 return true;
115 static inline size_t clean_string(char *const str)
117 bool space_flag = true;
118 size_t src = 0, out = 0;
120 while (str[src])
122 if (isspace(str[src]) || iscntrl(str[src])) /*replace any space-sequence with a single space character*/
124 src++;
125 if (!space_flag)
127 space_flag = true;
128 str[out++] = 0x20;
131 else /*otherwise we'll just copy over the current character*/
133 if (src != out)
135 str[out] = str[src];
137 space_flag = false;
138 out++; src++;
142 if (space_flag && (out > 0)) /*trim trailing space, if any*/
144 out--;
147 str[out] = NULL;
148 return out;
151 static inline void set_hicon(HICON *const ptr, const HICON val)
153 if (*ptr)
155 DestroyIcon(*ptr);
157 *ptr = val;
160 ///////////////////////////////////////////////////////////////////////////////
161 // TERMINAL SETUP
162 ///////////////////////////////////////////////////////////////////////////////
164 static inline std::filebuf *terminal_connect(FILE *const fs, std::ostream &os)
166 std::filebuf *result = NULL;
167 FILE *temp;
168 if (freopen_s(&temp, "CONOUT$", "wb", fs) == 0)
170 os.rdbuf(result = new std::filebuf(temp));
172 return result;
175 static void terminal_shutdown(void)
177 MUtils::Internal::CSLocker lock(g_terminal_lock);
179 if (g_terminal_attached)
181 g_fileBuf_stdout.reset();
182 g_fileBuf_stderr.reset();
183 FILE *temp[2];
184 if(stdout) freopen_s(&temp[0], "NUL", "wb", stdout);
185 if(stderr) freopen_s(&temp[1], "NUL", "wb", stderr);
186 FreeConsole();
187 set_hicon(&g_terminal_icon, NULL);
188 g_terminal_attached = false;
192 void MUtils::Terminal::setup(int &argc, char **argv, const char* const appName, const bool forceEnabled)
194 MUtils::Internal::CSLocker lock(g_terminal_lock);
195 bool enableConsole = (MUTILS_DEBUG) || forceEnabled;
197 if(_environ)
199 wchar_t *logfile = NULL; size_t logfile_len = 0;
200 if(!_wdupenv_s(&logfile, &logfile_len, L"MUTILS_LOGFILE"))
202 if(logfile && (logfile_len > 0))
204 g_terminal_log_file.reset(new QFile(MUTILS_QSTR(logfile)));
205 if(g_terminal_log_file->open(QIODevice::WriteOnly))
207 static const char MARKER[3] = { char(0xEF), char(0xBB), char(0xBF) };
208 g_terminal_log_file->write(MARKER, 3);
210 free(logfile);
215 if(!MUTILS_DEBUG)
217 for(int i = 0; i < argc; i++)
219 if(!stricmp(argv[i], "--console"))
221 enableConsole = true;
223 else if(!stricmp(argv[i], "--no-console"))
225 enableConsole = false;
230 if(enableConsole)
232 if(!g_terminal_attached)
234 if(AllocConsole() != FALSE)
236 SetConsoleOutputCP(CP_UTF8);
237 SetConsoleCtrlHandler(NULL, TRUE);
238 if(appName && appName[0])
240 char title[128];
241 _snprintf_s(title, 128, _TRUNCATE, "%s | Debug Console", appName);
242 SetConsoleTitleA(title);
244 g_terminal_attached = true;
248 if(g_terminal_attached)
250 g_fileBuf_stdout.reset(terminal_connect(stdout, std::cout));
251 g_fileBuf_stderr.reset(terminal_connect(stderr, std::cerr));
253 atexit(terminal_shutdown);
255 const HWND hwndConsole = GetConsoleWindow();
256 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
258 HMENU hMenu = GetSystemMenu(hwndConsole, 0);
259 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
260 RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
262 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
263 SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
264 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
270 ///////////////////////////////////////////////////////////////////////////////
271 // TERMINAL COLORS
272 ///////////////////////////////////////////////////////////////////////////////
274 //Colors
275 static const WORD COLOR_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
276 static const WORD COLOR_YELLOW = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
277 static const WORD COLOR_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
278 static const WORD COLOR_DEFAULT= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
280 static void set_terminal_color(FILE *const fp, const WORD &attributes)
282 if(_isatty(_fileno(fp)))
284 const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(fp)));
285 if (VALID_HANLDE(hConsole))
287 SetConsoleTextAttribute(hConsole, attributes);
292 ///////////////////////////////////////////////////////////////////////////////
293 // WRITE TO TERMINAL
294 ///////////////////////////////////////////////////////////////////////////////
296 static const char *const FORMAT = "[%c][%s] %s\r\n";
297 static const char *const GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
299 static void write_to_logfile(QFile *const file, const int &type, const char *const message)
301 int len = -1;
303 if (null_or_whitespace(message))
305 return; /*don't write empty message to log file*/
308 static char timestamp[32];
309 make_timestamp(timestamp, 32);
311 switch(type)
313 case QtCriticalMsg:
314 case QtFatalMsg:
315 len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'C', timestamp, message);
316 break;
317 case QtWarningMsg:
318 len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'W', timestamp, message);
319 break;
320 default:
321 len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'I', timestamp, message);
322 break;
325 if (len > 0)
327 if (clean_string(g_conOutBuff) > 0)
329 file->write(g_conOutBuff);
330 file->flush();
335 static void write_to_debugger(const int &type, const char *const message)
337 int len = -1;
339 if (null_or_whitespace(message))
341 return; /*don't send empty message to debugger*/
344 static char timestamp[32];
345 make_timestamp(timestamp, 32);
347 switch(type)
349 case QtCriticalMsg:
350 case QtFatalMsg:
351 len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'C', timestamp, message);
352 break;
353 case QtWarningMsg:
354 len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'W', timestamp, message);
355 break;
356 default:
357 len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'I', timestamp, message);
358 break;
361 if (len > 0)
363 if (clean_string(g_conOutBuff) > 0)
365 OutputDebugStringA(g_conOutBuff);
370 static void write_to_terminal(const int &type, const char *const message)
372 switch(type)
374 case QtCriticalMsg:
375 case QtFatalMsg:
376 set_terminal_color(stderr, COLOR_RED);
377 fprintf(stderr, GURU_MEDITATION);
378 fprintf(stderr, "%s\n", message);
379 break;
380 case QtWarningMsg:
381 set_terminal_color(stderr, COLOR_YELLOW);
382 fprintf(stderr, "%s\n", message);
383 break;
384 default:
385 set_terminal_color(stderr, COLOR_WHITE);
386 fprintf(stderr, "%s\n", message);
387 break;
390 fflush(stderr);
393 void MUtils::Terminal::write(const int &type, const char *const message)
395 MUtils::Internal::CSLocker lock(g_terminal_lock);
397 if(g_terminal_attached)
399 write_to_terminal(type, message);
401 else
403 write_to_debugger(type, message);
406 if(!g_terminal_log_file.isNull())
408 write_to_logfile(g_terminal_log_file.data(), type, message);
412 ///////////////////////////////////////////////////////////////////////////////
413 // TERMINAL ICON
414 ///////////////////////////////////////////////////////////////////////////////
416 void MUtils::Terminal::set_icon(const QIcon &icon)
418 MUtils::Internal::CSLocker lock(g_terminal_lock);
420 if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
422 if(const HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
424 typedef BOOL(__stdcall *SetConsoleIconFun)(HICON);
425 bool success = false;
426 if (const SetConsoleIconFun pSetConsoleIconFun = MUtils::Win32Utils::resolve<SetConsoleIconFun>(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon")))
428 const DWORD before = GetLastError();
429 if (pSetConsoleIconFun(hIcon))
431 success = true;
433 else
435 const DWORD error = GetLastError();
436 qWarning("SetConsoleIcon() has failed! [Error: 0x%08X]", error);
439 if (!success)
441 const HWND hwndConsole = GetConsoleWindow();
442 if ((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
444 SendMessage(hwndConsole, WM_SETICON, ICON_SMALL, LPARAM(hIcon));
445 success = true;
448 if (success)
450 set_hicon(&g_terminal_icon, hIcon);