From eec4e8e3871e6ead9d978286114d1a7e85551c64 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 9 Jul 2008 08:32:02 +0900 Subject: [PATCH] Don't inline the log implementation. --- SConstruct | 2 + log.cpp | 215 +++++++++++++++++++++++++++++++++++++++++++++++++++++ log.hpp | 204 ++++++-------------------------------------------- windows.py | 1 - 4 files changed, 240 insertions(+), 182 deletions(-) create mode 100644 log.cpp diff --git a/SConstruct b/SConstruct index 9f7b097..0993745 100644 --- a/SConstruct +++ b/SConstruct @@ -94,6 +94,7 @@ d3d8 = env.SharedLibrary( source = [ 'd3d8.def', 'd3d8.cpp', + 'log.cpp', ] ) @@ -110,6 +111,7 @@ d3d9 = env.SharedLibrary( source = [ 'd3d9.def', 'd3d9.cpp', + 'log.cpp', ] ) diff --git a/log.cpp b/log.cpp new file mode 100644 index 0000000..e1bc207 --- /dev/null +++ b/log.cpp @@ -0,0 +1,215 @@ +/**************************************************************************** + * + * Copyright 2008 Jose Fonseca + * + * This program 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 Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + * + ****************************************************************************/ + + +#include "log.hpp" + + +File::File(const TCHAR *szName, const TCHAR *szExtension) { + m_hFile = INVALID_HANDLE_VALUE; + Open(szName, szExtension); +} + +File::~File() { + Close(); +} + +void File::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); + else + _sntprintf(szFileName, MAX_PATH, TEXT("%s.%s"), szName, szExtension); + + m_hFile = CreateFile(szFileName, + GENERIC_WRITE, + FILE_SHARE_WRITE, + NULL, + CREATE_NEW, + FILE_ATTRIBUTE_NORMAL, + NULL); + ++dwCounter; + } while(m_hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS); +} + +void File::ReOpen(void) { + Close(); + + m_hFile = CreateFile(szFileName, + GENERIC_WRITE, + 0, + 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) + return; + + DWORD dwBytesToWrite = (DWORD)strlen(szText); + DWORD dwBytesWritten = 0; + + while (dwBytesWritten < dwBytesToWrite) { + OVERLAPPED overlapped; + memset(&overlapped, 0, sizeof(OVERLAPPED)); + + /* Write to end of file */ + overlapped.Offset = 0xffffffff; + overlapped.OffsetHigh = 0xffffffff; + + if(WriteFile(m_hFile, + szText + dwBytesWritten, + dwBytesToWrite - dwBytesWritten, + &dwBytesWritten, + &overlapped) == FALSE) { + Close(); + Open(TEXT("extra"), TEXT("xml")); + return; + } + } +} + + +Log::Log(const TCHAR *szName) : File(szName, TEXT("xml")) { + Write(""); + NewLine(); + Write(""); + NewLine(); + Write(""); + NewLine(); +} + +Log::~Log() { + Write(""); + NewLine(); +} + +void Log::NewLine(void) { + Write("\r\n"); +} + +void Log::Tag(const char *name) { + Write("<"); + Write(name); + Write("/>"); +} + +void Log::BeginTag(const char *name) { + Write("<"); + Write(name); + Write(">"); +} + +void Log::BeginTag(const char *name, + const char *attr1, const char *value1) { + Write("<"); + Write(name); + Write(" "); + Write(attr1); + Write("=\""); + Escape(value1); + Write("\">"); +} + +void Log::BeginTag(const char *name, + const char *attr1, const char *value1, + const char *attr2, const char *value2) { + Write("<"); + Write(name); + Write(" "); + Write(attr1); + Write("=\""); + Escape(value1); + Write("\" "); + Write(attr2); + Write("=\""); + Escape(value2); + Write("\">"); +} + +void Log::EndTag(const char *name) { + Write(""); +} + +void Log::Text(const char *text) { + Escape(text); +} + +void Log::TextF(const char *format, ...) { + char szBuffer[4196]; + va_list ap; + va_start(ap, format); + vsnprintf(szBuffer, sizeof(szBuffer), format, ap); + va_end(ap); + Escape(szBuffer); +} + +void Log::BeginCall(const char *function) { + Write("\t"); + BeginTag("call", "name", function); + NewLine(); +} + +void Log::EndCall(void) { + Write("\t"); + EndTag("call"); + NewLine(); +} + +void Log::BeginParam(const char *name, const char *type) { + Write("\t\t"); + BeginTag("param", "name", name, "type", type); +} + +void Log::EndParam(void) { + EndTag("param"); + NewLine(); +} + +void Log::BeginReturn(const char *type) { + Write("\t\t"); + BeginTag("return", "type", type); +} + +void Log::EndReturn(void) { + EndTag("return"); + NewLine(); +} + +void Log::Escape(const char *s) { + /* FIXME */ + Write(s); +} + + +Log * g_pLog = NULL; diff --git a/log.hpp b/log.hpp index a14bdff..a36dbe4 100644 --- a/log.hpp +++ b/log.hpp @@ -28,81 +28,13 @@ class File { public: - File(const TCHAR *szName, const TCHAR *szExtension) { - m_hFile = INVALID_HANDLE_VALUE; - Open(szName, szExtension); - } - - ~File() { - Close(); - } - - 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); - else - _sntprintf(szFileName, MAX_PATH, TEXT("%s.%s"), szName, szExtension); - - m_hFile = CreateFile(szFileName, - GENERIC_WRITE, - FILE_SHARE_WRITE, - NULL, - CREATE_NEW, - FILE_ATTRIBUTE_NORMAL, - NULL); - ++dwCounter; - } while(m_hFile == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_EXISTS); - } - - void ReOpen(void) { - Close(); - - m_hFile = CreateFile(szFileName, - GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - } - - void Close(void) { - if(m_hFile != INVALID_HANDLE_VALUE) { - CloseHandle(m_hFile); - m_hFile = INVALID_HANDLE_VALUE; - } - } - - void Write(const char *szText) { - if(m_hFile == INVALID_HANDLE_VALUE) - return; - - DWORD dwBytesToWrite = (DWORD)strlen(szText); - DWORD dwBytesWritten = 0; - - while (dwBytesWritten < dwBytesToWrite) { - OVERLAPPED overlapped; - memset(&overlapped, 0, sizeof(OVERLAPPED)); + File(const TCHAR *szName, const TCHAR *szExtension); + ~File(); - /* Write to end of file */ - overlapped.Offset = 0xffffffff; - overlapped.OffsetHigh = 0xffffffff; - - if(WriteFile(m_hFile, - szText + dwBytesWritten, - dwBytesToWrite - dwBytesWritten, - &dwBytesWritten, - &overlapped) == FALSE) { - Close(); - Open(TEXT("extra"), TEXT("xml")); - return; - } - } - } + void Open(const TCHAR *szName, const TCHAR *szExtension); + void ReOpen(void); + void Close(void); + void Write(const char *szText); private: HANDLE m_hFile; @@ -113,119 +45,29 @@ private: class Log : public File { public: - Log(const TCHAR *szName) : File(szName, TEXT("xml")) { - Write(""); - NewLine(); - Write(""); - NewLine(); - Write(""); - NewLine(); - } - - ~Log() { - Write(""); - NewLine(); - } - - void NewLine(void) { - Write("\r\n"); - } - - void Tag(const char *name) { - Write("<"); - Write(name); - Write("/>"); - } - - void BeginTag(const char *name) { - Write("<"); - Write(name); - Write(">"); - } + Log(const TCHAR *szName); + ~Log(); + void NewLine(void); + void Tag(const char *name); + void BeginTag(const char *name); void BeginTag(const char *name, - const char *attr1, const char *value1) { - Write("<"); - Write(name); - Write(" "); - Write(attr1); - Write("=\""); - Escape(value1); - Write("\">"); - } - + const char *attr1, const char *value1); void BeginTag(const char *name, const char *attr1, const char *value1, - const char *attr2, const char *value2) { - Write("<"); - Write(name); - Write(" "); - Write(attr1); - Write("=\""); - Escape(value1); - Write("\" "); - Write(attr2); - Write("=\""); - Escape(value2); - Write("\">"); - } - - void EndTag(const char *name) { - Write(""); - } - - void Text(const char *text) { - Escape(text); - } - - void TextF(const char *format, ...) { - char szBuffer[4196]; - va_list ap; - va_start(ap, format); - vsnprintf(szBuffer, sizeof(szBuffer), format, ap); - va_end(ap); - Escape(szBuffer); - } - - void BeginCall(const char *function) { - Write("\t"); - BeginTag("call", "name", function); - NewLine(); - } - - void EndCall(void) { - Write("\t"); - EndTag("call"); - NewLine(); - } - - void BeginParam(const char *name, const char *type) { - Write("\t\t"); - BeginTag("param", "name", name, "type", type); - } - - void EndParam(void) { - EndTag("param"); - NewLine(); - } - - void BeginReturn(const char *type) { - Write("\t\t"); - BeginTag("return", "type", type); - } - - void EndReturn(void) { - EndTag("return"); - NewLine(); - } + const char *attr2, const char *value2); + void EndTag(const char *name); + void Text(const char *text); + void TextF(const char *format, ...); + void BeginCall(const char *function); + void EndCall(void); + void BeginParam(const char *name, const char *type); + void EndParam(void); + void BeginReturn(const char *type); + void EndReturn(void); protected: - void Escape(const char *s) { - /* FIXME */ - Write(s); - } + void Escape(const char *s); }; diff --git a/windows.py b/windows.py index 29e4e60..79b835e 100644 --- a/windows.py +++ b/windows.py @@ -100,7 +100,6 @@ class Dll: def wrap_decl(self): print 'static HINSTANCE g_hDll = NULL;' - print 'Log * g_pLog = NULL;' print 'static TCHAR g_szDll[MAX_PATH] = {0};' print print 'BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);' -- 2.45.2