/[pcsx2_0.9.7]/trunk/common/include/Utilities/TraceLog.h
ViewVC logotype

Contents of /trunk/common/include/Utilities/TraceLog.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 62 - (show annotations) (download)
Tue Sep 7 11:08:22 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 8153 byte(s)
Auto Commited Import of: pcsx2-0.9.7-r3738-debug in ./trunk
1 /* PCSX2 - PS2 Emulator for PCs
2 * Copyright (C) 2002-2010 PCSX2 Dev Team
3 *
4 * PCSX2 is free software: you can redistribute it and/or modify it under the terms
5 * of the GNU Lesser General Public License as published by the Free Software Found-
6 * ation, either version 3 of the License, or (at your option) any later version.
7 *
8 * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10 * PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with PCSX2.
13 * If not, see <http://www.gnu.org/licenses/>.
14 */
15
16 #pragma once
17
18 #include "Console.h"
19
20 // These macros are currently not needed (anymore), but might be needed aain in the future --air
21 #define TraceLog_ImplementBaseAPI(thistype)
22 #define ConsoleLog_ImplementBaseAPI(thistype)
23
24 // --------------------------------------------------------------------------------------
25 // TraceLogDescriptor
26 // --------------------------------------------------------------------------------------
27 // Provides textual information for use by UIs; to give the end user a selection screen for
28 // enabling/disabling logs, and also for saving the log settings to INI.
29 //
30 struct TraceLogDescriptor
31 {
32 // short name, alphanumerics only: used for saving/loading options.
33 const wxChar* ShortName;
34
35 // Standard UI name for this log source. Used in menus, options dialogs.
36 const wxChar* Name;
37
38 // Length description for use as a tooltip or menu item description.
39 const wxChar* Description;
40
41 wxString GetShortName() const
42 {
43 pxAssumeDev(Name, "Tracelog descriptors require a valid name!");
44 return ShortName ? ShortName : Name;
45 }
46 };
47
48 // --------------------------------------------------------------------------------------
49 // BaseTraceLogSource
50 // --------------------------------------------------------------------------------------
51 // This class houses the base attributes for any trace log (to file or to console), which
52 // only includes the logfile's name, description, and enabled bit (for UIs and ini files),
53 // and an IsActive() method for determining if the log should be written or not.
54 //
55 // Derived classes then provide their own Write/DoWrite functions that format and write
56 // the log to the intended target(s).
57 //
58 // All individual calls to log write functions should be responsible for checking the
59 // status of the log via the IsActive() method manually (typically done via macro). This
60 // is done in favor of internal checks because most logs include detailed/formatted
61 // information, which itself can take a lot of cpu power to prepare. If the IsActive()
62 // check is done top-level, the parameters' calculations can be skipped. If the IsActive()
63 // check is done internally as part of the Write/Format calls, all parameters have to be
64 // resolved regardless of if the log is actually active.
65 //
66 class BaseTraceLogSource
67 {
68 protected:
69 const TraceLogDescriptor* m_Descriptor;
70
71 public:
72 // Indicates if the user has enabled this specific log. This boolean only represents
73 // the configured status of this log, and does *NOT* actually mean the log is active
74 // even when TRUE. Because many tracelogs have master enablers that act on a group
75 // of logs, logging checks should always use IsActive() instead to determine if a log
76 // should be processed or not.
77 bool Enabled;
78
79 protected:
80 BaseTraceLogSource() {}
81
82 public:
83 TraceLog_ImplementBaseAPI(BaseTraceLogSource)
84
85 BaseTraceLogSource( const TraceLogDescriptor* desc )
86 {
87 pxAssumeDev( desc, "Trace logs must have a valid (non-NULL) descriptor." );
88 Enabled = false;
89 m_Descriptor = desc;
90 }
91
92 // Provides a categorical identifier, typically in "group.subgroup.subgroup" form.
93 // (use periods in favor of colons, since they do not require escape characters when
94 // written to ini/config files).
95 virtual wxString GetCategory() const { return wxEmptyString; }
96
97 // This method should be used to determine if a log should be generated or not.
98 // See the class overview comments for details on how and why this method should
99 // be used.
100 virtual bool IsActive() const { return Enabled; }
101
102 virtual wxString GetShortName() const { return m_Descriptor->GetShortName(); }
103 virtual const wxChar* GetName() const { return m_Descriptor->Name; }
104 virtual const wxChar* GetDescription() const
105 {
106 return (m_Descriptor->Description!=NULL) ? pxGetTranslation(m_Descriptor->Description) : wxEmptyString;
107 }
108
109 virtual bool HasDescription() const { return m_Descriptor->Description != NULL;}
110
111 };
112
113 // --------------------------------------------------------------------------------------
114 // TextFileTraceLog
115 // --------------------------------------------------------------------------------------
116 // This class is tailored for performance logging to file. It does not support console
117 // colors or wide/unicode text conversion.
118 //
119 class TextFileTraceLog : public BaseTraceLogSource
120 {
121 public:
122 TextFileTraceLog( const TraceLogDescriptor* desc )
123 : BaseTraceLogSource( desc )
124 {
125 }
126
127 bool Write( const char* fmt, ... ) const
128 {
129 va_list list;
130 va_start( list, fmt );
131 WriteV( fmt, list );
132 va_end( list );
133
134 return false;
135 }
136
137 bool WriteV( const char *fmt, va_list list ) const
138 {
139 FastFormatAscii ascii;
140 ApplyPrefix(ascii);
141 ascii.WriteV( fmt, list );
142 DoWrite( ascii );
143 return false;
144 }
145
146 virtual void ApplyPrefix( FastFormatAscii& ascii ) const {}
147 virtual void DoWrite( const char* fmt ) const=0;
148 };
149
150 // --------------------------------------------------------------------------------------
151 // ConsoleLogSource
152 // --------------------------------------------------------------------------------------
153 // This class is tailored for logging to console. It applies default console color attributes
154 // to all writes, and supports both char and wxChar (Ascii and UF8/16) formatting.
155 //
156 class ConsoleLogSource : public BaseTraceLogSource
157 {
158 public:
159 ConsoleColors DefaultColor;
160
161 protected:
162 ConsoleLogSource() {}
163
164 public:
165 ConsoleLog_ImplementBaseAPI(ConsoleLogSource)
166
167 ConsoleLogSource( const TraceLogDescriptor* desc, ConsoleColors defaultColor = Color_Gray )
168 : BaseTraceLogSource(desc)
169 {
170 DefaultColor = defaultColor;
171 }
172
173 // Writes to the console using the source's default color. Note that the source's default
174 // color will always be used, thus ConsoleColorScope() will not be effectual unless the
175 // console's default color is Color_Default.
176 bool Write( const char* fmt, ... ) const
177 {
178 va_list list;
179 va_start( list, fmt );
180 WriteV( fmt, list );
181 va_end( list );
182
183 return false;
184 }
185
186 bool Write( const wxChar* fmt, ... ) const
187 {
188 va_list list;
189 va_start( list, fmt );
190 WriteV( fmt, list );
191 va_end( list );
192
193 return false;
194 }
195
196 // Writes to the console using the specified color. This overrides the default color setting
197 // for this log.
198 bool Write( ConsoleColors color, const char* fmt, ... ) const
199 {
200 va_list list;
201 va_start( list, fmt );
202 WriteV( color, fmt, list );
203 va_end( list );
204
205 return false;
206 }
207
208 bool Write( ConsoleColors color, const wxChar* fmt, ... ) const
209 {
210 va_list list;
211 va_start( list, fmt );
212 WriteV( color, fmt, list );
213 va_end( list );
214
215 return false;
216 }
217
218 // Writes to the console using bold yellow text -- overrides the log source's default
219 // color settings.
220 bool Warn( const wxChar* fmt, ... ) const
221 {
222 va_list list;
223 va_start( list, fmt );
224 WriteV( Color_StrongYellow, fmt, list );
225 va_end( list );
226
227 return false;
228 }
229
230 // Writes to the console using bold red text -- overrides the log source's default
231 // color settings.
232 bool Error( const wxChar* fmt, ... ) const
233 {
234 va_list list;
235 va_start( list, fmt );
236 WriteV( Color_StrongRed, fmt, list );
237 va_end( list );
238
239 return false;
240 }
241
242 bool WriteV( const char *fmt, va_list list ) const;
243 bool WriteV( const wxChar *fmt, va_list list ) const;
244
245 bool WriteV( ConsoleColors color, const char *fmt, va_list list ) const;
246 bool WriteV( ConsoleColors color, const wxChar *fmt, va_list list ) const;
247
248 virtual void DoWrite( const wxChar* msg ) const
249 {
250 Console.DoWriteLn( msg );
251 }
252 };
253

  ViewVC Help
Powered by ViewVC 1.1.22