Files
ARMSX2/common/src/Utilities/Console.cpp
T
Jake.Stine 653d09e821 Lots of new code maintenance stuffs:
* Completely new assertion macros: pxAssert, pxAssertMsg, and pxFail, pxAssertDev (both which default to using a message).  These replace *all* wxASSERT, DevAssert, and jASSUME varieties of macros.  New macros borrow the best of all assertion worlds: MSVCRT, wxASSERT, and AtlAssume. :)
 * Rewrote the Console namespace as a structure called IConsoleWriter, and created several varieties of ConsoleWriters for handling different states of log and console availability (should help reduce overhead of console logging nicely).
 * More improvements to the PersistentThread model, using safely interlocked "Do*" style callbacks for starting and cleaning up threads.
 * Fixed console logs so that they're readable in Win32 notepad again (the log writer adds CRs to naked LFs).
 * Added AppInit.cpp -- contains constructor, destructor, OnInit, and command line parsing mess.

git-svn-id: http://pcsx2.googlecode.com/svn/trunk@1950 96395faa-99c1-11dd-bbfe-3dabce05a288
2009-10-04 08:27:27 +00:00

319 lines
8.1 KiB
C++

/* 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.
*
* PCSX2 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 for more details.
*
* You should have received a copy of the GNU General Public License along with PCSX2.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "PrecompiledHeader.h"
#include "Threading.h"
using namespace Threading;
using namespace std;
// Important! Only Assert and Null console loggers are allowed for initial console targeting.
// Other log targets rely on the static buffer and a threaded mutex lock, which are only valid
// after C++ initialization has finished.
void Console_SetActiveHandler( const IConsoleWriter& writer, FILE* flushfp )
{
pxAssertDev(
(writer.DoWrite != NULL) && (writer.DoWriteLn != NULL) &&
(writer.Newline != NULL) && (writer.SetTitle != NULL) &&
(writer.SetColor != NULL) && (writer.ClearColor != NULL),
"Invalid IConsoleWriter object! All function pointer interfaces must be implemented."
);
if( !ConsoleBuffer_Get().IsEmpty() )
writer.DoWriteLn( ConsoleBuffer_Get() );
Console = writer;
#ifdef PCSX2_DEVBUILD
DevCon = writer;
#endif
#ifdef PCSX2_DEBUG
DbgCon = writer;
#endif
}
// --------------------------------------------------------------------------------------
// ConsoleImpl_Null
// --------------------------------------------------------------------------------------
static void __concall ConsoleNull_SetTitle( const wxString& title ) {}
static void __concall ConsoleNull_SetColor( ConsoleColors color ) {}
static void __concall ConsoleNull_ClearColor() {}
static void __concall ConsoleNull_Newline() {}
static void __concall ConsoleNull_DoWrite( const wxString& fmt ) {}
static void __concall ConsoleNull_DoWriteLn( const wxString& fmt ) {}
// --------------------------------------------------------------------------------------
// ConsoleImpl_Assert
// --------------------------------------------------------------------------------------
static void __concall ConsoleAssert_DoWrite( const wxString& fmt )
{
pxFail( L"Console class has not been initialized; Message written:\n\t" + fmt );
}
static void __concall ConsoleAssert_DoWriteLn( const wxString& fmt )
{
pxFail( L"Console class has not been initialized; Message written:\n\t" + fmt );
}
// --------------------------------------------------------------------------------------
// ConsoleImpl_Buffered Implementations
// --------------------------------------------------------------------------------------
static wxString m_buffer;
const wxString& ConsoleBuffer_Get()
{
return m_buffer;
}
void ConsoleBuffer_Clear()
{
m_buffer.Clear();
}
void ConsoleBuffer_FlushToFile( FILE *fp )
{
if( fp == NULL || m_buffer.IsEmpty() ) return;
px_fputs( fp, toUTF8(m_buffer) );
m_buffer.Clear();
}
static void __concall ConsoleBuffer_DoWrite( const wxString& fmt )
{
m_buffer += fmt;
}
static void __concall ConsoleBuffer_DoWriteLn( const wxString& fmt )
{
m_buffer += fmt + L"\n";
}
// --------------------------------------------------------------------------------------
// ConsoleImpl_wxLogError Implementations
// --------------------------------------------------------------------------------------
static void __concall Console_wxLogError_DoWriteLn( const wxString& fmt )
{
if( !m_buffer.IsEmpty() )
{
wxLogError( m_buffer );
m_buffer.Clear();
}
wxLogError( fmt );
}
// --------------------------------------------------------------------------------------
// IConsole Implementations
// --------------------------------------------------------------------------------------
// Default write action at startup and shutdown is to use the stdout.
void IConsole_DoWrite( const wxString& fmt )
{
wxPrintf( fmt );
}
// Default write action at startup and shutdown is to use the stdout.
void IConsole_DoWriteLn( const wxString& fmt )
{
wxPrintf( fmt );
}
// Writes a line of colored text to the console (no newline).
// The console color is reset to default when the operation is complete.
void IConsoleWriter::Write( ConsoleColors color, const wxString& fmt ) const
{
SetColor( color );
Write( fmt );
ClearColor();
}
// Writes a line of colored text to the console, with automatic newline appendage.
// The console color is reset to default when the operation is complete.
void IConsoleWriter::WriteLn( ConsoleColors color, const wxString& fmt ) const
{
SetColor( color );
WriteLn( fmt );
ClearColor();
}
void IConsoleWriter::_Write( const char* fmt, va_list args ) const
{
std::string m_format_buffer;
vssprintf( m_format_buffer, fmt, args );
Write( wxString::FromUTF8( m_format_buffer.c_str() ) );
}
void IConsoleWriter::_WriteLn( const char* fmt, va_list args ) const
{
std::string m_format_buffer;
vssprintf( m_format_buffer, fmt, args );
WriteLn( wxString::FromUTF8( m_format_buffer.c_str() ) );
}
void IConsoleWriter::_WriteLn( ConsoleColors color, const char* fmt, va_list args ) const
{
SetColor( color );
_WriteLn( fmt, args );
ClearColor();
}
void IConsoleWriter::Write( const char* fmt, ... ) const
{
va_list args;
va_start(args,fmt);
_Write( fmt, args );
va_end(args);
}
void IConsoleWriter::Write( ConsoleColors color, const char* fmt, ... ) const
{
va_list args;
va_start(args,fmt);
SetColor( color );
_Write( fmt, args );
ClearColor();
va_end(args);
}
void IConsoleWriter::WriteLn( const char* fmt, ... ) const
{
va_list args;
va_start(args,fmt);
_WriteLn( fmt, args );
va_end(args);
}
void IConsoleWriter::WriteLn( ConsoleColors color, const char* fmt, ... ) const
{
va_list args;
va_start(args,fmt);
_WriteLn( color, fmt, args );
va_end(args);
}
void IConsoleWriter::Write( const wxString& src ) const
{
DoWrite( src );
}
void IConsoleWriter::WriteLn( const wxString& src ) const
{
DoWriteLn( src );
}
void IConsoleWriter::Error( const char* fmt, ... ) const
{
va_list args;
va_start(args,fmt);
_WriteLn( Color_Red, fmt, args );
va_end(args);
}
void IConsoleWriter::Notice( const char* fmt, ... ) const
{
va_list list;
va_start(list,fmt);
_WriteLn( Color_Yellow, fmt, list );
va_end(list);
}
void IConsoleWriter::Status( const char* fmt, ... ) const
{
va_list list;
va_start(list,fmt);
_WriteLn( Color_Green, fmt, list );
va_end(list);
}
void IConsoleWriter::Error( const wxString& src ) const
{
WriteLn( Color_Red, src );
}
void IConsoleWriter::Notice( const wxString& src ) const
{
WriteLn( Color_Yellow, src );
}
void IConsoleWriter::Status( const wxString& src ) const
{
WriteLn( Color_Green, src );
}
const IConsoleWriter ConsoleWriter_Null =
{
ConsoleNull_DoWrite,
ConsoleNull_DoWriteLn,
ConsoleNull_Newline,
ConsoleNull_SetTitle,
ConsoleNull_SetColor,
ConsoleNull_ClearColor,
};
const IConsoleWriter ConsoleWriter_Assert =
{
ConsoleAssert_DoWrite,
ConsoleAssert_DoWriteLn,
ConsoleNull_Newline,
ConsoleNull_SetTitle,
ConsoleNull_SetColor,
ConsoleNull_ClearColor,
};
const IConsoleWriter ConsoleWriter_wxError =
{
ConsoleBuffer_DoWrite, // Writes without newlines go to buffer to avoid error log spam.
Console_wxLogError_DoWriteLn,
ConsoleNull_Newline,
ConsoleNull_SetTitle,
ConsoleNull_SetColor,
ConsoleNull_ClearColor,
};
const IConsoleWriter ConsoleWriter_Buffered =
{
ConsoleBuffer_DoWrite, // Writes without newlines go to buffer to avoid assertion spam.
ConsoleBuffer_DoWriteLn,
ConsoleNull_Newline,
ConsoleNull_SetTitle,
ConsoleNull_SetColor,
ConsoleNull_ClearColor,
};
// Important! Only Assert and Null console loggers are allowed for initial console targeting.
// Other log targets rely on the static buffer and a threaded mutex lock, which are only valid
// after C++ initialization has finished.
IConsoleWriter Console = ConsoleWriter_Assert;
#ifdef PCSX2_DEVBUILD
IConsoleWriter DevConWriter= ConsoleWriter_Assert;
#endif
#ifdef PCSX2_DEBUG
IConsoleWriter DbgConWriter= ConsoleWriter_Assert;
#endif