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 ///////////////////////////////////////////////////////////////////////////////
23 #include <MUtils/Terminal.h>
26 #include <MUtils/Global.h>
27 #include <MUtils/OSSupport.h>
28 #include "CriticalSection_Win32.h"
32 #define WIN32_LEAN_AND_MEAN 1
37 #include <QStringList>
47 static MUtils::Internal::CriticalSection g_terminal_lock;
50 #define stricmp(X,Y) _stricmp((X),(Y))
53 ///////////////////////////////////////////////////////////////////////////////
55 ///////////////////////////////////////////////////////////////////////////////
57 static void make_timestamp(char *timestamp, const size_t &buffsize)
63 if(localtime_s(&timeinfo, &rawtime) == 0)
65 strftime(timestamp, 32, "%H:%M:%S", &timeinfo);
73 static const char *clean_str(char *str)
77 while((*ptr) && isspace(*ptr))
83 while((*str) && isspace(*str))
89 size_t pos = strlen(str);
92 if(isspace(str[--pos]))
103 ///////////////////////////////////////////////////////////////////////////////
105 ///////////////////////////////////////////////////////////////////////////////
107 static bool g_terminal_attached = false;
108 static QScopedPointer<std::filebuf> g_filebufStdOut;
109 static QScopedPointer<std::filebuf> g_filebufStdErr;
110 static QScopedPointer<QFile> g_log_file;
112 void MUtils::Terminal::setup(int &argc, char **argv, const bool forceEnabled)
114 MUtils::Internal::CSLocker lock(g_terminal_lock);
115 bool enableConsole = (MUTILS_DEBUG) || forceEnabled;
119 wchar_t *logfile = NULL; size_t logfile_len = 0;
120 if(!_wdupenv_s(&logfile, &logfile_len, L"MUTILS_LOGFILE"))
122 if(logfile && (logfile_len > 0))
124 g_log_file.reset(new QFile(MUTILS_QSTR(logfile)));
125 if(g_log_file->open(QIODevice::WriteOnly))
127 static const char MARKER[3] = { char(0xEF), char(0xBB), char(0xBF) };
128 g_log_file->write(MARKER, 3);
137 for(int i = 0; i < argc; i++)
139 if(!stricmp(argv[i], "--console"))
141 enableConsole = true;
143 else if(!stricmp(argv[i], "--no-console"))
145 enableConsole = false;
152 if(!g_terminal_attached)
154 if(AllocConsole() != FALSE)
156 SetConsoleCtrlHandler(NULL, TRUE);
157 SetConsoleTitle(L"LameXP - Audio Encoder Front-End | Debug Console");
158 SetConsoleOutputCP(CP_UTF8);
159 g_terminal_attached = true;
163 if(g_terminal_attached)
165 //-------------------------------------------------------------------
166 //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
167 //-------------------------------------------------------------------
168 const int flags = _O_WRONLY | _O_U8TEXT;
169 const int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
170 const int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE ), flags);
171 FILE *const hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
172 FILE *const hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
176 g_filebufStdOut.reset(new std::filebuf(hfStdOut));
177 std::cout.rdbuf(g_filebufStdOut.data());
182 g_filebufStdErr.reset(new std::filebuf(hfStdErr));
183 std::cerr.rdbuf(g_filebufStdErr.data());
187 HWND hwndConsole = GetConsoleWindow();
188 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
190 HMENU hMenu = GetSystemMenu(hwndConsole, 0);
191 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
192 RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
194 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
195 SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
196 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
201 ///////////////////////////////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////////////////////////////
206 static const WORD COLOR_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
207 static const WORD COLOR_YELLOW = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
208 static const WORD COLOR_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
209 static const WORD COLOR_DEFAULT= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
211 static void set_terminal_color(FILE* file, const WORD &attributes)
213 const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
214 if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
216 SetConsoleTextAttribute(hConsole, attributes);
220 ///////////////////////////////////////////////////////////////////////////////
222 ///////////////////////////////////////////////////////////////////////////////
224 static const char *const FORMAT = "[%c][%s] %s\r\n";
225 static const char *const GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
227 static void write_logfile_helper(QFile *const file, const int &type, const char *const message)
229 static char input[1024], output[1024], timestamp[32];
230 make_timestamp(timestamp, 32);
231 strncpy_s(input, 1024, message, _TRUNCATE);
232 const char *const temp = clean_str(input);
238 _snprintf_s(output, 1024, FORMAT, 'C', timestamp, temp);
241 _snprintf_s(output, 1024, FORMAT, 'W', timestamp, temp);
244 _snprintf_s(output, 1024, FORMAT, 'I', timestamp, temp);
252 static void write_debugger_helper(const int &type, const char *const message)
254 static char input[1024], output[1024], timestamp[32];
255 make_timestamp(timestamp, 32);
256 strncpy_s(input, 1024, message, _TRUNCATE);
257 const char *const temp = clean_str(input);
263 _snprintf_s(output, 1024, FORMAT, 'C', timestamp, temp);
266 _snprintf_s(output, 1024, FORMAT, 'W', timestamp, temp);
269 _snprintf_s(output, 1024, FORMAT, 'I', timestamp, temp);
273 OutputDebugStringA(output);
276 static void write_terminal_helper(const int &type, const char *const message)
278 if(_isatty(_fileno(stderr)))
280 UINT oldOutputCP = GetConsoleOutputCP();
281 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
287 set_terminal_color(stderr, COLOR_RED);
288 fprintf(stderr, GURU_MEDITATION);
289 fprintf(stderr, "%s\n", message);
293 set_terminal_color(stderr, COLOR_YELLOW);
294 fprintf(stderr, "%s\n", message);
298 set_terminal_color(stderr, COLOR_WHITE);
299 fprintf(stderr, "%s\n", message);
304 set_terminal_color(stderr, COLOR_DEFAULT);
305 if(oldOutputCP != CP_UTF8)
307 SetConsoleOutputCP(oldOutputCP);
312 void MUtils::Terminal::write(const int &type, const char *const message)
314 MUtils::Internal::CSLocker lock(g_terminal_lock);
316 if(g_terminal_attached)
318 write_terminal_helper(type, message);
322 write_debugger_helper(type, message);
325 if(!g_log_file.isNull())
327 write_logfile_helper(g_log_file.data(), type, message);