1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2014 LoRd_MuldeR <MuldeR2@GMX.de>
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 ///////////////////////////////////////////////////////////////////////////////
25 #define WIN32_LEAN_AND_MEAN 1
30 #include <MUtils/Terminal.h>
31 #include <MUtils/Global.h>
32 #include <MUtils/OSSupport.h>
33 #include "Utils_Win32.h"
34 #include "CriticalSection_Win32.h"
38 #include <QStringList>
50 static MUtils::Internal::CriticalSection g_terminal_lock;
53 #define stricmp(X,Y) _stricmp((X),(Y))
56 ///////////////////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////////////////
60 static void make_timestamp(char *timestamp, const size_t &buffsize)
66 if(localtime_s(&timeinfo, &rawtime) == 0)
68 strftime(timestamp, 32, "%H:%M:%S", &timeinfo);
76 static const char *clean_str(char *str)
80 while((*ptr) && isspace(*ptr))
86 while((*str) && isspace(*str))
92 size_t pos = strlen(str);
95 if(isspace(str[--pos]))
106 ///////////////////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////////////////
110 static bool g_terminal_attached = false;
111 static QScopedPointer<std::filebuf> g_filebufStdOut;
112 static QScopedPointer<std::filebuf> g_filebufStdErr;
113 static QScopedPointer<QFile> g_log_file;
115 void MUtils::Terminal::setup(int &argc, char **argv, const bool forceEnabled)
117 MUtils::Internal::CSLocker lock(g_terminal_lock);
118 bool enableConsole = (MUTILS_DEBUG) || forceEnabled;
122 wchar_t *logfile = NULL; size_t logfile_len = 0;
123 if(!_wdupenv_s(&logfile, &logfile_len, L"MUTILS_LOGFILE"))
125 if(logfile && (logfile_len > 0))
127 g_log_file.reset(new QFile(MUTILS_QSTR(logfile)));
128 if(g_log_file->open(QIODevice::WriteOnly))
130 static const char MARKER[3] = { char(0xEF), char(0xBB), char(0xBF) };
131 g_log_file->write(MARKER, 3);
140 for(int i = 0; i < argc; i++)
142 if(!stricmp(argv[i], "--console"))
144 enableConsole = true;
146 else if(!stricmp(argv[i], "--no-console"))
148 enableConsole = false;
155 if(!g_terminal_attached)
157 if(AllocConsole() != FALSE)
159 SetConsoleCtrlHandler(NULL, TRUE);
160 SetConsoleTitle(L"LameXP - Audio Encoder Front-End | Debug Console");
161 SetConsoleOutputCP(CP_UTF8);
162 g_terminal_attached = true;
166 if(g_terminal_attached)
168 //-------------------------------------------------------------------
169 //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
170 //-------------------------------------------------------------------
171 const int flags = _O_WRONLY | _O_U8TEXT;
172 const int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
173 const int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE ), flags);
174 FILE *const hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
175 FILE *const hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
179 g_filebufStdOut.reset(new std::filebuf(hfStdOut));
180 std::cout.rdbuf(g_filebufStdOut.data());
185 g_filebufStdErr.reset(new std::filebuf(hfStdErr));
186 std::cerr.rdbuf(g_filebufStdErr.data());
189 const HWND hwndConsole = GetConsoleWindow();
190 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
192 HMENU hMenu = GetSystemMenu(hwndConsole, 0);
193 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
194 RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
196 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
197 SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
198 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
204 ///////////////////////////////////////////////////////////////////////////////
206 ///////////////////////////////////////////////////////////////////////////////
209 static const WORD COLOR_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
210 static const WORD COLOR_YELLOW = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
211 static const WORD COLOR_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
212 static const WORD COLOR_DEFAULT= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
214 static void set_terminal_color(FILE* file, const WORD &attributes)
216 const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
217 if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
219 SetConsoleTextAttribute(hConsole, attributes);
223 ///////////////////////////////////////////////////////////////////////////////
225 ///////////////////////////////////////////////////////////////////////////////
227 static const char *const FORMAT = "[%c][%s] %s\r\n";
228 static const char *const GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
230 static void write_logfile_helper(QFile *const file, const int &type, const char *const message)
232 static char input[1024], output[1024], timestamp[32];
233 make_timestamp(timestamp, 32);
234 strncpy_s(input, 1024, message, _TRUNCATE);
235 const char *const temp = clean_str(input);
241 _snprintf_s(output, 1024, FORMAT, 'C', timestamp, temp);
244 _snprintf_s(output, 1024, FORMAT, 'W', timestamp, temp);
247 _snprintf_s(output, 1024, FORMAT, 'I', timestamp, temp);
255 static void write_debugger_helper(const int &type, const char *const message)
257 static char input[1024], output[1024], timestamp[32];
258 make_timestamp(timestamp, 32);
259 strncpy_s(input, 1024, message, _TRUNCATE);
260 const char *const temp = clean_str(input);
266 _snprintf_s(output, 1024, FORMAT, 'C', timestamp, temp);
269 _snprintf_s(output, 1024, FORMAT, 'W', timestamp, temp);
272 _snprintf_s(output, 1024, FORMAT, 'I', timestamp, temp);
276 OutputDebugStringA(output);
279 static void write_terminal_helper(const int &type, const char *const message)
281 if(_isatty(_fileno(stderr)))
283 UINT oldOutputCP = GetConsoleOutputCP();
284 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
290 set_terminal_color(stderr, COLOR_RED);
291 fprintf(stderr, GURU_MEDITATION);
292 fprintf(stderr, "%s\n", message);
296 set_terminal_color(stderr, COLOR_YELLOW);
297 fprintf(stderr, "%s\n", message);
301 set_terminal_color(stderr, COLOR_WHITE);
302 fprintf(stderr, "%s\n", message);
307 set_terminal_color(stderr, COLOR_DEFAULT);
308 if(oldOutputCP != CP_UTF8)
310 SetConsoleOutputCP(oldOutputCP);
315 void MUtils::Terminal::write(const int &type, const char *const message)
317 MUtils::Internal::CSLocker lock(g_terminal_lock);
319 if(g_terminal_attached)
321 write_terminal_helper(type, message);
325 write_debugger_helper(type, message);
328 if(!g_log_file.isNull())
330 write_logfile_helper(g_log_file.data(), type, message);
334 ///////////////////////////////////////////////////////////////////////////////
336 ///////////////////////////////////////////////////////////////////////////////
338 void MUtils::Terminal::set_icon(const QIcon &icon)
340 MUtils::Internal::CSLocker lock(g_terminal_lock);
342 if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
344 QLibrary kernel32("kernel32.dll");
347 typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
348 if(SetConsoleIconFun SetConsoleIconPtr = (SetConsoleIconFun) kernel32.resolve("SetConsoleIcon"))
350 if(HICON hIcon = qicon_to_hicon(icon, 16, 16))
352 SetConsoleIconPtr(hIcon);