[Project64] Cleanup Trace Files

This commit is contained in:
zilmar 2015-12-10 17:38:33 +11:00
parent 146f295b10
commit ac1211eb76
3 changed files with 181 additions and 181 deletions

View File

@ -5,240 +5,240 @@ BOOL TraceClosed = FALSE;
class CTraceLog class CTraceLog
{ {
std::vector<CTraceModule *> m_Modules; std::vector<CTraceModule *> m_Modules;
CriticalSection m_CS; CriticalSection m_CS;
public: public:
CTraceLog() CTraceLog()
{ {
} }
~CTraceLog() { CloseTrace(); } ~CTraceLog() { CloseTrace(); }
CTraceModule * AddTraceModule(CTraceModule * TraceModule); CTraceModule * AddTraceModule(CTraceModule * TraceModule);
CTraceModule * RemoveTraceModule(CTraceModule * TraceModule); CTraceModule * RemoveTraceModule(CTraceModule * TraceModule);
void CloseTrace(void); void CloseTrace(void);
void WriteTrace(TraceType Type, LPCTSTR Message); void WriteTrace(TraceType Type, LPCTSTR Message);
void WriteTraceF(TraceType Type, LPCTSTR strFormat, va_list args); void WriteTraceF(TraceType Type, LPCTSTR strFormat, va_list args);
}; };
CTraceModule * CTraceLog::AddTraceModule(CTraceModule * TraceModule) CTraceModule * CTraceLog::AddTraceModule(CTraceModule * TraceModule)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
for (int i = 0; i < (int)m_Modules.size(); i++) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if (m_Modules[i] == TraceModule) if (m_Modules[i] == TraceModule)
{ {
return TraceModule; return TraceModule;
} }
} }
m_Modules.push_back(TraceModule); m_Modules.push_back(TraceModule);
return TraceModule; return TraceModule;
} }
CTraceModule * CTraceLog::RemoveTraceModule(CTraceModule * TraceModule) CTraceModule * CTraceLog::RemoveTraceModule(CTraceModule * TraceModule)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
for (std::vector<CTraceModule *>::iterator itr = m_Modules.begin(); itr != m_Modules.end(); itr++) for (std::vector<CTraceModule *>::iterator itr = m_Modules.begin(); itr != m_Modules.end(); itr++)
{ {
if ((*itr) == TraceModule) if ((*itr) == TraceModule)
{ {
m_Modules.erase(itr); m_Modules.erase(itr);
return TraceModule; return TraceModule;
} }
} }
return NULL; return NULL;
} }
void CTraceLog::CloseTrace(void) void CTraceLog::CloseTrace(void)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
for (int i = 0; i < (int)m_Modules.size(); i++) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if (m_Modules[i]) if (m_Modules[i])
delete m_Modules[i]; delete m_Modules[i];
} }
m_Modules.clear(); m_Modules.clear();
} }
void CTraceLog::WriteTraceF(TraceType Type, LPCTSTR strFormat, va_list args) void CTraceLog::WriteTraceF(TraceType Type, LPCTSTR strFormat, va_list args)
{ {
const int nMaxSize = 32 * 1024; const int nMaxSize = 32 * 1024;
TCHAR pBuffer[nMaxSize]; TCHAR pBuffer[nMaxSize];
_vsntprintf(pBuffer, nMaxSize, strFormat, args); _vsntprintf(pBuffer, nMaxSize, strFormat, args);
pBuffer[nMaxSize - 1] = 0; pBuffer[nMaxSize - 1] = 0;
WriteTrace(Type, pBuffer); WriteTrace(Type, pBuffer);
} }
void CTraceLog::WriteTrace(TraceType Type, LPCTSTR Message) void CTraceLog::WriteTrace(TraceType Type, LPCTSTR Message)
{ {
CGuard Guard(m_CS); CGuard Guard(m_CS);
if (Type != TraceNone) if (Type != TraceNone)
{ {
bool WriteToLog = false; bool WriteToLog = false;
for (int i = 0; i < (int)m_Modules.size(); i++) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if ((m_Modules[i]->GetTraceLevel() & Type) != 0) if ((m_Modules[i]->GetTraceLevel() & Type) != 0)
{ {
WriteToLog = true; WriteToLog = true;
break; break;
} }
} }
if (!WriteToLog) { return; } if (!WriteToLog) { return; }
} }
if ((Type & TraceNoHeader) == 0) if ((Type & TraceNoHeader) == 0)
{ {
TCHAR pBuffer[300]; TCHAR pBuffer[300];
int nPos = 0; int nPos = 0;
SYSTEMTIME sysTime; SYSTEMTIME sysTime;
::GetLocalTime(&sysTime); ::GetLocalTime(&sysTime);
nPos = _stprintf(pBuffer, _T("%04d/%02d/%02d %02d:%02d:%02d.%03d %05d: "), sysTime.wYear, nPos = _stprintf(pBuffer, _T("%04d/%02d/%02d %02d:%02d:%02d.%03d %05d: "), sysTime.wYear,
sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds,
::GetCurrentThreadId() ::GetCurrentThreadId()
); );
// show the debug level // show the debug level
if (Type == TraceNone) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("None : ")); } if (Type == TraceNone) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("None : ")); }
else if ((Type & TraceError) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Error : ")); } else if ((Type & TraceError) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Error : ")); }
else if ((Type & TraceSettings) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Setting: ")); } else if ((Type & TraceSettings) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Setting: ")); }
else if ((Type & TraceGfxPlugin) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Gfx : ")); } else if ((Type & TraceGfxPlugin) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Gfx : ")); }
else if ((Type & TraceDebug) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Debug : ")); } else if ((Type & TraceDebug) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Debug : ")); }
else if ((Type & TraceRecompiler) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Recomp : ")); } else if ((Type & TraceRecompiler) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Recomp : ")); }
else if ((Type & TraceRSP) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("RSP : ")); } else if ((Type & TraceRSP) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("RSP : ")); }
else if ((Type & TraceTLB) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("TLB : ")); } else if ((Type & TraceTLB) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("TLB : ")); }
else if ((Type & TraceValidate) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Valid : ")); } else if ((Type & TraceValidate) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Valid : ")); }
else if ((Type & TraceAudio) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Audio : ")); } else if ((Type & TraceAudio) != 0) { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Audio : ")); }
else { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Unknown: ")); } else { nPos += _stprintf(pBuffer + nPos, _T("%s"), _T("Unknown: ")); }
for (int i = 0; i < (int)m_Modules.size(); i++) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if ((m_Modules[i]->GetTraceLevel() & Type) != 0) if ((m_Modules[i]->GetTraceLevel() & Type) != 0)
{ {
m_Modules[i]->Write(pBuffer, false); m_Modules[i]->Write(pBuffer, false);
} }
} }
} }
for (int i = 0; i < (int)m_Modules.size(); i++) for (int i = 0; i < (int)m_Modules.size(); i++)
{ {
if ((m_Modules[i]->GetTraceLevel() & Type) != 0) if ((m_Modules[i]->GetTraceLevel() & Type) != 0)
{ {
m_Modules[i]->Write(Message, true); m_Modules[i]->Write(Message, true);
} }
} }
} }
CTraceLog & GetTraceObjet(void) CTraceLog & GetTraceObjet(void)
{ {
static CTraceLog TraceLog; static CTraceLog TraceLog;
return TraceLog; return TraceLog;
} }
void WriteTrace(TraceType Type, LPCTSTR Message) void WriteTrace(TraceType Type, LPCTSTR Message)
{ {
if (TraceClosed) if (TraceClosed)
{ {
return; return;
} }
GetTraceObjet().WriteTrace(Type, Message); GetTraceObjet().WriteTrace(Type, Message);
} }
void WriteTraceF(TraceType Type, LPCTSTR strFormat, ...) void WriteTraceF(TraceType Type, LPCTSTR strFormat, ...)
{ {
if (TraceClosed) if (TraceClosed)
{ {
return; return;
} }
va_list args; va_list args;
va_start(args, strFormat); va_start(args, strFormat);
GetTraceObjet().WriteTraceF(Type, strFormat, args); GetTraceObjet().WriteTraceF(Type, strFormat, args);
va_end(args); va_end(args);
} }
CTraceModule * AddTraceModule(CTraceModule * TraceModule) CTraceModule * AddTraceModule(CTraceModule * TraceModule)
{ {
if (TraceClosed) if (TraceClosed)
{ {
return NULL; return NULL;
} }
GetTraceObjet().AddTraceModule(TraceModule); GetTraceObjet().AddTraceModule(TraceModule);
return TraceModule; return TraceModule;
} }
CTraceModule * RemoveTraceModule(CTraceModule * TraceModule) CTraceModule * RemoveTraceModule(CTraceModule * TraceModule)
{ {
return GetTraceObjet().RemoveTraceModule(TraceModule); return GetTraceObjet().RemoveTraceModule(TraceModule);
} }
void CloseTrace(void) void CloseTrace(void)
{ {
TraceClosed = true; TraceClosed = true;
GetTraceObjet().CloseTrace(); GetTraceObjet().CloseTrace();
} }
CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile) : CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile) :
m_FlushFile(FlushFile) m_FlushFile(FlushFile)
{ {
m_hLogFile.SetFlush(false); m_hLogFile.SetFlush(false);
m_hLogFile.SetTruncateFile(true); m_hLogFile.SetTruncateFile(true);
m_hLogFile.SetMaxFileSize(5 * MB); m_hLogFile.SetMaxFileSize(5 * MB);
m_hLogFile.Open(FileName, Log_Append); m_hLogFile.Open(FileName, Log_Append);
} }
CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize) : CTraceFileLog::CTraceFileLog(LPCTSTR FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize) :
m_FlushFile(FlushFile) m_FlushFile(FlushFile)
{ {
enum { MB = 1024 * 1024 }; enum { MB = 1024 * 1024 };
m_hLogFile.SetFlush(false); m_hLogFile.SetFlush(false);
m_hLogFile.SetTruncateFile(true); m_hLogFile.SetTruncateFile(true);
if (dwMaxFileSize < 3 || dwMaxFileSize > 2047) if (dwMaxFileSize < 3 || dwMaxFileSize > 2047)
{ /* Clamp file size to 5 MB if it exceeds 2047 or falls short of 3. */ { /* Clamp file size to 5 MB if it exceeds 2047 or falls short of 3. */
dwMaxFileSize = 5; dwMaxFileSize = 5;
} }
m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB); m_hLogFile.SetMaxFileSize(dwMaxFileSize * MB);
m_hLogFile.Open(FileName, eMode); m_hLogFile.Open(FileName, eMode);
} }
CTraceFileLog::~CTraceFileLog() CTraceFileLog::~CTraceFileLog()
{ {
TraceClosed = true; TraceClosed = true;
} }
void CTraceFileLog::Write(LPCTSTR Message, bool EndOfLine) void CTraceFileLog::Write(LPCTSTR Message, bool EndOfLine)
{ {
if (!m_hLogFile.IsOpen()) { return; } if (!m_hLogFile.IsOpen()) { return; }
CGuard Section(m_CriticalSection); CGuard Section(m_CriticalSection);
m_hLogFile.Log(Message); m_hLogFile.Log(Message);
if (EndOfLine) if (EndOfLine)
{ {
m_hLogFile.Log(_T("\r\n")); m_hLogFile.Log(_T("\r\n"));
if (m_FlushFile) if (m_FlushFile)
{ {
m_hLogFile.Flush(); m_hLogFile.Flush();
} }
} }
} }
void CTraceFileLog::SetFlushFile(bool bFlushFile) void CTraceFileLog::SetFlushFile(bool bFlushFile)
{ {
m_FlushFile = bFlushFile; m_FlushFile = bFlushFile;
} }
void CDebugTraceLog::Write(const char * Message, bool EndOfLine) void CDebugTraceLog::Write(const char * Message, bool EndOfLine)
{ {
OutputDebugString(Message); OutputDebugString(Message);
if (EndOfLine) if (EndOfLine)
{ {
OutputDebugString("\n"); OutputDebugString("\n");
} }
} }

View File

@ -8,12 +8,12 @@ class CTraceModule
TraceLevel m_Type; TraceLevel m_Type;
public: public:
CTraceModule () { m_Type = TrLvError; } CTraceModule() { m_Type = TrLvError; }
virtual ~CTraceModule () {} virtual ~CTraceModule() {}
inline void SetTraceLevel ( TraceLevel Type ) { m_Type = Type; } inline void SetTraceLevel(TraceLevel Type) { m_Type = Type; }
inline TraceLevel GetTraceLevel ( void ) const { return m_Type; } inline TraceLevel GetTraceLevel(void) const { return m_Type; }
virtual void Write ( const char * Message, bool EndOfLine ) = 0; virtual void Write(const char * Message, bool EndOfLine) = 0;
}; };
class CTraceFileLog : public CTraceModule class CTraceFileLog : public CTraceModule
@ -25,19 +25,19 @@ class CTraceFileLog : public CTraceModule
bool m_FlushFile; bool m_FlushFile;
public: public:
CTraceFileLog (const char * FileName, bool FlushFile = true); CTraceFileLog(const char * FileName, bool FlushFile = true);
CTraceFileLog(const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize = 5); CTraceFileLog(const char * FileName, bool FlushFile, LOG_OPEN_MODE eMode, size_t dwMaxFileSize = 5);
virtual ~CTraceFileLog (); virtual ~CTraceFileLog();
void Write ( const char * Message, bool EndOfLine ); void Write(const char * Message, bool EndOfLine);
void SetFlushFile ( bool bFlushFile ); void SetFlushFile(bool bFlushFile);
}; };
class CDebugTraceLog : public CTraceModule class CDebugTraceLog : public CTraceModule
{ {
public: public:
void Write ( const char * Message, bool EndOfLine ); void Write(const char * Message, bool EndOfLine);
}; };
CTraceModule * AddTraceModule ( CTraceModule * TraceModule ); // Must be created with new CTraceModule * AddTraceModule(CTraceModule * TraceModule); // Must be created with new
CTraceModule * RemoveTraceModule ( CTraceModule * TraceModule ); // Is not automaticly deleted CTraceModule * RemoveTraceModule(CTraceModule * TraceModule); // Is not automaticly deleted

View File

@ -2,30 +2,30 @@
enum TraceType enum TraceType
{ {
TraceNone = 0x00000000, TraceNone = 0x00000000,
TraceError = 0x00000001, TraceError = 0x00000001,
TraceSettings = 0x00000002, TraceSettings = 0x00000002,
TraceGfxPlugin = 0x00000004, TraceGfxPlugin = 0x00000004,
TraceDebug = 0x00000010, TraceDebug = 0x00000010,
TraceRecompiler = 0x00000020, TraceRecompiler = 0x00000020,
TraceRSP = 0x00000040, TraceRSP = 0x00000040,
TraceTLB = 0x00000080, TraceTLB = 0x00000080,
TraceValidate = 0x00000100, TraceValidate = 0x00000100,
TraceAudio = 0x00000200, TraceAudio = 0x00000200,
TraceProtectedMem = 0x00000400, TraceProtectedMem = 0x00000400,
TraceNoHeader = 0x80000000, TraceNoHeader = 0x80000000,
}; };
enum TraceLevel enum TraceLevel
{ {
//Handle Existing Code //Handle Existing Code
TrLvError = TraceError, TrLvError = TraceError,
TrLv1 = TraceSettings | TrLvError, TrLv1 = TraceSettings | TrLvError,
TrLv2 = TrLv1 | TraceDebug, TrLv2 = TrLv1 | TraceDebug,
TrlvGfxPlugin = TraceGfxPlugin, TrlvGfxPlugin = TraceGfxPlugin,
TrLvAll = ~TraceNoHeader, TrLvAll = ~TraceNoHeader,
}; };
void WriteTrace ( TraceType Type, const char * Message ); void WriteTrace(TraceType Type, const char * Message);
void WriteTraceF ( TraceType Type, const char * strFormat, ... ); void WriteTraceF(TraceType Type, const char * strFormat, ...);
void CloseTrace ( void ); //Free's all memory associated with trace void CloseTrace(void); //Free's all memory associated with trace