From: <man...@us...> - 2014-02-28 13:13:07
|
Revision: 3792 http://sourceforge.net/p/modplug/code/3792 Author: manxorist Date: 2014-02-28 13:12:54 +0000 (Fri, 28 Feb 2014) Log Message: ----------- [Ref] Move logging code to its own file Logging.cpp and cleanup the preprocessor mess. [Ref] Refactor the (file,line,function) tuple into its own class mpt::log::Context . Modified Paths: -------------- trunk/OpenMPT/build/android_ndk/Android.mk trunk/OpenMPT/common/Logging.h trunk/OpenMPT/common/typedefs.cpp trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters trunk/OpenMPT/mptrack/mptrack_10.vcxproj trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters Added Paths: ----------- trunk/OpenMPT/common/Logging.cpp Modified: trunk/OpenMPT/build/android_ndk/Android.mk =================================================================== --- trunk/OpenMPT/build/android_ndk/Android.mk 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/build/android_ndk/Android.mk 2014-02-28 13:12:54 UTC (rev 3792) @@ -17,6 +17,7 @@ LOCAL_SRC_FILES := \ common/AudioCriticalSection.cpp \ common/stdafx.cpp \ + common/Logging.cpp \ common/misc_util.cpp \ common/mptPathString.cpp \ common/mptString.cpp \ Added: trunk/OpenMPT/common/Logging.cpp =================================================================== --- trunk/OpenMPT/common/Logging.cpp (rev 0) +++ trunk/OpenMPT/common/Logging.cpp 2014-02-28 13:12:54 UTC (rev 3792) @@ -0,0 +1,186 @@ +/* + * Logging.cpp + * ----------- + * Purpose: General logging and user confirmation support + * 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 "Logging.h" + +#include <iostream> +#include <cstring> + + +//#define LOG_TO_FILE + + +namespace mpt +{ +namespace log +{ + + +#ifndef NO_LOGGING + + +static const std::size_t LOGBUF_SIZE = 1024; + + +Context::Context(const char *file, int line, const char *function) +//---------------------------------------------------------------- + : file(file) + , line(line) + , function(function) +{ + return; +} + + +Context::Context(const Context &c) +//-------------------------------- + : file(c.file) + , line(c.line) + , function(c.function) +{ + return; +} + + +#if defined(MODPLUG_TRACKER) + + +static uint64 GetTimeMS() +//----------------------- +{ + FILETIME filetime; + GetSystemTimeAsFileTime(&filetime); + return ((uint64)filetime.dwHighDateTime << 32 | filetime.dwLowDateTime) / 10000; +} + + +static std::string TimeAsAsString(uint64 ms) +//------------------------------------------ +{ + + FILETIME filetime; + SYSTEMTIME systime; + filetime.dwHighDateTime = (DWORD)(((uint64)ms * 10000) >> 32); + filetime.dwLowDateTime = (DWORD)((uint64)ms * 10000); + FileTimeToSystemTime(&filetime, &systime); + + std::string result; + TCHAR buf[LOGBUF_SIZE]; + + GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &systime, "yyyy-MM-dd ", buf, LOGBUF_SIZE); + result.append(buf); + + GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &systime, "HH:mm:ss.", buf, LOGBUF_SIZE); + result.append(buf); + + sprintf(buf, "%03u", (unsigned)systime.wMilliseconds); + result.append(buf); + + return result; +} + + +static std::string TimeDiffAsString(uint64 ms) +//-------------------------------------------- +{ + return mpt::fmt::dec<6>(ms); +} + + +static std::wstring TimeDiffAsStringW(uint64 ms) +//---------------------------------------------- +{ + return mpt::wfmt::dec<6>(ms); +} + + +#endif // MODPLUG_TRACKER + + +static noinline void DoLog(const mpt::log::Context &context, std::wstring message) +//-------------------------------------------------------------------------------- +{ + // remove eol if already present + message = mpt::String::RTrim(message, L"\r\n"); + #if defined(MODPLUG_TRACKER) + static uint64_t s_lastlogtime = 0; + uint64 cur = GetTimeMS(); + uint64 diff = cur - s_lastlogtime; + s_lastlogtime = cur; + #ifdef LOG_TO_FILE + { + static FILE * s_logfile = nullptr; + if(!s_logfile) + { + s_logfile = mpt_fopen(MPT_PATHSTRING("mptrack.log"), "a"); + } + if(s_logfile) + { + fprintf(s_logfile, "%s+%s %s(%i): %s [%s]\n", TimeAsAsString(cur).c_str(), TimeDiffAsString(diff).c_str(), context.file, context.line, mpt::To(mpt::CharsetUTF8, message).c_str(), context.function); + fflush(s_logfile); + } + } + #endif // LOG_TO_FILE + { + OutputDebugStringW(mpt::String::PrintW(L"%1(%2): +%3 %4 [%5]\n", mpt::ToWide(mpt::CharsetASCII, context.file), context.line, TimeDiffAsStringW(diff), message, mpt::ToWide(mpt::CharsetASCII, context.function)).c_str()); + } + #else // !MODPLUG_TRACKER + std::clog + << "openmpt: " + << context.file << "(" << context.line << ")" << ": " + << mpt::ToLocale(message) + << " [" << context.function << "]" + << std::endl; + #endif // MODPLUG_TRACKER +} + + +static noinline void DoLog(const mpt::log::Context &context, const char *format, va_list args) +//-------------------------------------------------------------------------------------------- +{ + char message[LOGBUF_SIZE]; + va_list va; + va_copy(va, args); + vsnprintf(message, LOGBUF_SIZE, format, va); + message[LOGBUF_SIZE - 1] = '\0'; + va_end(va); + DoLog(context, mpt::ToWide(mpt::CharsetLocale, message)); +} + + +void Logger::operator () (const char *format, ...) +//------------------------------------------------ +{ + va_list va; + va_start(va, format); + DoLog(context, format, va); + va_end(va); +} + +void Logger::operator () (const std::string &text) +//------------------------------------------------ +{ + DoLog(context, mpt::ToWide(mpt::CharsetLocale, text)); +} + +void Logger::operator () (const std::wstring &text) +//------------------------------------------------- +{ + DoLog(context, text); +} + + +#endif // !NO_LOGGING + + +} // namespace log +} // namespace mpt Property changes on: trunk/OpenMPT/common/Logging.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/Logging.h =================================================================== --- trunk/OpenMPT/common/Logging.h 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/common/Logging.h 2014-02-28 13:12:54 UTC (rev 3792) @@ -41,27 +41,47 @@ }; + +namespace mpt +{ +namespace log +{ + + #ifndef NO_LOGGING -void MPT_PRINTF_FUNC(1,2) Log(const char *format, ...); -void Log(const std::string &text); -void Log(const std::wstring &text); -class Logger + + +class Context { -private: +public: const char * const file; - int const line; + const int line; const char * const function; public: - Logger(const char *file_, int line_, const char *function_) : file(file_), line(line_), function(function_) {} + Context(const char *file, int line, const char *function); + Context(const Context &c); +}; // class Context + +#define MPT_LOG_CURRENTCONTEXT() ::mpt::log::Context( __FILE__ , __LINE__ , __FUNCTION__ ) + + +class Logger +{ +private: + const Context &context; +public: + Logger(const Context &context) : context(context) {} void MPT_PRINTF_FUNC(2,3) operator () (const char *format, ...); void operator () (const std::string &text); void operator () (const std::wstring &text); }; -#define Log Logger(__FILE__, __LINE__, __FUNCTION__) + +#define Log ::mpt::log::Logger(MPT_LOG_CURRENTCONTEXT()) + + #else // !NO_LOGGING -static inline void MPT_PRINTF_FUNC(1,2) Log(const char * /*format*/, ...) {} -static inline void Log(const std::string & /*text*/ ) {} -static inline void Log(const std::wstring & /*text*/ ) {} + + class Logger { public: @@ -69,9 +89,12 @@ inline void operator () (const std::string & /*text*/ ) {} inline void operator () (const std::wstring & /*text*/ ) {} }; -#define Log if(true) {} else Logger() // completely compile out arguments to Log() so that they do not even get evaluated + +#define Log if(true) {} else ::mpt::log::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 +} // namespace log +} // namespace mpt Modified: trunk/OpenMPT/common/typedefs.cpp =================================================================== --- trunk/OpenMPT/common/typedefs.cpp 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/common/typedefs.cpp 2014-02-28 13:12:54 UTC (rev 3792) @@ -10,8 +10,7 @@ #include "stdafx.h" #include "typedefs.h" -#include <iostream> -#include <cstring> +#include "Logging.h" #if !defined(MODPLUG_TRACKER) && defined(MPT_ASSERT_HANDLER_NEEDED) @@ -21,190 +20,12 @@ { if(msg) { - Logger(file, line, function)("ASSERTION FAILED: %s (%s)", msg, expr); + mpt::log::Logger(mpt::log::Context(file, line, function))("ASSERTION FAILED: %s (%s)", msg, expr); } else { - Logger(file, line, function)("ASSERTION FAILED: %s", expr); + mpt::log::Logger(mpt::log::Context(file, line, function))("ASSERTION FAILED: %s", expr); } } #endif // !MODPLUG_TRACKER && MPT_ASSERT_HANDLER_NEEDED - -//#define LOG_TO_FILE - - -static const std::size_t LOGBUF_SIZE = 1024; - - -#if defined(MODPLUG_TRACKER) && !defined(NO_LOGGING) - - -static uint64 GetTimeMS() -//----------------------- -{ - FILETIME filetime; - GetSystemTimeAsFileTime(&filetime); - return ((uint64)filetime.dwHighDateTime << 32 | filetime.dwLowDateTime) / 10000; -} - -static std::string TimeAsAsString(uint64 ms) -//------------------------------------------ -{ - - FILETIME filetime; - SYSTEMTIME systime; - filetime.dwHighDateTime = (DWORD)(((uint64)ms * 10000) >> 32); - filetime.dwLowDateTime = (DWORD)((uint64)ms * 10000); - FileTimeToSystemTime(&filetime, &systime); - - std::string result; - TCHAR buf[LOGBUF_SIZE]; - - GetDateFormat(LOCALE_SYSTEM_DEFAULT, 0, &systime, "yyyy-MM-dd ", buf, LOGBUF_SIZE); - result.append(buf); - - GetTimeFormat(LOCALE_SYSTEM_DEFAULT, TIME_FORCE24HOURFORMAT, &systime, "HH:mm:ss.", buf, LOGBUF_SIZE); - result.append(buf); - - sprintf(buf, "%03u", (unsigned)systime.wMilliseconds); - result.append(buf); - - return result; -} - -static std::string TimeDiffAsString(uint64 ms) -//-------------------------------------------- -{ - return mpt::fmt::dec<6>(ms); -} - - -static std::wstring TimeDiffAsStringW(uint64 ms) -//---------------------------------------------- -{ - return mpt::wfmt::dec<6>(ms); -} - - -#endif // MODPLUG_TRACKER - - -static noinline void DoLog(const char *file, int line, const char *function, std::wstring message) -//------------------------------------------------------------------------------------------------ -{ -#if !defined(MODPLUG_TRACKER) || (defined(MODPLUG_TRACKER) && !defined(NO_LOGGING)) - if(!file) - { - file = "unknown"; - } - if(!function) - { - function = ""; - } - // remove eol if already present - message = mpt::String::RTrim(message, L"\r\n"); - #if defined(MODPLUG_TRACKER) - static uint64_t s_lastlogtime = 0; - uint64 cur = GetTimeMS(); - uint64 diff = cur - s_lastlogtime; - s_lastlogtime = cur; - #ifdef LOG_TO_FILE - { - static FILE * s_logfile = nullptr; - if(!s_logfile) - { - s_logfile = mpt_fopen(MPT_PATHSTRING("mptrack.log"), "a"); - } - if(s_logfile) - { - fprintf(s_logfile, "%s+%s %s(%i): %s [%s]\n", TimeAsAsString(cur).c_str(), TimeDiffAsString(diff).c_str(), file, line, mpt::To(mpt::CharsetUTF8, message).c_str(), function); - fflush(s_logfile); - } - } - #endif // LOG_TO_FILE - { - OutputDebugStringW(mpt::String::PrintW(L"%1(%2): +%3 %4 [%5]\n", mpt::ToWide(mpt::CharsetASCII, file), line, TimeDiffAsStringW(diff), message, mpt::ToWide(mpt::CharsetASCII, function)).c_str()); - } - #else // !MODPLUG_TRACKER - std::clog - << "openmpt: " - << file << "(" << line << ")" << ": " - << mpt::ToLocale(message) - << " [" << function << "]" - << std::endl; - #endif // MODPLUG_TRACKER -#else - MPT_UNREFERENCED_PARAMETER(file); - MPT_UNREFERENCED_PARAMETER(line); - MPT_UNREFERENCED_PARAMETER(function); - MPT_UNREFERENCED_PARAMETER(message); -#endif -} - - -static noinline void DoLog(const char *file, int line, const char *function, const char *format, va_list args) -//------------------------------------------------------------------------------------------------------------ -{ -#if !defined(MODPLUG_TRACKER) || (defined(MODPLUG_TRACKER) && !defined(NO_LOGGING)) - char message[LOGBUF_SIZE]; - va_list va; - va_copy(va, args); - vsnprintf(message, LOGBUF_SIZE, format, va); - message[LOGBUF_SIZE - 1] = '\0'; - va_end(va); - DoLog(file, line, function, mpt::ToWide(mpt::CharsetLocale, message)); -#else - MPT_UNREFERENCED_PARAMETER(file); - MPT_UNREFERENCED_PARAMETER(line); - MPT_UNREFERENCED_PARAMETER(function); - MPT_UNREFERENCED_PARAMETER(format); - MPT_UNREFERENCED_PARAMETER(args); -#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); -} -void Logger::operator () (const std::string &text) -//------------------------------------------------ -{ - DoLog(file, line, function, mpt::ToWide(mpt::CharsetLocale, text)); -} -void Logger::operator () (const std::wstring &text) -//------------------------------------------------- -{ - DoLog(file, line, function, text); -} - - -#undef Log -void Log(const char * format, ...) -//-------------------------------- -{ - va_list va; - va_start(va, format); - DoLog(nullptr, 0, nullptr, format, va); - va_end(va); -} -void Log(const std::string &text) -//------------------------------- -{ - DoLog(nullptr, 0, nullptr, mpt::ToWide(mpt::CharsetLocale, text)); -} -void Log(const std::wstring &text) -//-------------------------------- -{ - DoLog(nullptr, 0, nullptr, text); -} - -#endif // !NO_LOGGING - Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj 2014-02-28 13:12:54 UTC (rev 3792) @@ -254,6 +254,7 @@ </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp" /> + <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/libopenmpt/libopenmpt.vcxproj.filters 2014-02-28 13:12:54 UTC (rev 3792) @@ -502,5 +502,8 @@ <ClCompile Include="libopenmpt_ext.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="..\common\Logging.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj 2014-02-28 13:12:54 UTC (rev 3792) @@ -264,6 +264,7 @@ </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp" /> + <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/libopenmpt/libopenmptDLL.vcxproj.filters 2014-02-28 13:12:54 UTC (rev 3792) @@ -508,5 +508,8 @@ <ClCompile Include="libopenmpt_ext.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="..\common\Logging.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj 2014-02-28 13:12:54 UTC (rev 3792) @@ -258,6 +258,7 @@ </ItemGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp" /> + <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> Modified: trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters =================================================================== --- trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/libopenmpt/libopenmpt_test.vcxproj.filters 2014-02-28 13:12:54 UTC (rev 3792) @@ -505,5 +505,8 @@ <ClCompile Include="libopenmpt_ext.cpp"> <Filter>Source Files</Filter> </ClCompile> + <ClCompile Include="..\common\Logging.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> </ItemGroup> </Project> \ No newline at end of file Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj 2014-02-28 13:12:54 UTC (rev 3792) @@ -445,6 +445,7 @@ </ItemDefinitionGroup> <ItemGroup> <ClCompile Include="..\common\AudioCriticalSection.cpp" /> + <ClCompile Include="..\common\Logging.cpp" /> <ClCompile Include="..\common\misc_util.cpp" /> <ClCompile Include="..\common\mptPathString.cpp" /> <ClCompile Include="..\common\mptString.cpp" /> Modified: trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters =================================================================== --- trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2014-02-28 12:07:16 UTC (rev 3791) +++ trunk/OpenMPT/mptrack/mptrack_10.vcxproj.filters 2014-02-28 13:12:54 UTC (rev 3792) @@ -499,6 +499,9 @@ <ClCompile Include="..\soundlib\plugins\DmoToVst.cpp"> <Filter>Source Files\soundlib\plugins</Filter> </ClCompile> + <ClCompile Include="..\common\Logging.cpp"> + <Filter>Source Files\common</Filter> + </ClCompile> </ItemGroup> <ItemGroup> <ClInclude Include="..\soundlib\Loaders.h"> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |