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;
49 ///////////////////////////////////////////////////////////////////////////////
51 ///////////////////////////////////////////////////////////////////////////////
53 static void make_timestamp(char *timestamp, const size_t &buffsize)
59 if(localtime_s(&timeinfo, &rawtime) == 0)
61 strftime(timestamp, 32, "%H:%M:%S", &timeinfo);
69 static const char *clean_str(char *str)
73 while((*ptr) && isspace(*ptr))
79 while((*str) && isspace(*str))
85 size_t pos = strlen(str);
88 if(isspace(str[--pos]))
99 ///////////////////////////////////////////////////////////////////////////////
101 ///////////////////////////////////////////////////////////////////////////////
103 static bool g_terminal_attached = false;
104 static QScopedPointer<std::filebuf> g_filebufStdOut;
105 static QScopedPointer<std::filebuf> g_filebufStdErr;
106 static QScopedPointer<QFile> g_log_file;
108 void MUtils::Terminal::setup(const QStringList &argv, const bool forceEnabled)
110 MUtils::Internal::CSLocker lock(g_terminal_lock);
111 bool enableConsole = (MUTILS_DEBUG) || forceEnabled;
115 wchar_t *logfile = NULL; size_t logfile_len = 0;
116 if(!_wdupenv_s(&logfile, &logfile_len, L"MUTILS_LOGFILE"))
118 if(logfile && (logfile_len > 0))
120 g_log_file.reset(new QFile(MUTILS_QSTR(logfile)));
121 if(g_log_file->open(QIODevice::WriteOnly))
123 static const char MARKER[3] = { char(0xEF), char(0xBB), char(0xBF) };
124 g_log_file->write(MARKER, 3);
133 for(int i = 0; i < argv.count(); i++)
135 if(!argv.at(i).compare("--console", Qt::CaseInsensitive))
137 enableConsole = true;
139 else if(!argv.at(i).compare("--no-console", Qt::CaseInsensitive))
141 enableConsole = false;
148 if(!g_terminal_attached)
150 if(AllocConsole() != FALSE)
152 SetConsoleCtrlHandler(NULL, TRUE);
153 SetConsoleTitle(L"LameXP - Audio Encoder Front-End | Debug Console");
154 SetConsoleOutputCP(CP_UTF8);
155 g_terminal_attached = true;
159 if(g_terminal_attached)
161 //-------------------------------------------------------------------
162 //See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
163 //-------------------------------------------------------------------
164 const int flags = _O_WRONLY | _O_U8TEXT;
165 const int hCrtStdOut = _open_osfhandle((intptr_t) GetStdHandle(STD_OUTPUT_HANDLE), flags);
166 const int hCrtStdErr = _open_osfhandle((intptr_t) GetStdHandle(STD_ERROR_HANDLE ), flags);
167 FILE *const hfStdOut = (hCrtStdOut >= 0) ? _fdopen(hCrtStdOut, "wb") : NULL;
168 FILE *const hfStdErr = (hCrtStdErr >= 0) ? _fdopen(hCrtStdErr, "wb") : NULL;
172 g_filebufStdOut.reset(new std::filebuf(hfStdOut));
173 std::cout.rdbuf(g_filebufStdOut.data());
178 g_filebufStdErr.reset(new std::filebuf(hfStdErr));
179 std::cerr.rdbuf(g_filebufStdErr.data());
183 HWND hwndConsole = GetConsoleWindow();
184 if((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
186 HMENU hMenu = GetSystemMenu(hwndConsole, 0);
187 EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
188 RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
190 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
191 SetWindowLong(hwndConsole, GWL_STYLE, GetWindowLong(hwndConsole, GWL_STYLE) & (~WS_MAXIMIZEBOX) & (~WS_MINIMIZEBOX));
192 SetWindowPos (hwndConsole, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
197 ///////////////////////////////////////////////////////////////////////////////
199 ///////////////////////////////////////////////////////////////////////////////
202 static const WORD COLOR_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
203 static const WORD COLOR_YELLOW = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
204 static const WORD COLOR_WHITE = FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
205 static const WORD COLOR_DEFAULT= FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED;
207 static void set_terminal_color(FILE* file, const WORD &attributes)
209 const HANDLE hConsole = (HANDLE)(_get_osfhandle(_fileno(file)));
210 if((hConsole != NULL) && (hConsole != INVALID_HANDLE_VALUE))
212 SetConsoleTextAttribute(hConsole, attributes);
216 ///////////////////////////////////////////////////////////////////////////////
218 ///////////////////////////////////////////////////////////////////////////////
220 static const char *const FORMAT = "[%c][%s] %s\r\n";
221 static const char *const GURU_MEDITATION = "\n\nGURU MEDITATION !!!\n\n";
223 static void write_logfile_helper(QFile *const file, const int &type, const char *const message)
225 static char input[1024], output[1024], timestamp[32];
226 make_timestamp(timestamp, 32);
227 strncpy_s(input, 1024, message, _TRUNCATE);
228 const char *const temp = clean_str(input);
234 _snprintf_s(output, 1024, FORMAT, 'C', timestamp, temp);
237 _snprintf_s(output, 1024, FORMAT, 'W', timestamp, temp);
240 _snprintf_s(output, 1024, FORMAT, 'I', timestamp, temp);
248 static void write_debugger_helper(const int &type, const char *const message)
250 static char input[1024], output[1024], timestamp[32];
251 make_timestamp(timestamp, 32);
252 strncpy_s(input, 1024, message, _TRUNCATE);
253 const char *const temp = clean_str(input);
259 _snprintf_s(output, 1024, FORMAT, 'C', timestamp, temp);
262 _snprintf_s(output, 1024, FORMAT, 'W', timestamp, temp);
265 _snprintf_s(output, 1024, FORMAT, 'I', timestamp, temp);
269 OutputDebugStringA(output);
272 static void write_terminal_helper(const int &type, const char *const message)
274 if(_isatty(_fileno(stderr)))
276 UINT oldOutputCP = GetConsoleOutputCP();
277 if(oldOutputCP != CP_UTF8) SetConsoleOutputCP(CP_UTF8);
283 set_terminal_color(stderr, COLOR_RED);
284 fprintf(stderr, GURU_MEDITATION);
285 fprintf(stderr, "%s\n", message);
289 set_terminal_color(stderr, COLOR_YELLOW);
290 fprintf(stderr, "%s\n", message);
294 set_terminal_color(stderr, COLOR_WHITE);
295 fprintf(stderr, "%s\n", message);
300 set_terminal_color(stderr, COLOR_DEFAULT);
301 if(oldOutputCP != CP_UTF8)
303 SetConsoleOutputCP(oldOutputCP);
308 void MUtils::Terminal::write(const int &type, const char *const message)
310 MUtils::Internal::CSLocker lock(g_terminal_lock);
312 if(g_terminal_attached)
314 write_terminal_helper(type, message);
318 write_debugger_helper(type, message);
321 if(!g_log_file.isNull())
323 write_logfile_helper(g_log_file.data(), type, message);