From 190f66186f7d79d4c64e8f2a14554f2090da57c5 Mon Sep 17 00:00:00 2001 From: arcum42 Date: Sun, 8 Nov 2009 04:32:15 +0000 Subject: [PATCH] Change the console functions so that the terminal actually has colored text in Linux. (And doesn't have blue underlined text for most of FFX...) git-svn-id: http://pcsx2.googlecode.com/svn/trunk@2159 96395faa-99c1-11dd-bbfe-3dabce05a288 --- common/src/Utilities/Console.cpp | 85 +++++++++++++++++++++++++++++--- pcsx2/gui/ConsoleLogger.cpp | 54 ++++++++++++++------ 2 files changed, 115 insertions(+), 24 deletions(-) diff --git a/common/src/Utilities/Console.cpp b/common/src/Utilities/Console.cpp index 384c303049..7532ec044a 100644 --- a/common/src/Utilities/Console.cpp +++ b/common/src/Utilities/Console.cpp @@ -1,6 +1,6 @@ /* PCSX2 - PS2 Emulator for PCs * Copyright (C) 2002-2009 PCSX2 Dev Team - * + * * PCSX2 is free software: you can redistribute it and/or modify it under the terms * of the GNU Lesser General Public License as published by the Free Software Found- * ation, either version 3 of the License, or (at your option) any later version. @@ -33,7 +33,7 @@ void Console_SetActiveHandler( const IConsoleWriter& writer, FILE* flushfp ) (writer.SetColor != NULL) && (writer.ClearColor != NULL), "Invalid IConsoleWriter object! All function pointer interfaces must be implemented." ); - + if( !ConsoleBuffer_Get().IsEmpty() ) writer.DoWriteLn( ConsoleBuffer_Get() ); @@ -75,6 +75,29 @@ const IConsoleWriter ConsoleWriter_Null = // Console_Stdio // -------------------------------------------------------------------------------------- +#ifdef __LINUX__ +static wxString tbl_color_codes[] = +{ + L"\033[30m" // black + , L"\033[31m" // red + , L"\033[32m" // green + , L"\033[33m" // yellow + , L"\033[34m" // blue + , L"\033[35m" // magenta + , L"\033[36m" // cyan + , L"\033[37m" // white! + , L"\033[30m\033[1m" // strong black + , L"\033[31m\033[1m" // strong red + , L"\033[32m\033[1m" // strong green + , L"\033[33m\033[1m" // strong yellow + , L"\033[34m\033[1m" // strong blue + , L"\033[35m\033[1m" // strong magenta + , L"\033[36m\033[1m" // strong cyan + , L"\033[37m\033[1m" // strong white! + , L"\033[0m" // Back to default. +}; +#endif + // One possible default write action at startup and shutdown is to use the stdout. static void __concall ConsoleStdio_DoWrite( const wxString& fmt ) { @@ -87,16 +110,62 @@ static void __concall ConsoleStdio_DoWriteLn( const wxString& fmt ) wxPrintf( fmt + L"\n" ); } +static void __concall ConsoleStdio_Newline() +{ + wxPrintf( L"\n" ); +} + +static void __concall ConsoleStdio_SetColor( ConsoleColors color ) +{ +#ifdef __LINUX__ + wxPrintf(tbl_color_codes[16]); + switch (color) + { + case Color_Black: wxPrintf(tbl_color_codes[0]); break; + case Color_Green: wxPrintf(tbl_color_codes[2]); break; + case Color_Red: wxPrintf(tbl_color_codes[1]); break; + case Color_Blue: wxPrintf(tbl_color_codes[4]); break; + case Color_Magenta: wxPrintf(tbl_color_codes[5]); break; + case Color_Orange: wxPrintf(tbl_color_codes[5]); break; // No orange, so use magenta. + case Color_Gray: wxPrintf(tbl_color_codes[7]); break; // Use white instead of grey. + case Color_Cyan: wxPrintf(tbl_color_codes[6]); break; + case Color_Yellow: wxPrintf(tbl_color_codes[3]); break; + case Color_White: wxPrintf(tbl_color_codes[7]); break; + case Color_StrongBlack: wxPrintf(tbl_color_codes[8]); break; + case Color_StrongRed: wxPrintf(tbl_color_codes[9]); break; + case Color_StrongGreen: wxPrintf(tbl_color_codes[10]); break; + case Color_StrongBlue: wxPrintf(tbl_color_codes[12]); break; + case Color_StrongOrange: wxPrintf(tbl_color_codes[13]); break; // strong magenta. + default: wxPrintf(tbl_color_codes[16]); break; + } +#endif +} + + +static void __concall ConsoleStdio_SetTitle( const wxString& title ) +{ +#ifdef __LINUX__ + wxPrintf(L"\033]0;" + title + L"\007"); +#endif +} + +static void __concall ConsoleStdio_ClearColor() +{ +#ifdef __LINUX__ + wxPrintf(tbl_color_codes[17]); +#endif +} + const IConsoleWriter ConsoleWriter_Stdio = { ConsoleStdio_DoWrite, // Writes without newlines go to buffer to avoid error log spam. ConsoleStdio_DoWriteLn, - ConsoleNull_Newline, + ConsoleStdio_Newline, - ConsoleNull_SetTitle, - ConsoleNull_SetColor, - ConsoleNull_ClearColor, + ConsoleStdio_SetTitle, + ConsoleStdio_SetColor, + ConsoleStdio_ClearColor, }; // -------------------------------------------------------------------------------------- @@ -209,7 +278,7 @@ class FormatBuffer : public Mutex public: bool& clearbit; SafeArray buffer; - + FormatBuffer( bool& bit_to_clear_on_destruction ) : clearbit( bit_to_clear_on_destruction ) , buffer( 4096, wxsFormat( L"%s Format Buffer", (sizeof(CharType)==1) ? "Ascii" : "Unicode" ) ) @@ -221,7 +290,7 @@ public: clearbit = true; Wait(); // lock the mutex, just in case. } - + }; static bool ascii_buffer_is_deleted = false; diff --git a/pcsx2/gui/ConsoleLogger.cpp b/pcsx2/gui/ConsoleLogger.cpp index cabcb5c49b..144009db87 100644 --- a/pcsx2/gui/ConsoleLogger.cpp +++ b/pcsx2/gui/ConsoleLogger.cpp @@ -638,18 +638,19 @@ void Pcsx2App::ProgramLog_PostEvent( wxEvent& evt ) // -------------------------------------------------------------------------------------- // ConsoleImpl_ToFile // -------------------------------------------------------------------------------------- -static void __concall _immediate_logger( const char* src ) + +static void __concall _immediate_logger( wxString src ) { #ifdef __LINUX__ - fputs( src, stdout ); + ConsoleWriter_Stdio.DoWrite(src); #endif - px_fputs( emuLog, src ); + px_fputs( emuLog, src.ToUTF8() ); } static void __concall ConsoleToFile_Newline() { #ifdef __LINUX__ - fputc( '\n', stdout ); + ConsoleWriter_Stdio.Newline(); fputc( '\n', emuLog ); #else fputs( "\r\n", emuLog ); @@ -658,28 +659,43 @@ static void __concall ConsoleToFile_Newline() static void __concall ConsoleToFile_DoWrite( const wxString& fmt ) { - _immediate_logger( fmt.ToUTF8() ); + _immediate_logger( fmt ); } static void __concall ConsoleToFile_DoWriteLn( const wxString& fmt ) { - _immediate_logger( fmt.ToUTF8() ); + _immediate_logger( fmt ); ConsoleToFile_Newline(); if( emuLog != NULL ) fflush( emuLog ); } +static void __concall ConsoleToFile_SetTitle( const wxString& title ) +{ + ConsoleWriter_Stdio.SetTitle(title); +} + +static void __concall ConsoleToFile_SetColor( ConsoleColors color ) +{ + ConsoleWriter_Stdio.SetColor(color); +} + +static void __concall ConsoleToFile_ClearColor() +{ + ConsoleWriter_Stdio.ClearColor(); +} + extern const IConsoleWriter ConsoleWriter_File; -const IConsoleWriter ConsoleWriter_File = +const IConsoleWriter ConsoleWriter_File = { ConsoleToFile_DoWrite, ConsoleToFile_DoWriteLn, ConsoleToFile_Newline, - ConsoleWriter_Null.SetTitle, - ConsoleWriter_Null.SetColor, - ConsoleWriter_Null.ClearColor, + ConsoleToFile_SetTitle, + ConsoleToFile_SetColor, + ConsoleToFile_ClearColor, }; // thread-local console color storage. @@ -688,20 +704,26 @@ static __threadlocal ConsoleColors th_CurrentColor = DefaultConsoleColor; // -------------------------------------------------------------------------------------- // ConsoleToWindow Implementations // -------------------------------------------------------------------------------------- +template< const IConsoleWriter& secondary > static void __concall ConsoleToWindow_SetTitle( const wxString& title ) { + secondary.SetTitle(title); wxCommandEvent evt( wxEVT_SetTitleText ); evt.SetString( title ); wxGetApp().ProgramLog_PostEvent( evt ); } +template< const IConsoleWriter& secondary > static void __concall ConsoleToWindow_SetColor( ConsoleColors color ) { + secondary.SetColor(color); th_CurrentColor = color; } +template< const IConsoleWriter& secondary > static void __concall ConsoleToWindow_ClearColor() { + secondary.ClearColor(); th_CurrentColor = DefaultConsoleColor; } @@ -734,9 +756,9 @@ static const IConsoleWriter ConsoleWriter_Window = ConsoleToWindow_DoWriteLn, ConsoleToWindow_Newline, - ConsoleToWindow_SetTitle, - ConsoleToWindow_SetColor, - ConsoleToWindow_ClearColor, + ConsoleToWindow_SetTitle, + ConsoleToWindow_SetColor, + ConsoleToWindow_ClearColor, }; static const IConsoleWriter ConsoleWriter_WindowAndFile = @@ -745,9 +767,9 @@ static const IConsoleWriter ConsoleWriter_WindowAndFile = ConsoleToWindow_DoWriteLn, ConsoleToWindow_Newline, - ConsoleToWindow_SetTitle, - ConsoleToWindow_SetColor, - ConsoleToWindow_ClearColor, + ConsoleToWindow_SetTitle, + ConsoleToWindow_SetColor, + ConsoleToWindow_ClearColor, }; void Pcsx2App::EnableAllLogging() const