File: Console.h

package info (click to toggle)
pcsx2 1.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,172 kB
  • ctags: 40,348
  • sloc: cpp: 232,892; ansic: 22,912; asm: 2,273; lisp: 1,346; sh: 561; perl: 253; makefile: 113; xml: 69
file content (271 lines) | stat: -rw-r--r-- 10,038 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*  PCSX2 - PS2 Emulator for PCs
 *  Copyright (C) 2002-2010  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/>.
 */

#pragma once

#include "StringHelpers.h"

enum ConsoleColors
{
	Color_Current = -1,

	Color_Black = 0,
	Color_Green,
	Color_Red,
	Color_Blue,
	Color_Magenta,
	Color_Orange,
	Color_Gray,

	Color_Cyan,			// faint visibility, intended for logging PS2/IOP output
	Color_Yellow,		// faint visibility, intended for logging PS2/IOP output
	Color_White,		// faint visibility, intended for logging PS2/IOP output

	// Strong text *may* result in mis-aligned text in the console, depending on the
	// font and the platform, so use these with caution.
	Color_StrongBlack,
	Color_StrongRed,	// intended for errors
	Color_StrongGreen,	// intended for infrequent state information
	Color_StrongBlue,	// intended for block headings
	Color_StrongMagenta,
	Color_StrongOrange,	// intended for warnings
	Color_StrongGray,

	Color_StrongCyan,
	Color_StrongYellow,
	Color_StrongWhite,

    Color_Default,

	ConsoleColors_Count
};

static const ConsoleColors DefaultConsoleColor = Color_Default;


// Use fastcall for the console; should be helpful in most cases
#define __concall	__fastcall

// ----------------------------------------------------------------------------------------
//  IConsoleWriter -- For printing messages to the console.
// ----------------------------------------------------------------------------------------
// General ConsoleWrite Threading Guideline:
//   PCSX2 is a threaded environment and multiple threads can write to the console asynchronously.
//   Individual calls to ConsoleWriter APIs will be written in atomic fashion, however "partial"
//   logs may end up interrupted by logs on other threads.  This is usually not a problem for
//   WriteLn, but may be undesirable for typical uses of Write.  In cases where you want to
//   print multi-line blocks of uninterrupted logs, compound the entire log into a single large
//   string and issue that to WriteLn.
//
struct IConsoleWriter
{
	// A direct console write, without tabbing or newlines.  Useful to devs who want to do quick
	// logging of various junk; but should *not* be used in production code due.
	void (__concall *WriteRaw)( const wxString& fmt );

	// WriteLn implementation for internal use only.  Bypasses tabbing, prefixing, and other
	// formatting.
	void (__concall *DoWriteLn)( const wxString& fmt );

	// SetColor implementation for internal use only.
	void (__concall *DoSetColor)( ConsoleColors color );

	// Special implementation of DoWrite that's pretty much for MSVC use only.
	// All implementations should map to DoWrite, except Stdio which should map to Null.
	// (This avoids circular/recursive stdio output)
	void (__concall *DoWriteFromStdout)( const wxString& fmt );

	void (__concall *Newline)();
	void (__concall *SetTitle)( const wxString& title );

	// internal value for indentation of individual lines.  Use the Indent() member to invoke.
	int _imm_indentation;

	// For internal use only.
	wxString _addIndentation( const wxString& src, int glob_indent ) const;

	// ----------------------------------------------------------------------------
	// Public members; call these to print stuff to console!
	//
	// All functions always return false.  Return value is provided only so that we can easily
	// disable logs at compile time using the "0&&action" macro trick.

	ConsoleColors GetColor() const;
	const IConsoleWriter& SetColor( ConsoleColors color ) const;
	const IConsoleWriter& ClearColor() const;
	const IConsoleWriter& SetIndent( int tabcount=1 ) const;

	IConsoleWriter Indent( int tabcount=1 ) const;

	bool FormatV( const char* fmt, va_list args ) const;
	bool WriteLn( ConsoleColors color, const char* fmt, ... ) const;
	bool WriteLn( const char* fmt, ... ) const;
	bool Error( const char* fmt, ... ) const;
	bool Warning( const char* fmt, ... ) const;

	bool FormatV( const wxChar* fmt, va_list args ) const;
	bool WriteLn( ConsoleColors color, const wxChar* fmt, ... ) const;
	bool WriteLn( const wxChar* fmt, ... ) const;
	bool Error( const wxChar* fmt, ... ) const;
	bool Warning( const wxChar* fmt, ... ) const;

#if wxMAJOR_VERSION >= 3
	bool WriteLn( ConsoleColors color, const wxString fmt, ... ) const;
	bool WriteLn( const wxString fmt, ... ) const;
	bool Error( const wxString fmt, ... ) const;
	bool Warning( const wxString fmt, ... ) const;
#endif
};

// --------------------------------------------------------------------------------------
//  NullConsoleWriter
// --------------------------------------------------------------------------------------
// Used by Release builds for Debug and Devel writes (DbgCon / DevCon).  Inlines to NOPs. :)
//
struct NullConsoleWriter
{
	void WriteRaw( const wxString& fmt ) {}
	void DoWriteLn( const wxString& fmt ) {}
	void DoSetColor( ConsoleColors color ) {}
	void DoWriteFromStdout( const wxString& fmt ) {}
	void Newline() {}
	void SetTitle( const wxString& title ) {}


	ConsoleColors GetColor() const { return Color_Current; }
	const NullConsoleWriter& SetColor( ConsoleColors color ) const { return *this; }
	const NullConsoleWriter& ClearColor() const { return *this; }
	const NullConsoleWriter& SetIndent( int tabcount=1 ) const { return *this; }

	NullConsoleWriter Indent( int tabcount=1 ) const { return NullConsoleWriter(); }

	bool FormatV( const char* fmt, va_list args ) const				{ return false; }
	bool WriteLn( ConsoleColors color, const char* fmt, ... ) const	{ return false; }
	bool WriteLn( const char* fmt, ... ) const						{ return false; }
	bool Error( const char* fmt, ... ) const						{ return false; }
	bool Warning( const char* fmt, ... ) const						{ return false; }

	bool FormatV( const wxChar* fmt, va_list args ) const			{ return false; }
	bool WriteLn( ConsoleColors color, const wxChar* fmt, ... ) const { return false; }
	bool WriteLn( const wxChar* fmt, ... ) const					{ return false; }
	bool Error( const wxChar* fmt, ... ) const						{ return false; }
	bool Warning( const wxChar* fmt, ... ) const					{ return false; }

#if wxMAJOR_VERSION >= 3
	bool WriteLn( ConsoleColors color, const wxString fmt, ... ) const { return false; }
	bool WriteLn( const wxString fmt, ... ) const					{ return false; }
	bool Error( const wxString fmt, ... ) const					{ return false; }
	bool Warning( const wxString fmt, ... ) const					{ return false; }
#endif
};

// --------------------------------------------------------------------------------------
//  ConsoleIndentScope
// --------------------------------------------------------------------------------------
// Provides a scoped indentation of the IConsoleWriter interface for the current thread.
// Any console writes performed from this scope will be indented by the specified number
// of tab characters.
//
// Scoped Object Notes:  Like most scoped objects, this is intended to be used as a stack
// or temporary object only.  Using it in a situation where the object's lifespan out-lives
// a scope will almost certainly result in unintended /undefined side effects.
//
class ConsoleIndentScope
{
	DeclareNoncopyableObject( ConsoleIndentScope );

protected:
	int			m_amount;
	bool		m_IsScoped;

public:
	// Constructor: The specified number of tabs will be appended to the current indentation
	// setting.  The tabs will be unrolled when the object leaves scope or is destroyed.
	ConsoleIndentScope( int tabs=1 );
	virtual ~ConsoleIndentScope() throw();	
	void EnterScope();
	void LeaveScope();
};

// --------------------------------------------------------------------------------------
//  ConsoleColorScope
// --------------------------------------------------------------------------------------
class ConsoleColorScope
{
	DeclareNoncopyableObject( ConsoleColorScope );

protected:
	ConsoleColors	m_newcolor;
	ConsoleColors	m_old_color;
	bool			m_IsScoped;

public:
	ConsoleColorScope( ConsoleColors newcolor );
	virtual ~ConsoleColorScope() throw();
	void EnterScope();
	void LeaveScope();
};

// --------------------------------------------------------------------------------------
//  ConsoleAttrScope
// --------------------------------------------------------------------------------------
// Applies both color and tab attributes in a single object constructor.
//
class ConsoleAttrScope
{
	DeclareNoncopyableObject( ConsoleAttrScope );

protected:
	ConsoleColors	m_old_color;
	int				m_tabsize;

public:
	ConsoleAttrScope( ConsoleColors newcolor, int indent=0 );
	virtual ~ConsoleAttrScope() throw();
};

extern IConsoleWriter	Console;

#ifdef __linux__
extern void Console_SetStdout(FILE *fp);
#endif
extern void Console_SetActiveHandler( const IConsoleWriter& writer, FILE* flushfp=NULL );
extern const wxString& ConsoleBuffer_Get();
extern void ConsoleBuffer_Clear();
extern void ConsoleBuffer_FlushToFile( FILE *fp );

extern const IConsoleWriter		ConsoleWriter_Null;
extern const IConsoleWriter		ConsoleWriter_Stdout;
extern const IConsoleWriter		ConsoleWriter_Assert;
extern const IConsoleWriter		ConsoleWriter_Buffered;
extern const IConsoleWriter		ConsoleWriter_wxError;

extern NullConsoleWriter	NullCon;

extern IConsoleWriter			DevConWriter;
extern bool						DevConWriterEnabled;

#ifdef PCSX2_DEVBUILD
#	define DevCon DevConWriter
#else
#	define DevCon DevConWriterEnabled && DevConWriter
#endif

#ifdef PCSX2_DEBUG
extern IConsoleWriter		DbgConWriter;
#	define DbgCon				DbgConWriter
#else
#	define DbgCon				0&&NullCon
#endif