From: José Fonseca Date: Wed, 9 Jul 2008 00:38:45 +0000 (+0900) Subject: Have logging methods as regular functions. X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=22aec83b67331d2263023ff1e4a28d87588367cc;p=apitrace Have logging methods as regular functions. --- diff --git a/base.py b/base.py index 5abed45..8f91a6d 100644 --- a/base.py +++ b/base.py @@ -55,7 +55,7 @@ class Intrinsic(Type): self.format = format def dump(self, instance): - print ' g_pLog->TextF("%s", %s);' % (self.format, instance) + print ' Log::TextF("%s", %s);' % (self.format, instance) class Const(Type): @@ -85,10 +85,10 @@ class Pointer(Type): try: self.type.dump("*" + instance) except NotImplementedError: - print ' g_pLog->TextF("%%p", %s);' % instance + print ' Log::TextF("%%p", %s);' % instance print ' }' print ' else' - print ' g_pLog->Text("NULL");' + print ' Log::Text("NULL");' def wrap_instance(self, instance): self.type.wrap_instance("*" + instance) @@ -117,10 +117,10 @@ class Enum(Type): print ' switch(%s) {' % instance for value in self.values: print ' case %s:' % value - print ' g_pLog->Text("%s");' % value + print ' Log::Text("%s");' % value print ' break;' print ' default:' - print ' g_pLog->TextF("%%i", %s);' % instance + print ' Log::TextF("%%i", %s);' % instance print ' break;' print ' }' @@ -137,7 +137,7 @@ class Flags(Type): print ' %s l_Value = %s;' % (self.type, instance) for value in self.values: print ' if((l_Value & %s) == %s) {' % (value, value) - print ' g_pLog->Text("%s | ");' % value + print ' Log::Text("%s | ");' % value print ' l_Value &= ~%s;' % value print ' }' self.type.dump("l_Value"); @@ -151,15 +151,15 @@ class Struct(Type): self.members = members def dump(self, instance): - print ' g_pLog->Text("{");' + print ' Log::Text("{");' first = True for type, name in self.members: if first: first = False else: - print ' g_pLog->Text(", ");' + print ' Log::Text(", ");' type.dump('(%s).%s' % (instance, name)) - print ' g_pLog->Text("}");' + print ' Log::Text("}");' class Alias(Type): @@ -251,29 +251,29 @@ class Interface(Type): else: print ' %s result;' % method.type result = 'result = ' - print ' g_pLog->BeginCall("%s");' % (self.name + '::' + method.name) - print ' g_pLog->BeginParam("this", "%s *");' % self.name - print ' g_pLog->TextF("%p", m_pInstance);' - print ' g_pLog->EndParam();' + print ' Log::BeginCall("%s");' % (self.name + '::' + method.name) + print ' Log::BeginParam("this", "%s *");' % self.name + print ' Log::TextF("%p", m_pInstance);' + print ' Log::EndParam();' for type, name in method.args: if not type.isoutput(): type.unwrap_instance(name) - print ' g_pLog->BeginParam("%s", "%s");' % (name, type) + print ' Log::BeginParam("%s", "%s");' % (name, type) type.dump(name) - print ' g_pLog->EndParam();' + print ' Log::EndParam();' print ' %sm_pInstance->%s(%s);' % (result, method.name, ', '.join([str(name) for type, name in method.args])) for type, name in method.args: if type.isoutput(): - print ' g_pLog->BeginParam("%s", "%s");' % (name, type) + print ' Log::BeginParam("%s", "%s");' % (name, type) type.dump(name) - print ' g_pLog->EndParam();' + print ' Log::EndParam();' type.wrap_instance(name) if method.type is not Void: - print ' g_pLog->BeginReturn("%s");' % method.type + print ' Log::BeginReturn("%s");' % method.type method.type.dump("result") - print ' g_pLog->EndReturn();' + print ' Log::EndReturn();' method.type.wrap_instance('result') - print ' g_pLog->EndCall();' + print ' Log::EndCall();' if method.name == 'QueryInterface': print ' if(*ppvObj == m_pInstance)' print ' *ppvObj = this;' diff --git a/log.cpp b/log.cpp index e1bc207..9a0ef3f 100644 --- a/log.cpp +++ b/log.cpp @@ -18,29 +18,35 @@ ****************************************************************************/ +#include + #include "log.hpp" -File::File(const TCHAR *szName, const TCHAR *szExtension) { - m_hFile = INVALID_HANDLE_VALUE; - Open(szName, szExtension); -} +namespace Log { + -File::~File() { - Close(); +static HANDLE g_hFile = INVALID_HANDLE_VALUE; +static TCHAR g_szFileName[MAX_PATH]; + +static void _Close(void) { + if(g_hFile != INVALID_HANDLE_VALUE) { + CloseHandle(g_hFile); + g_hFile = INVALID_HANDLE_VALUE; + } } -void File::Open(const TCHAR *szName, const TCHAR *szExtension) { - Close(); +static void _Open(const TCHAR *szName, const TCHAR *szExtension) { + _Close(); DWORD dwCounter = 0; do { if(dwCounter) - _sntprintf(szFileName, MAX_PATH, TEXT("%s.%u.%s"), szName, dwCounter, szExtension); + _sntprintf(g_szFileName, MAX_PATH, TEXT("%s.%u.%s"), szName, dwCounter, szExtension); else - _sntprintf(szFileName, MAX_PATH, TEXT("%s.%s"), szName, szExtension); + _sntprintf(g_szFileName, MAX_PATH, TEXT("%s.%s"), szName, szExtension); - m_hFile = CreateFile(szFileName, + g_hFile = CreateFile(g_szFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, @@ -48,30 +54,23 @@ void File::Open(const TCHAR *szName, const TCHAR *szExtension) { FILE_ATTRIBUTE_NORMAL, NULL); ++dwCounter; - } while(m_hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS); + } while(g_hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS); } -void File::ReOpen(void) { - Close(); +static void _ReOpen(void) { + _Close(); - m_hFile = CreateFile(szFileName, + g_hFile = CreateFile(g_szFileName, GENERIC_WRITE, - 0, + FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); } -void File::Close(void) { - if(m_hFile != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFile); - m_hFile = INVALID_HANDLE_VALUE; - } -} - -void File::Write(const char *szText) { - if(m_hFile == INVALID_HANDLE_VALUE) +static void Write(const char *szText) { + if(g_hFile == INVALID_HANDLE_VALUE) return; DWORD dwBytesToWrite = (DWORD)strlen(szText); @@ -85,20 +84,21 @@ void File::Write(const char *szText) { overlapped.Offset = 0xffffffff; overlapped.OffsetHigh = 0xffffffff; - if(WriteFile(m_hFile, + if(WriteFile(g_hFile, szText + dwBytesWritten, dwBytesToWrite - dwBytesWritten, &dwBytesWritten, &overlapped) == FALSE) { - Close(); - Open(TEXT("extra"), TEXT("xml")); + _Close(); + _Open(TEXT("extra"), TEXT("xml")); return; } } } -Log::Log(const TCHAR *szName) : File(szName, TEXT("xml")) { +void Open(const TCHAR *szName) { + _Open(szName, TEXT("xml")); Write(""); NewLine(); Write(""); @@ -107,28 +107,38 @@ Log::Log(const TCHAR *szName) : File(szName, TEXT("xml")) { NewLine(); } -Log::~Log() { +void ReOpen(void) { + _ReOpen(); +} + +void Close(void) { Write(""); NewLine(); + _Close(); +} + +static void Escape(const char *s) { + /* FIXME */ + Write(s); } -void Log::NewLine(void) { +void NewLine(void) { Write("\r\n"); } -void Log::Tag(const char *name) { +void Tag(const char *name) { Write("<"); Write(name); Write("/>"); } -void Log::BeginTag(const char *name) { +void BeginTag(const char *name) { Write("<"); Write(name); Write(">"); } -void Log::BeginTag(const char *name, +void BeginTag(const char *name, const char *attr1, const char *value1) { Write("<"); Write(name); @@ -139,7 +149,7 @@ void Log::BeginTag(const char *name, Write("\">"); } -void Log::BeginTag(const char *name, +void BeginTag(const char *name, const char *attr1, const char *value1, const char *attr2, const char *value2) { Write("<"); @@ -155,17 +165,17 @@ void Log::BeginTag(const char *name, Write("\">"); } -void Log::EndTag(const char *name) { +void EndTag(const char *name) { Write(""); } -void Log::Text(const char *text) { +void Text(const char *text) { Escape(text); } -void Log::TextF(const char *format, ...) { +void TextF(const char *format, ...) { char szBuffer[4196]; va_list ap; va_start(ap, format); @@ -174,42 +184,37 @@ void Log::TextF(const char *format, ...) { Escape(szBuffer); } -void Log::BeginCall(const char *function) { +void BeginCall(const char *function) { Write("\t"); BeginTag("call", "name", function); NewLine(); } -void Log::EndCall(void) { +void EndCall(void) { Write("\t"); EndTag("call"); NewLine(); } -void Log::BeginParam(const char *name, const char *type) { +void BeginParam(const char *name, const char *type) { Write("\t\t"); BeginTag("param", "name", name, "type", type); } -void Log::EndParam(void) { +void EndParam(void) { EndTag("param"); NewLine(); } -void Log::BeginReturn(const char *type) { +void BeginReturn(const char *type) { Write("\t\t"); BeginTag("return", "type", type); } -void Log::EndReturn(void) { +void EndReturn(void) { EndTag("return"); NewLine(); } -void Log::Escape(const char *s) { - /* FIXME */ - Write(s); -} - -Log * g_pLog = NULL; +} /* namespace Log */ \ No newline at end of file diff --git a/log.hpp b/log.hpp index a36dbe4..1e75c20 100644 --- a/log.hpp +++ b/log.hpp @@ -22,31 +22,13 @@ #include #include -#include -class File -{ -public: - File(const TCHAR *szName, const TCHAR *szExtension); - ~File(); +namespace Log { - void Open(const TCHAR *szName, const TCHAR *szExtension); + void Open(const TCHAR *szName); void ReOpen(void); void Close(void); - void Write(const char *szText); - -private: - HANDLE m_hFile; - TCHAR szFileName[MAX_PATH]; -}; - - -class Log : public File -{ -public: - Log(const TCHAR *szName); - ~Log(); void NewLine(void); void Tag(const char *name); @@ -65,13 +47,6 @@ public: void EndParam(void); void BeginReturn(const char *type); void EndReturn(void); - -protected: - void Escape(const char *s); -}; - - -extern Log * g_pLog; - +} #endif /* _LOG_HPP_ */ diff --git a/windows.py b/windows.py index 79b835e..090e246 100644 --- a/windows.py +++ b/windows.py @@ -107,34 +107,17 @@ class Dll: def wrap_impl(self): print r'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {' - #print r' Log * pLog;' print r' switch(fdwReason) {' print r' case DLL_PROCESS_ATTACH:' print r' if(!GetSystemDirectory(g_szDll, MAX_PATH))' print r' return FALSE;' print r' _tcscat(g_szDll, TEXT("\\%s.dll"));' % self.name - #print r' if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)' - #print r' return FALSE;' - #print r' if(!g_pLog)' - #print r' g_pLog = new Log(TEXT("%s"));' % self.name print r' case DLL_THREAD_ATTACH:' - #print r' pLog = new Log(TEXT("%s"));' % self.name - #print r' TlsSetValue(dwTlsIndex, pLog);' print r' return TRUE;' print r' case DLL_THREAD_DETACH:' - #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);' - #print r' if (pLog != NULL)' - #print r' delete pLog;' print r' return TRUE;' print r' case DLL_PROCESS_DETACH:' - #print r' pLog = (Log *)TlsGetValue(dwTlsIndex);' - #print r' if (pLog != NULL)' - #print r' delete pLog;' - #print r' TlsFree(dwTlsIndex);' - print r' if(g_pLog) {' - print r' delete g_pLog;' - print r' g_pLog = NULL;' - print r' }' + print r' Log::Close();' print r' if(g_hDll) {' print r' FreeLibrary(g_hDll);' print r' g_hDll = NULL;' @@ -150,10 +133,9 @@ class Dll: type = 'P' + function.name print function.prototype() + ' {' if 1: - print ' if(g_pLog)' - print ' delete g_pLog;' - print ' g_pLog = new Log(TEXT("%s"));' % self.name - #print ' g_pLog->ReOpen();' + print ' Log::Close();' + print ' Log::Open(TEXT("%s"));' % self.name + #print ' Log::ReOpen();' print ' typedef ' + function.prototype('* %s' % type) + ';' print ' %s pFunction;' % type if function.type is Void: @@ -169,9 +151,9 @@ class Dll: print ' pFunction = (%s)GetProcAddress( g_hDll, "%s");' % (type, function.name) print ' if(!pFunction)' print ' ExitProcess(0);' - print ' g_pLog->BeginCall("%s");' % (function.name) + print ' Log::BeginCall("%s");' % (function.name) print ' %spFunction(%s);' % (result, ', '.join([str(name) for type, name in function.args])) - print ' g_pLog->EndCall();' + print ' Log::EndCall();' for type, name in function.args: if type.isoutput(): type.wrap_instance(name)