2012-11-01 15:19:01 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// 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
|
2012-11-04 22:01:49 +00:00
|
|
|
// the Free Software Foundation, version 2.0 or later versions.
|
2012-11-01 15:19:01 +00:00
|
|
|
|
|
|
|
// 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 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
|
|
|
|
|
|
|
|
|
|
|
#include <algorithm> // min
|
|
|
|
#include <string> // System: To be able to add strings with "+"
|
|
|
|
#include <math.h>
|
|
|
|
#ifdef _WIN32
|
2014-06-29 10:53:03 +00:00
|
|
|
#include <process.h>
|
2013-07-29 03:43:25 +00:00
|
|
|
#include "CommonWindows.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include <array>
|
|
|
|
#else
|
|
|
|
#include <stdarg.h>
|
|
|
|
#endif
|
|
|
|
|
2013-08-19 06:25:14 +00:00
|
|
|
#include "thread/threadutil.h"
|
2013-08-26 17:00:16 +00:00
|
|
|
#include "util/text/utf8.h"
|
2012-11-01 15:19:01 +00:00
|
|
|
#include "Common.h"
|
|
|
|
#include "ConsoleListener.h" // Common
|
2013-05-03 06:10:23 +00:00
|
|
|
#include "Atomics.h"
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2013-01-30 16:19:25 +00:00
|
|
|
const int LOG_PENDING_MAX = 120 * 10000;
|
2012-12-23 09:46:37 +00:00
|
|
|
const int LOG_LATENCY_DELAY_MS = 20;
|
|
|
|
const int LOG_SHUTDOWN_DELAY_MS = 250;
|
2013-01-30 06:33:51 +00:00
|
|
|
const int LOG_MAX_DISPLAY_LINES = 4000;
|
2013-06-02 05:18:21 +00:00
|
|
|
|
|
|
|
int ConsoleListener::refCount = 0;
|
|
|
|
HANDLE ConsoleListener::hThread = NULL;
|
|
|
|
HANDLE ConsoleListener::hTriggerEvent = NULL;
|
|
|
|
CRITICAL_SECTION ConsoleListener::criticalSection;
|
|
|
|
|
|
|
|
char *ConsoleListener::logPending = NULL;
|
|
|
|
volatile u32 ConsoleListener::logPendingReadPos = 0;
|
|
|
|
volatile u32 ConsoleListener::logPendingWritePos = 0;
|
2012-12-23 09:46:37 +00:00
|
|
|
#endif
|
2012-11-01 15:19:01 +00:00
|
|
|
|
2013-10-16 05:27:03 +00:00
|
|
|
ConsoleListener::ConsoleListener() : bHidden(true)
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
hConsole = NULL;
|
|
|
|
bUseColor = true;
|
2017-01-25 16:14:16 +00:00
|
|
|
if (hTriggerEvent == NULL) {
|
2013-06-02 05:18:21 +00:00
|
|
|
hTriggerEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|
|
|
InitializeCriticalSection(&criticalSection);
|
2013-10-16 05:27:03 +00:00
|
|
|
logPending = new char[LOG_PENDING_MAX];
|
2013-06-02 05:18:21 +00:00
|
|
|
}
|
|
|
|
++refCount;
|
2017-01-25 16:14:16 +00:00
|
|
|
#elif defined(ANDROID)
|
|
|
|
bUseColor = false;
|
|
|
|
#elif defined(IOS)
|
2013-12-03 17:09:16 +00:00
|
|
|
bUseColor = false;
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
|
|
|
bUseColor = isatty(fileno(stdout));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsoleListener::~ConsoleListener()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
2013-08-30 23:14:53 +00:00
|
|
|
#ifdef _WIN32
|
2013-08-29 21:40:50 +00:00
|
|
|
// Handle console event
|
|
|
|
bool WINAPI ConsoleHandler(DWORD msgType)
|
|
|
|
{
|
|
|
|
if (msgType == CTRL_C_EVENT)
|
|
|
|
{
|
|
|
|
OutputDebugString(L"Ctrl-C!\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
else if (msgType == CTRL_CLOSE_EVENT)
|
|
|
|
{
|
|
|
|
OutputDebugString(L"Close console window!\n");
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Other messages:
|
|
|
|
CTRL_BREAK_EVENT Ctrl-Break pressed
|
|
|
|
CTRL_LOGOFF_EVENT User log off
|
|
|
|
CTRL_SHUTDOWN_EVENT System shutdown
|
|
|
|
*/
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-08-30 23:14:53 +00:00
|
|
|
#endif
|
2013-08-29 21:40:50 +00:00
|
|
|
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// 100, 100, "Dolphin Log Console"
|
|
|
|
// Open console window - width and height is the size of console window
|
|
|
|
// Name is the window title
|
2013-10-16 05:27:03 +00:00
|
|
|
void ConsoleListener::Init(bool AutoOpen, int Width, int Height, const char *Title)
|
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2013-10-16 05:27:03 +00:00
|
|
|
openWidth_ = Width;
|
|
|
|
openHeight_ = Height;
|
|
|
|
title_ = ConvertUTF8ToWString(Title);
|
|
|
|
|
|
|
|
if (AutoOpen)
|
|
|
|
Open();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleListener::Open()
|
2012-11-01 15:19:01 +00:00
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
if (!GetConsoleWindow())
|
|
|
|
{
|
|
|
|
// Open the console window and create the window handle for GetStdHandle()
|
|
|
|
AllocConsole();
|
2015-12-26 06:43:49 +00:00
|
|
|
hWnd = GetConsoleWindow();
|
|
|
|
ShowWindow(hWnd, SW_SHOWDEFAULT);
|
2013-08-29 21:40:50 +00:00
|
|
|
// disable console close button
|
2015-12-26 06:43:49 +00:00
|
|
|
HMENU hMenu=GetSystemMenu(hWnd, false);
|
2013-08-29 21:40:50 +00:00
|
|
|
EnableMenuItem(hMenu,SC_CLOSE,MF_GRAYED|MF_BYCOMMAND);
|
2012-11-01 15:19:01 +00:00
|
|
|
// Save the window handle that AllocConsole() created
|
|
|
|
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
2013-08-29 21:40:50 +00:00
|
|
|
// Set console handler
|
|
|
|
if(SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE)){OutputDebugString(L"Console handler is installed!\n");}
|
2012-11-01 15:19:01 +00:00
|
|
|
// Set the console window title
|
2013-10-16 05:27:03 +00:00
|
|
|
SetConsoleTitle(title_.c_str());
|
2013-08-26 17:00:16 +00:00
|
|
|
SetConsoleCP(CP_UTF8);
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
// Set letter space
|
2013-10-16 05:27:03 +00:00
|
|
|
LetterSpace(openWidth_, LOG_MAX_DISPLAY_LINES);
|
2012-11-01 15:19:01 +00:00
|
|
|
//MoveWindow(GetConsoleWindow(), 200,200, 800,800, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
}
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2013-06-02 05:18:21 +00:00
|
|
|
if (hTriggerEvent != NULL && hThread == NULL)
|
2013-06-08 00:32:07 +00:00
|
|
|
hThread = (HANDLE)_beginthreadex(NULL, 0, &ConsoleListener::RunThread, this, 0, NULL);
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleListener::Show(bool bShow)
|
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2013-10-16 05:27:03 +00:00
|
|
|
if (bShow && bHidden)
|
|
|
|
{
|
|
|
|
if (!IsOpen())
|
|
|
|
Open();
|
|
|
|
ShowWindow(GetConsoleWindow(), SW_SHOW);
|
|
|
|
bHidden = false;
|
|
|
|
}
|
|
|
|
else if (!bShow && !bHidden)
|
|
|
|
{
|
|
|
|
ShowWindow(GetConsoleWindow(), SW_HIDE);
|
|
|
|
bHidden = true;
|
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConsoleListener::UpdateHandle()
|
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the console window and close the eventual file handle
|
|
|
|
void ConsoleListener::Close()
|
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
if (hConsole == NULL)
|
|
|
|
return;
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2013-06-02 05:18:21 +00:00
|
|
|
if (--refCount <= 0)
|
2012-12-23 09:46:37 +00:00
|
|
|
{
|
2013-06-02 05:18:21 +00:00
|
|
|
if (hThread != NULL)
|
|
|
|
{
|
|
|
|
Common::AtomicStoreRelease(logPendingWritePos, (u32) -1);
|
|
|
|
|
|
|
|
SetEvent(hTriggerEvent);
|
|
|
|
WaitForSingleObject(hThread, LOG_SHUTDOWN_DELAY_MS);
|
|
|
|
CloseHandle(hThread);
|
|
|
|
hThread = NULL;
|
|
|
|
}
|
|
|
|
if (hTriggerEvent != NULL)
|
|
|
|
{
|
|
|
|
DeleteCriticalSection(&criticalSection);
|
|
|
|
CloseHandle(hTriggerEvent);
|
|
|
|
hTriggerEvent = NULL;
|
|
|
|
}
|
|
|
|
if (logPending != NULL)
|
|
|
|
{
|
|
|
|
delete [] logPending;
|
|
|
|
logPending = NULL;
|
|
|
|
}
|
|
|
|
refCount = 0;
|
2012-12-23 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
FreeConsole();
|
|
|
|
hConsole = NULL;
|
|
|
|
#else
|
|
|
|
fflush(NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConsoleListener::IsOpen()
|
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
return (hConsole != NULL);
|
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
LetterSpace: SetConsoleScreenBufferSize and SetConsoleWindowInfo are
|
|
|
|
dependent on each other, that's the reason for the additional checks.
|
|
|
|
*/
|
|
|
|
void ConsoleListener::BufferWidthHeight(int BufferWidth, int BufferHeight, int ScreenWidth, int ScreenHeight, bool BufferFirst)
|
|
|
|
{
|
2013-10-16 05:27:03 +00:00
|
|
|
_dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console.");
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
BOOL SB, SW;
|
|
|
|
if (BufferFirst)
|
|
|
|
{
|
|
|
|
// Change screen buffer size
|
2015-08-30 13:36:53 +00:00
|
|
|
COORD Co = {(SHORT)BufferWidth, (SHORT)BufferHeight};
|
2012-11-01 15:19:01 +00:00
|
|
|
SB = SetConsoleScreenBufferSize(hConsole, Co);
|
|
|
|
// Change the screen buffer window size
|
2015-09-05 17:42:58 +00:00
|
|
|
SMALL_RECT coo = {(SHORT)0, (SHORT)0, (SHORT)ScreenWidth, (SHORT)ScreenHeight}; // top, left, right, bottom
|
2012-11-01 15:19:01 +00:00
|
|
|
SW = SetConsoleWindowInfo(hConsole, TRUE, &coo);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Change the screen buffer window size
|
2015-08-30 13:36:53 +00:00
|
|
|
SMALL_RECT coo = {(SHORT)0, (SHORT)0, (SHORT)ScreenWidth, (SHORT)ScreenHeight}; // top, left, right, bottom
|
2012-11-01 15:19:01 +00:00
|
|
|
SW = SetConsoleWindowInfo(hConsole, TRUE, &coo);
|
|
|
|
// Change screen buffer size
|
2015-09-05 17:42:58 +00:00
|
|
|
COORD Co = {(SHORT)BufferWidth, (SHORT)BufferHeight};
|
2012-11-01 15:19:01 +00:00
|
|
|
SB = SetConsoleScreenBufferSize(hConsole, Co);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
void ConsoleListener::LetterSpace(int Width, int Height)
|
|
|
|
{
|
2013-10-16 05:27:03 +00:00
|
|
|
_dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console.");
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
// Get console info
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO ConInfo;
|
|
|
|
GetConsoleScreenBufferInfo(hConsole, &ConInfo);
|
|
|
|
|
|
|
|
//
|
|
|
|
int OldBufferWidth = ConInfo.dwSize.X;
|
|
|
|
int OldBufferHeight = ConInfo.dwSize.Y;
|
|
|
|
int OldScreenWidth = (ConInfo.srWindow.Right - ConInfo.srWindow.Left);
|
|
|
|
int OldScreenHeight = (ConInfo.srWindow.Bottom - ConInfo.srWindow.Top);
|
|
|
|
//
|
|
|
|
int NewBufferWidth = Width;
|
|
|
|
int NewBufferHeight = Height;
|
|
|
|
int NewScreenWidth = NewBufferWidth - 1;
|
|
|
|
int NewScreenHeight = OldScreenHeight;
|
|
|
|
|
|
|
|
// Width
|
|
|
|
BufferWidthHeight(NewBufferWidth, OldBufferHeight, NewScreenWidth, OldScreenHeight, (NewBufferWidth > OldScreenWidth-1));
|
|
|
|
// Height
|
|
|
|
BufferWidthHeight(NewBufferWidth, NewBufferHeight, NewScreenWidth, NewScreenHeight, (NewBufferHeight > OldScreenHeight-1));
|
|
|
|
|
|
|
|
// Resize the window too
|
|
|
|
//MoveWindow(GetConsoleWindow(), 200,200, (Width*8 + 50),(NewScreenHeight*12 + 200), true);
|
|
|
|
#endif
|
|
|
|
}
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
COORD ConsoleListener::GetCoordinates(int BytesRead, int BufferWidth)
|
|
|
|
{
|
|
|
|
COORD Ret = {0, 0};
|
|
|
|
// Full rows
|
|
|
|
int Step = (int)floor((float)BytesRead / (float)BufferWidth);
|
|
|
|
Ret.Y += Step;
|
|
|
|
// Partial row
|
|
|
|
Ret.X = BytesRead - (BufferWidth * Step);
|
|
|
|
return Ret;
|
|
|
|
}
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2013-06-08 00:32:07 +00:00
|
|
|
unsigned int WINAPI ConsoleListener::RunThread(void *lpParam)
|
2012-12-23 09:46:37 +00:00
|
|
|
{
|
2014-06-29 10:53:03 +00:00
|
|
|
setCurrentThreadName("Console");
|
2013-06-08 00:32:07 +00:00
|
|
|
ConsoleListener *consoleLog = (ConsoleListener *)lpParam;
|
2012-12-23 09:46:37 +00:00
|
|
|
consoleLog->LogWriterThread();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleListener::LogWriterThread()
|
|
|
|
{
|
2013-01-30 16:19:25 +00:00
|
|
|
char *logLocal = new char[LOG_PENDING_MAX];
|
2012-12-23 09:46:37 +00:00
|
|
|
int logLocalSize = 0;
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
WaitForSingleObject(hTriggerEvent, INFINITE);
|
|
|
|
Sleep(LOG_LATENCY_DELAY_MS);
|
|
|
|
|
2013-01-30 07:13:16 +00:00
|
|
|
u32 logRemotePos = Common::AtomicLoadAcquire(logPendingWritePos);
|
|
|
|
if (logRemotePos == (u32) -1)
|
2012-12-23 09:46:37 +00:00
|
|
|
break;
|
2013-01-30 07:13:16 +00:00
|
|
|
else if (logRemotePos == logPendingReadPos)
|
2012-12-23 09:46:37 +00:00
|
|
|
continue;
|
2013-01-30 07:13:16 +00:00
|
|
|
else
|
2012-12-23 09:46:37 +00:00
|
|
|
{
|
|
|
|
EnterCriticalSection(&criticalSection);
|
2013-01-30 07:13:16 +00:00
|
|
|
logRemotePos = Common::AtomicLoadAcquire(logPendingWritePos);
|
|
|
|
|
|
|
|
int start = 0;
|
|
|
|
if (logRemotePos < logPendingReadPos)
|
|
|
|
{
|
|
|
|
const int count = LOG_PENDING_MAX - logPendingReadPos;
|
2013-01-30 16:19:25 +00:00
|
|
|
memcpy(logLocal + start, logPending + logPendingReadPos, count);
|
2013-01-30 07:13:16 +00:00
|
|
|
|
|
|
|
start = count;
|
|
|
|
logPendingReadPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int count = logRemotePos - logPendingReadPos;
|
2013-01-30 16:19:25 +00:00
|
|
|
memcpy(logLocal + start, logPending + logPendingReadPos, count);
|
2013-01-30 07:13:16 +00:00
|
|
|
|
2013-02-01 05:55:36 +00:00
|
|
|
logPendingReadPos += count;
|
2012-12-23 09:46:37 +00:00
|
|
|
LeaveCriticalSection(&criticalSection);
|
2013-01-30 07:13:16 +00:00
|
|
|
|
|
|
|
// Double check.
|
|
|
|
if (logPendingWritePos == (u32) -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
logLocalSize = start + count;
|
2012-12-23 09:46:37 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 16:19:25 +00:00
|
|
|
for (char *Text = logLocal, *End = logLocal + logLocalSize; Text < End; )
|
2013-01-30 06:33:51 +00:00
|
|
|
{
|
2013-01-30 16:19:25 +00:00
|
|
|
LogTypes::LOG_LEVELS Level = LogTypes::LINFO;
|
2013-01-30 06:33:51 +00:00
|
|
|
|
2013-01-30 16:19:25 +00:00
|
|
|
char *next = (char *) memchr(Text + 1, '\033', End - Text);
|
|
|
|
size_t Len = next - Text;
|
|
|
|
if (next == NULL)
|
|
|
|
Len = End - Text;
|
|
|
|
|
|
|
|
if (Text[0] == '\033' && Text + 1 < End)
|
|
|
|
{
|
|
|
|
Level = (LogTypes::LOG_LEVELS) (Text[1] - '0');
|
|
|
|
Len -= 2;
|
|
|
|
Text += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we didn't start quitting. This is kinda slow.
|
|
|
|
if (logPendingWritePos == (u32) -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
WriteToConsole(Level, Text, Len);
|
|
|
|
Text += Len;
|
2012-12-23 09:46:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] logLocal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleListener::SendToThread(LogTypes::LOG_LEVELS Level, const char *Text)
|
|
|
|
{
|
2013-01-30 06:33:51 +00:00
|
|
|
// Oops, we're already quitting. Just do nothing.
|
2013-01-30 07:13:16 +00:00
|
|
|
if (logPendingWritePos == (u32) -1)
|
2013-01-30 06:33:51 +00:00
|
|
|
return;
|
|
|
|
|
2013-02-24 18:38:30 +00:00
|
|
|
int Len = (int)strlen(Text);
|
2013-02-01 06:25:28 +00:00
|
|
|
if (Len > LOG_PENDING_MAX)
|
|
|
|
Len = LOG_PENDING_MAX - 16;
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2013-01-30 16:19:25 +00:00
|
|
|
char ColorAttr[16] = "";
|
2013-02-24 18:38:30 +00:00
|
|
|
int ColorLen = 0;
|
2013-01-30 16:19:25 +00:00
|
|
|
if (bUseColor)
|
|
|
|
{
|
|
|
|
// Not ANSI, since the console doesn't support it, but ANSI-like.
|
|
|
|
snprintf(ColorAttr, 16, "\033%d", Level);
|
|
|
|
// For now, rather than properly support it.
|
|
|
|
_dbg_assert_msg_(COMMON, strlen(ColorAttr) == 2, "Console logging doesn't support > 9 levels.");
|
2013-02-24 18:38:30 +00:00
|
|
|
ColorLen = (int)strlen(ColorAttr);
|
2013-01-30 16:19:25 +00:00
|
|
|
}
|
2012-12-23 09:46:37 +00:00
|
|
|
|
|
|
|
EnterCriticalSection(&criticalSection);
|
2013-01-30 16:19:25 +00:00
|
|
|
u32 logWritePos = Common::AtomicLoad(logPendingWritePos);
|
2013-01-31 05:20:26 +00:00
|
|
|
u32 prevLogWritePos = logWritePos;
|
2013-02-01 06:25:28 +00:00
|
|
|
if (logWritePos + ColorLen + Len >= LOG_PENDING_MAX)
|
2013-01-30 16:19:25 +00:00
|
|
|
{
|
2013-02-24 18:38:30 +00:00
|
|
|
for (int i = 0; i < ColorLen; ++i)
|
2013-02-01 06:25:28 +00:00
|
|
|
logPending[(logWritePos + i) % LOG_PENDING_MAX] = ColorAttr[i];
|
|
|
|
logWritePos += ColorLen;
|
|
|
|
if (logWritePos >= LOG_PENDING_MAX)
|
|
|
|
logWritePos -= LOG_PENDING_MAX;
|
2013-01-30 16:19:25 +00:00
|
|
|
|
|
|
|
int start = 0;
|
2013-02-01 06:58:16 +00:00
|
|
|
if (logWritePos < LOG_PENDING_MAX && logWritePos + Len >= LOG_PENDING_MAX)
|
2013-01-30 16:19:25 +00:00
|
|
|
{
|
|
|
|
const int count = LOG_PENDING_MAX - logWritePos;
|
2013-02-01 06:25:28 +00:00
|
|
|
memcpy(logPending + logWritePos, Text, count);
|
2013-01-30 16:19:25 +00:00
|
|
|
start = count;
|
|
|
|
logWritePos = 0;
|
|
|
|
}
|
|
|
|
const int count = Len - start;
|
2013-02-01 06:25:28 +00:00
|
|
|
if (count > 0)
|
|
|
|
{
|
|
|
|
memcpy(logPending + logWritePos, Text + start, count);
|
|
|
|
logWritePos += count;
|
|
|
|
}
|
2013-01-30 16:19:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-02-01 06:25:28 +00:00
|
|
|
memcpy(logPending + logWritePos, ColorAttr, ColorLen);
|
|
|
|
memcpy(logPending + logWritePos + ColorLen, Text, Len);
|
|
|
|
logWritePos += ColorLen + Len;
|
2013-01-31 05:20:26 +00:00
|
|
|
}
|
2013-01-30 16:19:25 +00:00
|
|
|
|
2013-01-31 05:20:26 +00:00
|
|
|
// Oops, we passed the read pos.
|
|
|
|
if (prevLogWritePos < logPendingReadPos && logWritePos >= logPendingReadPos)
|
|
|
|
{
|
|
|
|
char *nextNewline = (char *) memchr(logPending + logWritePos, '\n', LOG_PENDING_MAX - logWritePos);
|
|
|
|
if (nextNewline == NULL && logWritePos > 0)
|
|
|
|
nextNewline = (char *) memchr(logPending, '\n', logWritePos);
|
2013-01-30 16:19:25 +00:00
|
|
|
|
2013-01-31 05:20:26 +00:00
|
|
|
// Okay, have it go right after the next newline.
|
|
|
|
if (nextNewline != NULL)
|
2013-02-24 18:38:30 +00:00
|
|
|
logPendingReadPos = (u32)(nextNewline - logPending + 1);
|
2013-01-30 16:19:25 +00:00
|
|
|
}
|
2013-01-31 05:20:26 +00:00
|
|
|
|
|
|
|
// Double check we didn't start quitting.
|
2016-03-20 21:16:54 +00:00
|
|
|
if (logPendingWritePos == (u32) -1) {
|
|
|
|
LeaveCriticalSection(&criticalSection);
|
2013-01-31 05:20:26 +00:00
|
|
|
return;
|
2016-03-20 21:16:54 +00:00
|
|
|
}
|
2013-01-31 05:20:26 +00:00
|
|
|
|
|
|
|
Common::AtomicStoreRelease(logPendingWritePos, logWritePos);
|
2012-12-23 09:46:37 +00:00
|
|
|
LeaveCriticalSection(&criticalSection);
|
|
|
|
|
|
|
|
SetEvent(hTriggerEvent);
|
|
|
|
}
|
|
|
|
|
2013-01-30 16:19:25 +00:00
|
|
|
void ConsoleListener::WriteToConsole(LogTypes::LOG_LEVELS Level, const char *Text, size_t Len)
|
2012-12-23 09:46:37 +00:00
|
|
|
{
|
2013-10-16 05:27:03 +00:00
|
|
|
_dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console.");
|
|
|
|
|
2012-12-23 09:46:37 +00:00
|
|
|
/*
|
|
|
|
const int MAX_BYTES = 1024*10;
|
|
|
|
char Str[MAX_BYTES];
|
|
|
|
va_list ArgPtr;
|
|
|
|
int Cnt;
|
|
|
|
va_start(ArgPtr, Text);
|
|
|
|
Cnt = vsnprintf(Str, MAX_BYTES, Text, ArgPtr);
|
|
|
|
va_end(ArgPtr);
|
|
|
|
*/
|
|
|
|
DWORD cCharsWritten;
|
|
|
|
WORD Color;
|
2013-08-26 17:00:16 +00:00
|
|
|
static wchar_t tempBuf[2048];
|
2012-12-23 09:46:37 +00:00
|
|
|
|
|
|
|
switch (Level)
|
|
|
|
{
|
|
|
|
case NOTICE_LEVEL: // light green
|
|
|
|
Color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
|
|
|
case ERROR_LEVEL: // light red
|
|
|
|
Color = FOREGROUND_RED | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
|
|
|
case WARNING_LEVEL: // light yellow
|
|
|
|
Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
|
|
|
case INFO_LEVEL: // cyan
|
|
|
|
Color = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
|
|
|
|
break;
|
|
|
|
case DEBUG_LEVEL: // gray
|
|
|
|
Color = FOREGROUND_INTENSITY;
|
|
|
|
break;
|
|
|
|
default: // off-white
|
|
|
|
Color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
|
|
|
break;
|
|
|
|
}
|
2013-01-30 16:19:25 +00:00
|
|
|
if (Len > 10)
|
2012-12-23 09:46:37 +00:00
|
|
|
{
|
|
|
|
// First 10 chars white
|
|
|
|
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
|
2013-08-26 17:00:16 +00:00
|
|
|
int wlen = MultiByteToWideChar(CP_UTF8, 0, Text, (int)Len, NULL, NULL);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, Text, (int)Len, tempBuf, wlen);
|
|
|
|
WriteConsole(hConsole, tempBuf, 10, &cCharsWritten, NULL);
|
2012-12-23 09:46:37 +00:00
|
|
|
Text += 10;
|
2013-01-30 16:19:25 +00:00
|
|
|
Len -= 10;
|
2012-12-23 09:46:37 +00:00
|
|
|
}
|
|
|
|
SetConsoleTextAttribute(hConsole, Color);
|
2013-08-26 17:00:16 +00:00
|
|
|
int wlen = MultiByteToWideChar(CP_UTF8, 0, Text, (int)Len, NULL, NULL);
|
|
|
|
MultiByteToWideChar(CP_UTF8, 0, Text, (int)Len, tempBuf, wlen);
|
|
|
|
WriteConsole(hConsole, tempBuf, (DWORD)Len, &cCharsWritten, NULL);
|
2012-12-23 09:46:37 +00:00
|
|
|
}
|
2012-11-01 15:19:01 +00:00
|
|
|
#endif
|
2012-12-23 09:46:37 +00:00
|
|
|
|
2012-11-01 15:19:01 +00:00
|
|
|
void ConsoleListener::PixelSpace(int Left, int Top, int Width, int Height, bool Resize)
|
|
|
|
{
|
2013-10-16 05:27:03 +00:00
|
|
|
_dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console.");
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
// Check size
|
|
|
|
if (Width < 8 || Height < 12) return;
|
|
|
|
|
|
|
|
bool DBef = true;
|
|
|
|
bool DAft = true;
|
|
|
|
std::string SLog = "";
|
|
|
|
|
|
|
|
// Get console info
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO ConInfo;
|
|
|
|
GetConsoleScreenBufferInfo(hConsole, &ConInfo);
|
|
|
|
DWORD BufferSize = ConInfo.dwSize.X * ConInfo.dwSize.Y;
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------
|
|
|
|
// Save the current text
|
|
|
|
// ------------------------
|
|
|
|
DWORD cCharsRead = 0;
|
|
|
|
COORD coordScreen = { 0, 0 };
|
|
|
|
|
|
|
|
static const int MAX_BYTES = 1024 * 16;
|
|
|
|
|
2013-08-26 17:00:16 +00:00
|
|
|
std::vector<std::array<wchar_t, MAX_BYTES>> Str;
|
2012-11-01 15:19:01 +00:00
|
|
|
std::vector<std::array<WORD, MAX_BYTES>> Attr;
|
|
|
|
|
|
|
|
// ReadConsoleOutputAttribute seems to have a limit at this level
|
|
|
|
static const int ReadBufferSize = MAX_BYTES - 32;
|
|
|
|
|
|
|
|
DWORD cAttrRead = ReadBufferSize;
|
|
|
|
DWORD BytesRead = 0;
|
|
|
|
while (BytesRead < BufferSize)
|
|
|
|
{
|
|
|
|
Str.resize(Str.size() + 1);
|
|
|
|
if (!ReadConsoleOutputCharacter(hConsole, Str.back().data(), ReadBufferSize, coordScreen, &cCharsRead))
|
|
|
|
SLog += StringFromFormat("WriteConsoleOutputCharacter error");
|
|
|
|
|
|
|
|
Attr.resize(Attr.size() + 1);
|
|
|
|
if (!ReadConsoleOutputAttribute(hConsole, Attr.back().data(), ReadBufferSize, coordScreen, &cAttrRead))
|
|
|
|
SLog += StringFromFormat("WriteConsoleOutputAttribute error");
|
|
|
|
|
|
|
|
// Break on error
|
|
|
|
if (cAttrRead == 0) break;
|
|
|
|
BytesRead += cAttrRead;
|
|
|
|
coordScreen = GetCoordinates(BytesRead, ConInfo.dwSize.X);
|
|
|
|
}
|
|
|
|
// Letter space
|
|
|
|
int LWidth = (int)(floor((float)Width / 8.0f) - 1.0f);
|
|
|
|
int LHeight = (int)(floor((float)Height / 12.0f) - 1.0f);
|
|
|
|
int LBufWidth = LWidth + 1;
|
|
|
|
int LBufHeight = (int)floor((float)BufferSize / (float)LBufWidth);
|
|
|
|
// Change screen buffer size
|
|
|
|
LetterSpace(LBufWidth, LBufHeight);
|
|
|
|
|
|
|
|
|
|
|
|
ClearScreen(true);
|
|
|
|
coordScreen.Y = 0;
|
|
|
|
coordScreen.X = 0;
|
|
|
|
DWORD cCharsWritten = 0;
|
|
|
|
|
|
|
|
int BytesWritten = 0;
|
|
|
|
DWORD cAttrWritten = 0;
|
|
|
|
for (size_t i = 0; i < Attr.size(); i++)
|
|
|
|
{
|
|
|
|
if (!WriteConsoleOutputCharacter(hConsole, Str[i].data(), ReadBufferSize, coordScreen, &cCharsWritten))
|
|
|
|
SLog += StringFromFormat("WriteConsoleOutputCharacter error");
|
|
|
|
if (!WriteConsoleOutputAttribute(hConsole, Attr[i].data(), ReadBufferSize, coordScreen, &cAttrWritten))
|
|
|
|
SLog += StringFromFormat("WriteConsoleOutputAttribute error");
|
|
|
|
|
|
|
|
BytesWritten += cAttrWritten;
|
|
|
|
coordScreen = GetCoordinates(BytesWritten, LBufWidth);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int OldCursor = ConInfo.dwCursorPosition.Y * ConInfo.dwSize.X + ConInfo.dwCursorPosition.X;
|
|
|
|
COORD Coo = GetCoordinates(OldCursor, LBufWidth);
|
|
|
|
SetConsoleCursorPosition(hConsole, Coo);
|
|
|
|
|
|
|
|
if (SLog.length() > 0) Log(LogTypes::LNOTICE, SLog.c_str());
|
|
|
|
|
|
|
|
// Resize the window too
|
|
|
|
if (Resize) MoveWindow(GetConsoleWindow(), Left,Top, (Width + 100),Height, true);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConsoleListener::Log(LogTypes::LOG_LEVELS Level, const char *Text)
|
|
|
|
{
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2013-10-16 05:27:03 +00:00
|
|
|
if (hThread == NULL && IsOpen())
|
2013-01-30 16:19:25 +00:00
|
|
|
WriteToConsole(Level, Text, strlen(Text));
|
2012-12-23 09:46:37 +00:00
|
|
|
else
|
|
|
|
SendToThread(Level, Text);
|
2012-11-01 15:19:01 +00:00
|
|
|
#else
|
|
|
|
char ColorAttr[16] = "";
|
|
|
|
char ResetAttr[16] = "";
|
|
|
|
|
|
|
|
if (bUseColor)
|
|
|
|
{
|
|
|
|
strcpy(ResetAttr, "\033[0m");
|
|
|
|
switch (Level)
|
|
|
|
{
|
|
|
|
case NOTICE_LEVEL: // light green
|
|
|
|
strcpy(ColorAttr, "\033[92m");
|
|
|
|
break;
|
|
|
|
case ERROR_LEVEL: // light red
|
|
|
|
strcpy(ColorAttr, "\033[91m");
|
|
|
|
break;
|
|
|
|
case WARNING_LEVEL: // light yellow
|
|
|
|
strcpy(ColorAttr, "\033[93m");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stderr, "%s%s%s", ColorAttr, Text, ResetAttr);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
// Clear console screen
|
|
|
|
void ConsoleListener::ClearScreen(bool Cursor)
|
|
|
|
{
|
2013-10-16 05:27:03 +00:00
|
|
|
_dbg_assert_msg_(COMMON, IsOpen(), "Don't call this before opening the console.");
|
2016-01-03 23:10:50 +00:00
|
|
|
#if defined(USING_WIN_UI)
|
2012-11-01 15:19:01 +00:00
|
|
|
COORD coordScreen = { 0, 0 };
|
|
|
|
DWORD cCharsWritten;
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
|
|
|
DWORD dwConSize;
|
|
|
|
|
|
|
|
GetConsoleScreenBufferInfo(hConsole, &csbi);
|
|
|
|
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
|
|
|
|
// Write space to the entire console
|
|
|
|
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
|
|
|
|
GetConsoleScreenBufferInfo(hConsole, &csbi);
|
|
|
|
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
|
|
|
|
// Reset cursor
|
|
|
|
if (Cursor) SetConsoleCursorPosition(hConsole, coordScreen);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|