From: <man...@us...> - 2013-04-18 18:24:31
|
Revision: 1911 http://sourceforge.net/p/modplug/code/1911 Author: manxorist Date: 2013-04-18 18:24:19 +0000 (Thu, 18 Apr 2013) Log Message: ----------- [Ref] Move mpt::String::Format out of the header file. [Ref] Move Log() from mptrack/mptrack.cpp to common/typedefs.cpp [Ref] Make Log() print file and line number in addition to the log message to the debug window. [Ref] Add NO_LOGGING preprocessor macro to completely disable all debug logging (not set by default). [Ref] Add AlwaysAssertHandler for !MODPLUG_TRACKER. [Ref] Add C99 compatible c99_snprintf and c99_vsnprintf for msvc. Modified Paths: -------------- trunk/OpenMPT/common/mptString.h trunk/OpenMPT/common/serialization_utils.h trunk/OpenMPT/common/stdafx.h trunk/OpenMPT/common/typedefs.h trunk/OpenMPT/mptrack/CommandSet.cpp trunk/OpenMPT/mptrack/Mptrack.cpp trunk/OpenMPT/mptrack/Mptrack.h trunk/OpenMPT/mptrack/mptrack_08.vcproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters trunk/OpenMPT/soundlib/Load_med.cpp trunk/OpenMPT/soundlib/Load_mid.cpp trunk/OpenMPT/soundlib/Mmcmp.cpp trunk/OpenMPT/unarchiver/unlha.cpp Added Paths: ----------- trunk/OpenMPT/common/mptString.cpp trunk/OpenMPT/common/typedefs.cpp Added: trunk/OpenMPT/common/mptString.cpp =================================================================== --- trunk/OpenMPT/common/mptString.cpp (rev 0) +++ trunk/OpenMPT/common/mptString.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -0,0 +1,52 @@ +/* + * mptString.cpp + * ------------- + * Purpose: A wrapper around std::string implemeting the CString. + * Notes : Should be removed somewhen in the future when all uses of CString have been converted to std::string. + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#pragma once + +#include "stdafx.h" +#include "mptString.h" + +#include <vector> +#include <cstdarg> + + +namespace mpt { + +// Formats this string, like CString::Format. +void String::Format(const CharT* pszFormat, ...) +{ + #ifdef _MSC_VER + va_list argList; + va_start( argList, pszFormat ); + + // Count the needed array size. + const size_t nCount = _vscprintf(pszFormat, argList); // null character not included. + resize(nCount + 1); // + 1 is for null terminator. + + // Hack: directly modify the std::string's string. + // In C++11 std::string is guaranteed to be contiguous. + const int nCount2 = vsprintf_s(&*begin(), size(), pszFormat, argList); + resize(nCount2); // Removes the null character that vsprintf_s adds. + + va_end( argList ); + #else + va_list argList; + va_start(argList, pszFormat); + int size = vsnprintf(NULL, 0, pszFormat, argList); // get required size, requires c99 compliant vsnprintf which msvc does not have + va_end(argList); + std::vector<char> temp(size + 1); + va_start(argList, pszFormat); + vsnprintf(&(temp[0]), size + 1, pszFormat, argList); + va_end(argList); + assign(&(temp[0])); + #endif +} + +} // namespace mpt Property changes on: trunk/OpenMPT/common/mptString.cpp ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-c++src \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/OpenMPT/common/mptString.h =================================================================== --- trunk/OpenMPT/common/mptString.h 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/common/mptString.h 2013-04-18 18:24:19 UTC (rev 1911) @@ -64,21 +64,7 @@ } // Formats this string, like CString::Format. - void Format(const CharT* pszFormat, ...) - { - va_list argList; - va_start( argList, pszFormat ); - - // Count the needed array size. - const size_t nCount = _vscprintf(pszFormat, argList); // null character not included. - resize(nCount + 1); // + 1 is for null terminator. - - // Hack: directly modify the std::string's string. - // In C++11 std::string is guaranteed to be contiguous. - const int nCount2 = vsprintf_s(&*begin(), size(), pszFormat, argList); - resize(nCount2); // Removes the null character that vsprintf_s adds. - - va_end( argList ); - } + void Format(const CharT* pszFormat, ...); }; } + Modified: trunk/OpenMPT/common/serialization_utils.h =================================================================== --- trunk/OpenMPT/common/serialization_utils.h 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/common/serialization_utils.h 2013-04-18 18:24:19 UTC (rev 1911) @@ -234,7 +234,7 @@ bool GetFlag(Rwf flag) const {return m_Flags[flag];} // Write given string to log if log func is defined. - void Log(LPCTSTR psz) {if (m_fpLogFunc) m_fpLogFunc(psz);} + void AddToLog(LPCTSTR psz) {if (m_fpLogFunc) m_fpLogFunc(psz);} SsbStatus m_Status; uint32 m_nFixedEntrySize; // Read/write: If > 0, data entries have given fixed size. Modified: trunk/OpenMPT/common/stdafx.h =================================================================== --- trunk/OpenMPT/common/stdafx.h 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/common/stdafx.h 2013-04-18 18:24:19 UTC (rev 1911) @@ -33,7 +33,6 @@ #define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES_COUNT 1 #define _SCL_SECURE_NO_WARNINGS - #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC extensions #include <afxcmn.h> // MFC support for Windows Common Controls @@ -46,7 +45,6 @@ #include <mmreg.h> #include <msacm.h> #include <afxdlgs.h> -//#include <afxdhtml.h> #pragma warning(default:4201) #include <string> @@ -73,6 +71,9 @@ #endif // ENABLE_ASM +// Disable any debug logging +//#define NO_LOGGING + // Disable unarchiving support //#define NO_ARCHIVE_SUPPORT @@ -109,20 +110,11 @@ // Define to build without MP3 import support (via mpg123) //#define NO_MP3_SAMPLES - -void Log(LPCSTR format,...); - #include "../common/typedefs.h" -#include "../common/mptString.h" - // Exception type that is used to catch "operator new" exceptions. //typedef std::bad_alloc & MPTMemoryException; typedef CMemoryException * MPTMemoryException; -//To mark string that should be translated in case of multilingual version. -#define GetStrI18N(x) (x) - - //{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line. Added: trunk/OpenMPT/common/typedefs.cpp =================================================================== --- trunk/OpenMPT/common/typedefs.cpp (rev 0) +++ trunk/OpenMPT/common/typedefs.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -0,0 +1,147 @@ +/* + * typedefs.cpp + * ------------ + * Purpose: Basic data type definitions and assorted compiler-related helpers. + * Notes : (currently none) + * Authors: OpenMPT Devs + * The OpenMPT source code is released under the BSD license. Read LICENSE for more details. + */ + + +#include "stdafx.h" +#include "typedefs.h" +#include <iostream> + + +#ifndef MODPLUG_TRACKER +void AlwaysAssertHandler(const char *file, int line, const char *function, const char *expr) +//------------------------------------------------------------------------------------------ +{ + std::cerr + << "openmpt: ASSERTION FAILED: " + << file << "(" << line << ")" << ": " + << std::string(expr) + << " [" << function << "]" + << std::endl + ; +} +#endif + + +#ifdef _MSC_VER + +int c99_vsnprintf(char *str, size_t size, const char *format, va_list args) +{ + int ret; + va_list ap; + va_copy(ap, args); + ret = _vsnprintf(str, size, format, ap); + va_end(ap); + if ( ret < 0 ) { + str[size - 1] = '\0'; + ret = size; + } + return ret; +} + + +int c99_snprintf(char *str, size_t size, const char *format, ...) +{ + int ret; + va_list ap; + va_start( ap, format ); + ret = c99_vsnprintf(str, size, format, ap); + va_end( ap ); + return ret; +} + +#endif + + +static const std::size_t LOGBUF_SIZE = 1024; + +static void DoLog(const char *file, int line, const char *function, const char *format, va_list args) +//--------------------------------------------------------------------------------------------------- +{ +#if !defined(MODPLUG_TRACKER) || defined(_DEBUG) + char message[LOGBUF_SIZE]; + va_list va; + va_copy(va, args); + vsnprintf(message, LOGBUF_SIZE, format, va); + va_end(va); + #if defined(MODPLUG_TRACKER) + char buf2[LOGBUF_SIZE]; + char *verbose_message = nullptr; + if(file || function) + { + snprintf(buf2, LOGBUF_SIZE, "%s(%i): %s", file?file:"", line, message); + verbose_message = buf2; + } else + { + verbose_message = message; + } + OutputDebugString(verbose_message); + #ifdef LOG_TO_FILE + FILE *f = fopen("c:\\mptrack.log", "a"); + if(f) + { + fwrite(verbose_message, 1, strlen(verbose_message), f); + fclose(f); + } + #endif //LOG_TO_FILE + #else // !MODPLUG_TRACKER + std::size_t len = strlen(message); + // remove eol if already present + if(len > 0 && message[len-1] == '\n') + { + message[len-1] = '\0'; + len--; + } + if(len > 0 && message[len-1] == '\r') + { + message[len-1] = '\0'; + len--; + } + if(file || function) + { + std::clog + << "openmpt: DEBUG: " + << (file?file:"") << "(" << line << ")" << ": " + << std::string(message) + << " [" << (function?function:"") << "]" + << std::endl; + } else + { + std::clog + << "openmpt: DEBUG: " + << std::string(message) + << std::endl; + } + #endif // MODPLUG_TRACKER +#endif +} + + +#ifndef NO_LOGGING + +void Logger::operator () (const char *format, ...) +//------------------------------------------------ +{ + va_list va; + va_start(va, format); + DoLog(file, line, function, format, va); + va_end(va); +} + + +#undef Log +void Log(const char * format, ...) +//-------------------------------- +{ + va_list va; + va_start(va, format); + DoLog(nullptr, 0, nullptr, format, va); + va_end(va); +} + +#endif Property changes on: trunk/OpenMPT/common/typedefs.cpp ___________________________________________________________________ Added: svn:mime-type ## -0,0 +1 ## +text/x-c++src \ No newline at end of property Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Modified: trunk/OpenMPT/common/typedefs.h =================================================================== --- trunk/OpenMPT/common/typedefs.h 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/common/typedefs.h 2013-04-18 18:24:19 UTC (rev 1911) @@ -166,3 +166,48 @@ float32 f; uint32 i; }; + + +#include <cstdarg> +#ifdef _MSC_VER +#ifndef va_copy +#define va_copy(dst, src) do { (dst) = (src); } while (0) +#endif +#endif + + +#include "../common/mptString.h" + +//To mark string that should be translated in case of multilingual version. +#define GetStrI18N(x) (x) + + +#ifndef NO_LOGGING +void Log(const char *format, ...); +class Logger +{ +private: + const char * const file; + int const line; + const char * const function; +public: + Logger(const char *file_, int line_, const char *function_) : file(file_), line(line_), function(function_) {} + void operator () (const char *format, ...); +}; +#define Log Logger(__FILE__, __LINE__, __FUNCTION__) +#else // !NO_LOGGING +inline void Log(const char *format, ...) {} +class Logger { public: void operator () (const char *format, ...) {} }; +#define Log if(true) {} else Logger() // completely compile out arguments to Log() so that they do not even get evaluated +#endif // NO_LOGGING + +// just #undef Log in files, where this Log redefinition causes problems +//#undef Log + + +#include <stdio.h> +#ifdef _MSC_VER +int c99_vsnprintf(char *str, size_t size, const char *format, va_list args); +int c99_snprintf(char *str, size_t size, const char *format, ...); +#define snprintf c99_snprintf +#endif Modified: trunk/OpenMPT/mptrack/CommandSet.cpp =================================================================== --- trunk/OpenMPT/mptrack/CommandSet.cpp 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/mptrack/CommandSet.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -23,6 +23,7 @@ #if(ENABLE_LOGGING) // #else + #undef Log #define Log #endif Modified: trunk/OpenMPT/mptrack/Mptrack.cpp =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.cpp 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/mptrack/Mptrack.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -2278,31 +2278,6 @@ } -/////////////////////////////////////////////////////////////////////////////////// -// Debug - -void Log(LPCSTR format,...) -//------------------------- -{ - #ifdef _DEBUG - CHAR cBuf[1024]; - va_list va; - va_start(va, format); - wvsprintf(cBuf, format, va); - OutputDebugString(cBuf); - #ifdef LOG_TO_FILE - FILE *f = fopen("c:\\mptrack.log", "a"); - if (f) - { - fwrite(cBuf, 1, strlen(cBuf), f); - fclose(f); - } - #endif //LOG_TO_FILE - va_end(va); - #endif //_DEBUG -} - - ////////////////////////////////////////////////////////////////////////////////// // Localized strings Modified: trunk/OpenMPT/mptrack/Mptrack.h =================================================================== --- trunk/OpenMPT/mptrack/Mptrack.h 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/mptrack/Mptrack.h 2013-04-18 18:24:19 UTC (rev 1911) @@ -321,7 +321,6 @@ // Misc functions class CVstPlugin; -void Log(LPCSTR format,...); UINT MsgBox(UINT nStringID, CWnd *p=NULL, LPCSTR lpszTitle=NULL, UINT n=MB_OK); void ErrorBox(UINT nStringID, CWnd*p=NULL); Modified: trunk/OpenMPT/mptrack/mptrack_08.vcproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_08.vcproj 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/mptrack/mptrack_08.vcproj 2013-04-18 18:24:19 UTC (rev 1911) @@ -381,6 +381,14 @@ > </File> <File + RelativePath="..\common\mptString.cpp" + > + </File> + <File + RelativePath="..\common\typedefs.cpp" + > + </File> + <File RelativePath="..\soundlib\MixerSettings.cpp" > </File> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2013-04-18 18:24:19 UTC (rev 1911) @@ -246,9 +246,11 @@ <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> + <ClCompile Include="..\common\mptString.cpp" /> <ClCompile Include="..\common\Profiler.cpp" /> <ClCompile Include="..\common\Reporting.cpp" /> <ClCompile Include="..\common\serialization_utils.cpp" /> + <ClCompile Include="..\common\typedefs.cpp" /> <ClCompile Include="..\sounddev\SoundDevice.cpp" /> <ClCompile Include="..\sounddsp\AGC.cpp" /> <ClCompile Include="..\sounddsp\DSP.cpp" /> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2013-04-18 18:24:19 UTC (rev 1911) @@ -430,6 +430,12 @@ <ClCompile Include="Vstplug.cpp"> <Filter>Source Files\mptrack</Filter> </ClCompile> + <ClCompile Include="..\common\mptString.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> + <ClCompile Include="..\common\typedefs.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\soundlib\Loaders.h"> Modified: trunk/OpenMPT/soundlib/Load_med.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_med.cpp 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/soundlib/Load_med.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -14,10 +14,6 @@ //#define MED_LOG -#ifdef MED_LOG -extern void Log(LPCSTR s, ...); -#endif - #define MED_MAX_COMMENT_LENGTH 5*1024 //: Is 5 kB enough? // flags Modified: trunk/OpenMPT/soundlib/Load_mid.cpp =================================================================== --- trunk/OpenMPT/soundlib/Load_mid.cpp 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/soundlib/Load_mid.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -23,7 +23,6 @@ #ifdef MIDI_LOG //#define MIDI_DETAILED_LOG -extern void Log(LPCSTR, ...); #endif #define MIDI_DRUMCHANNEL 10 Modified: trunk/OpenMPT/soundlib/Mmcmp.cpp =================================================================== --- trunk/OpenMPT/soundlib/Mmcmp.cpp 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/soundlib/Mmcmp.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -14,11 +14,7 @@ //#define MMCMP_LOG -#ifdef MMCMP_LOG -extern void Log(LPCSTR s, ...); -#endif - BOOL XPK_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength); BOOL PP20_Unpack(LPCBYTE *ppMemFile, LPDWORD pdwMemLength); Modified: trunk/OpenMPT/unarchiver/unlha.cpp =================================================================== --- trunk/OpenMPT/unarchiver/unlha.cpp 2013-04-18 17:10:49 UTC (rev 1910) +++ trunk/OpenMPT/unarchiver/unlha.cpp 2013-04-18 18:24:19 UTC (rev 1911) @@ -43,7 +43,7 @@ #ifdef _DEBUG #define LHADEBUG -extern void Log(LPCSTR, ...); +extern void Log(const char *, ...); #endif #include "unlha/lharc.h" This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |