2014-11-25 22:32:20 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-07-18 14:01:48 +02:00
|
|
|
// MuldeR's Utilities for Qt
|
2016-02-20 16:30:17 +01:00
|
|
|
// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
|
2014-11-25 22:32:20 +01:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version, but always including the *additional*
|
|
|
|
// restrictions defined in the "License.txt" file.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
//
|
|
|
|
// http://www.gnu.org/licenses/gpl-2.0.txt
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2014-11-29 01:22:46 +01:00
|
|
|
//Windows includes
|
|
|
|
#define NOMINMAX
|
|
|
|
#define WIN32_LEAN_AND_MEAN 1
|
|
|
|
#include <Windows.h>
|
|
|
|
|
2014-11-25 22:32:20 +01:00
|
|
|
//Internal
|
2014-11-29 01:22:46 +01:00
|
|
|
#include <MUtils/Terminal.h>
|
2014-11-25 22:32:20 +01:00
|
|
|
#include <MUtils/Global.h>
|
|
|
|
#include <MUtils/OSSupport.h>
|
2014-11-29 01:22:46 +01:00
|
|
|
#include "Utils_Win32.h"
|
2014-11-25 22:32:20 +01:00
|
|
|
#include "CriticalSection_Win32.h"
|
|
|
|
|
|
|
|
//Qt
|
|
|
|
#include <QFile>
|
|
|
|
#include <QStringList>
|
2014-11-29 01:22:46 +01:00
|
|
|
#include <QIcon>
|
2014-11-25 22:32:20 +01:00
|
|
|
|
|
|
|
//CRT
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <ctime>
|
2015-08-09 18:15:51 +02:00
|
|
|
#include <stdarg.h>
|
2015-08-09 21:21:05 +02:00
|
|
|
#include <io.h>
|
|
|
|
#include <fcntl.h>
|
2014-11-25 22:32:20 +01:00
|
|
|
|
2014-11-26 02:37:08 +01:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define stricmp(X,Y) _stricmp((X),(Y))
|
|
|
|
#endif
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
#define VALID_HANLDE(X) (((X) != NULL) && ((X) != INVALID_HANDLE_VALUE))
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TERMINAL VARIABLES
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//Critical section
|
|
|
|
static MUtils::Internal::CriticalSection g_terminal_lock;
|
|
|
|
|
|
|
|
//Is terminal attached?
|
|
|
|
static volatile bool g_terminal_attached = false;
|
|
|
|
|
|
|
|
//Terminal output buffer
|
|
|
|
static const size_t BUFF_SIZE = 8192;
|
|
|
|
static char g_conOutBuff[BUFF_SIZE] = { '\0' };
|
|
|
|
|
2015-08-09 21:21:05 +02:00
|
|
|
//Buffer objects
|
|
|
|
static QScopedPointer<std::filebuf> g_fileBuf_stdout;
|
|
|
|
static QScopedPointer<std::filebuf> g_fileBuf_stderr;
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
//The log file
|
|
|
|
static QScopedPointer<QFile> g_terminal_log_file;
|
|
|
|
|
2016-12-22 22:49:30 +01:00
|
|
|
//Terminal icon
|
|
|
|
static HICON g_terminal_icon = NULL;
|
|
|
|
|
2014-11-25 22:32:20 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// HELPER FUNCTIONS
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
static inline void make_timestamp(char *timestamp, const size_t &buffsize)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
|
|
|
time_t rawtime;
|
|
|
|
struct tm timeinfo;
|
|
|
|
|
|
|
|
time(&rawtime);
|
|
|
|
if(localtime_s(&timeinfo, &rawtime) == 0)
|
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
strftime(timestamp, buffsize, "%H:%M:%S", &timeinfo);
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timestamp[0] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
static inline bool null_or_whitespace(const char *const str)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
if (str)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
while (str[pos])
|
|
|
|
{
|
|
|
|
if (!(isspace(str[pos]) || iscntrl(str[pos])))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
pos++;
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
2015-08-09 18:15:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
|
2015-08-09 21:21:05 +02:00
|
|
|
static inline size_t clean_string(char *const str)
|
2015-08-09 18:15:51 +02:00
|
|
|
{
|
|
|
|
bool space_flag = true;
|
|
|
|
size_t src = 0, out = 0;
|
2014-11-25 22:32:20 +01:00
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
while (str[src])
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
if (isspace(str[src]) || iscntrl(str[src])) /*replace any space-sequence with a single space character*/
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
src++;
|
|
|
|
if (!space_flag)
|
|
|
|
{
|
|
|
|
space_flag = true;
|
|
|
|
str[out++] = 0x20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /*otherwise we'll just copy over the current character*/
|
|
|
|
{
|
|
|
|
if (src != out)
|
|
|
|
{
|
|
|
|
str[out] = str[src];
|
|
|
|
}
|
|
|
|
space_flag = false;
|
|
|
|
out++; src++;
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
if (space_flag && (out > 0)) /*trim trailing space, if any*/
|
|
|
|
{
|
|
|
|
out--;
|
|
|
|
}
|
2014-12-21 17:19:04 +01:00
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
str[out] = NULL;
|
|
|
|
return out;
|
|
|
|
}
|
2014-12-21 17:19:04 +01:00
|
|
|
|
2016-12-22 22:49:30 +01:00
|
|
|
static inline void set_hicon(HICON *const ptr, const HICON val)
|
|
|
|
{
|
|
|
|
if (*ptr)
|
|
|
|
{
|
|
|
|
DestroyIcon(*ptr);
|
|
|
|
}
|
|
|
|
*ptr = val;
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:19:04 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-08-09 18:15:51 +02:00
|
|
|
// TERMINAL SETUP
|
2014-12-21 17:19:04 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-08-09 21:21:05 +02:00
|
|
|
static inline std::filebuf *terminal_connect(FILE *const fs, std::ostream &os)
|
2014-12-21 17:19:04 +01:00
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
std::filebuf *result = NULL;
|
|
|
|
FILE *temp;
|
|
|
|
if (freopen_s(&temp, "CONOUT$", "wb", fs) == 0)
|
|
|
|
{
|
|
|
|
os.rdbuf(result = new std::filebuf(temp));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2014-12-21 17:19:04 +01:00
|
|
|
|
2015-08-09 21:21:05 +02:00
|
|
|
static void terminal_shutdown(void)
|
|
|
|
{
|
2015-08-09 21:34:31 +02:00
|
|
|
MUtils::Internal::CSLocker lock(g_terminal_lock);
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
if (g_terminal_attached)
|
2014-12-21 17:19:04 +01:00
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
g_fileBuf_stdout.reset();
|
|
|
|
g_fileBuf_stderr.reset();
|
|
|
|
FILE *temp[2];
|
|
|
|
if(stdout) freopen_s(&temp[0], "NUL", "wb", stdout);
|
|
|
|
if(stderr) freopen_s(&temp[1], "NUL", "wb", stderr);
|
|
|
|
FreeConsole();
|
2016-12-22 22:49:30 +01:00
|
|
|
set_hicon(&g_terminal_icon, NULL);
|
2014-12-21 17:19:04 +01:00
|
|
|
g_terminal_attached = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MUtils::Terminal::setup(int &argc, char **argv, const char* const appName, const bool forceEnabled)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
|
|
|
MUtils::Internal::CSLocker lock(g_terminal_lock);
|
|
|
|
bool enableConsole = (MUTILS_DEBUG) || forceEnabled;
|
|
|
|
|
|
|
|
if(_environ)
|
|
|
|
{
|
|
|
|
wchar_t *logfile = NULL; size_t logfile_len = 0;
|
|
|
|
if(!_wdupenv_s(&logfile, &logfile_len, L"MUTILS_LOGFILE"))
|
|
|
|
{
|
|
|
|
if(logfile && (logfile_len > 0))
|
|
|
|
{
|
2014-12-21 17:19:04 +01:00
|
|
|
g_terminal_log_file.reset(new QFile(MUTILS_QSTR(logfile)));
|
|
|
|
if(g_terminal_log_file->open(QIODevice::WriteOnly))
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
|
|
|
static const char MARKER[3] = { char(0xEF), char(0xBB), char(0xBF) };
|
2014-12-21 17:19:04 +01:00
|
|
|
g_terminal_log_file->write(MARKER, 3);
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
free(logfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!MUTILS_DEBUG)
|
|
|
|
{
|
2014-11-26 02:37:08 +01:00
|
|
|
for(int i = 0; i < argc; i++)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2014-11-26 02:37:08 +01:00
|
|
|
if(!stricmp(argv[i], "--console"))
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
|
|
|
enableConsole = true;
|
|
|
|
}
|
2014-11-26 02:37:08 +01:00
|
|
|
else if(!stricmp(argv[i], "--no-console"))
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
|
|
|
enableConsole = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(enableConsole)
|
|
|
|
{
|
|
|
|
if(!g_terminal_attached)
|
|
|
|
{
|
|
|
|
if(AllocConsole() != FALSE)
|
|
|
|
{
|
|
|
|
SetConsoleOutputCP(CP_UTF8);
|
2014-12-21 17:19:04 +01:00
|
|
|
SetConsoleCtrlHandler(NULL, TRUE);
|
|
|
|
if(appName && appName[0])
|
|
|
|
{
|
|
|
|
char title[128];
|
|
|
|
_snprintf_s(title, 128, _TRUNCATE, "%s | Debug Console", appName);
|
|
|
|
SetConsoleTitleA(title);
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
g_terminal_attached = true;
|
|
|
|
}
|
|
|
|
}
|
2014-12-20 23:47:06 +01:00
|
|
|
|
2014-11-25 22:32:20 +01:00
|
|
|
if(g_terminal_attached)
|
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
g_fileBuf_stdout.reset(terminal_connect(stdout, std::cout));
|
|
|
|
g_fileBuf_stderr.reset(terminal_connect(stderr, std::cerr));
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
atexit(terminal_shutdown);
|
2014-11-25 22:32:20 +01:00
|
|
|
|
2014-11-29 01:22:46 +01:00
|
|
|
const HWND hwndConsole = GetConsoleWindow();
|
|
|
|
if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
|
|
|
|
{
|
|
|
|
HMENU hMenu = GetSystemMenu(hwndConsole, 0);
|
|
|
|
EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
|
|
|
|
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
|
2014-11-25 22:32:20 +01:00
|
|
|
|
2014-11-29 01:22:46 +01:00
|
|
|
SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
|
|
|
|
SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
|
|
|
|
SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TERMINAL COLORS
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//Colors
|
|
|
|
static const WORD COLOR_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
static const WORD COLOR_YELLOW = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
static const WORD COLOR_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
static const WORD COLOR_DEFAULT= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
|
|
|
|
|
2015-08-09 21:21:05 +02:00
|
|
|
static void set_terminal_color(FILE *const fp, const WORD &attributes)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
if(_isatty(_fileno(fp)))
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(fp)));
|
|
|
|
if (VALID_HANLDE(hConsole))
|
|
|
|
{
|
|
|
|
SetConsoleTextAttribute(hConsole, attributes);
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// WRITE TO TERMINAL
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static const char *const FORMAT = "[%c][%s] %s\r\n";
|
|
|
|
static const char *const GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
static void write_to_logfile(QFile *const file, const int &type, const char *const message)
|
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
int len = -1;
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
if (null_or_whitespace(message))
|
|
|
|
{
|
|
|
|
return; /*don't write empty message to log file*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static char timestamp[32];
|
2014-11-25 22:32:20 +01:00
|
|
|
make_timestamp(timestamp, 32);
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case QtCriticalMsg:
|
|
|
|
case QtFatalMsg:
|
2015-08-09 18:15:51 +02:00
|
|
|
len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'C', timestamp, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
2015-08-09 18:15:51 +02:00
|
|
|
len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'W', timestamp, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-09 18:15:51 +02:00
|
|
|
len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'I', timestamp, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
if (clean_string(g_conOutBuff) > 0)
|
2015-08-09 18:15:51 +02:00
|
|
|
{
|
|
|
|
file->write(g_conOutBuff);
|
|
|
|
file->flush();
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
static void write_to_debugger(const int &type, const char *const message)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
int len = -1;
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
if (null_or_whitespace(message))
|
|
|
|
{
|
|
|
|
return; /*don't send empty message to debugger*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static char timestamp[32];
|
2014-11-25 22:32:20 +01:00
|
|
|
make_timestamp(timestamp, 32);
|
|
|
|
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case QtCriticalMsg:
|
|
|
|
case QtFatalMsg:
|
2015-08-09 18:15:51 +02:00
|
|
|
len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'C', timestamp, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
2015-08-09 18:15:51 +02:00
|
|
|
len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'W', timestamp, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-09 18:15:51 +02:00
|
|
|
len = _snprintf_s(g_conOutBuff, BUFF_SIZE, _TRUNCATE, FORMAT, 'I', timestamp, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
if (len > 0)
|
|
|
|
{
|
2015-08-09 21:21:05 +02:00
|
|
|
if (clean_string(g_conOutBuff) > 0)
|
2015-08-09 18:15:51 +02:00
|
|
|
{
|
|
|
|
OutputDebugStringA(g_conOutBuff);
|
|
|
|
}
|
|
|
|
}
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
|
2015-08-09 18:15:51 +02:00
|
|
|
static void write_to_terminal(const int &type, const char *const message)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
switch(type)
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
case QtCriticalMsg:
|
|
|
|
case QtFatalMsg:
|
2015-08-09 21:21:05 +02:00
|
|
|
set_terminal_color(stderr, COLOR_RED);
|
|
|
|
fprintf(stderr, GURU_MEDITATION);
|
|
|
|
fprintf(stderr, "%s\n", message);
|
2015-08-09 18:15:51 +02:00
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
2015-08-09 21:21:05 +02:00
|
|
|
set_terminal_color(stderr, COLOR_YELLOW);
|
|
|
|
fprintf(stderr, "%s\n", message);
|
2015-08-09 18:15:51 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-08-09 21:21:05 +02:00
|
|
|
set_terminal_color(stderr, COLOR_WHITE);
|
|
|
|
fprintf(stderr, "%s\n", message);
|
2015-08-09 18:15:51 +02:00
|
|
|
break;
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
2015-08-09 21:21:05 +02:00
|
|
|
|
|
|
|
fflush(stderr);
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MUtils::Terminal::write(const int &type, const char *const message)
|
|
|
|
{
|
|
|
|
MUtils::Internal::CSLocker lock(g_terminal_lock);
|
|
|
|
|
|
|
|
if(g_terminal_attached)
|
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
write_to_terminal(type, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
write_to_debugger(type, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
|
2014-12-21 17:19:04 +01:00
|
|
|
if(!g_terminal_log_file.isNull())
|
2014-11-25 22:32:20 +01:00
|
|
|
{
|
2015-08-09 18:15:51 +02:00
|
|
|
write_to_logfile(g_terminal_log_file.data(), type, message);
|
2014-11-25 22:32:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-29 01:22:46 +01:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// TERMINAL ICON
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void MUtils::Terminal::set_icon(const QIcon &icon)
|
|
|
|
{
|
|
|
|
MUtils::Internal::CSLocker lock(g_terminal_lock);
|
|
|
|
|
2014-12-03 22:21:53 +01:00
|
|
|
if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
|
2014-11-29 01:22:46 +01:00
|
|
|
{
|
2016-12-22 22:49:30 +01:00
|
|
|
if(const HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
|
2014-11-29 01:22:46 +01:00
|
|
|
{
|
2016-12-22 22:49:30 +01:00
|
|
|
typedef BOOL(__stdcall *SetConsoleIconFun)(HICON);
|
|
|
|
bool success = false;
|
|
|
|
if (const SetConsoleIconFun pSetConsoleIconFun = MUtils::Win32Utils::resolve<SetConsoleIconFun>(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon")))
|
|
|
|
{
|
|
|
|
const DWORD before = GetLastError();
|
|
|
|
if (pSetConsoleIconFun(hIcon))
|
|
|
|
{
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const DWORD error = GetLastError();
|
|
|
|
qWarning("SetConsoleIcon() has failed! [Error: 0x%08X]", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
const HWND hwndConsole = GetConsoleWindow();
|
|
|
|
if ((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
|
|
|
|
{
|
|
|
|
SendMessage(hwndConsole, WM_SETICON, ICON_SMALL, LPARAM(hIcon));
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (success)
|
2014-11-29 01:22:46 +01:00
|
|
|
{
|
2016-12-22 22:49:30 +01:00
|
|
|
set_hicon(&g_terminal_icon, hIcon);
|
2014-11-29 01:22:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|