From: <man...@us...> - 2013-07-05 19:59:42
|
Revision: 2503 http://sourceforge.net/p/modplug/code/2503 Author: manxorist Date: 2013-07-05 19:59:35 +0000 (Fri, 05 Jul 2013) Log Message: ----------- [Ref] Logging actually goes nowhere in release builds, so we can as well just #define NO_LOGGING. [Ref] When LOG_TO_FILE is compiled in debug mode, also write timestamps. [Ref] Other small cleanups to DoLog(). Modified Paths: -------------- trunk/OpenMPT/common/BuildSettings.h trunk/OpenMPT/common/typedefs.cpp trunk/OpenMPT/common/typedefs.h Property Changed: ---------------- trunk/OpenMPT/mptrack/ Modified: trunk/OpenMPT/common/BuildSettings.h =================================================================== --- trunk/OpenMPT/common/BuildSettings.h 2013-07-05 16:55:57 UTC (rev 2502) +++ trunk/OpenMPT/common/BuildSettings.h 2013-07-05 19:59:35 UTC (rev 2503) @@ -69,7 +69,9 @@ //#define MODPLUG_NO_FILESAVE // Disable any debug logging -//#define NO_LOGGING +#ifndef _DEBUG +#define NO_LOGGING +#endif // Disable unarchiving support //#define NO_ARCHIVE_SUPPORT Modified: trunk/OpenMPT/common/typedefs.cpp =================================================================== --- trunk/OpenMPT/common/typedefs.cpp 2013-07-05 16:55:57 UTC (rev 2502) +++ trunk/OpenMPT/common/typedefs.cpp 2013-07-05 19:59:35 UTC (rev 2503) @@ -57,67 +57,123 @@ #endif +//#define LOG_TO_FILE + + static const std::size_t LOGBUF_SIZE = 1024; + +#if defined(MODPLUG_TRACKER) && defined(_DEBUG) + + +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::String::Format("%6u", (unsigned)(ms)); +} + + +#endif // MODPLUG_TRACKER + + static void DoLog(const char *file, int line, const char *function, const char *format, va_list args) //--------------------------------------------------------------------------------------------------- { -#if !defined(MODPLUG_TRACKER) || defined(_DEBUG) +#if !defined(MODPLUG_TRACKER) || (defined(MODPLUG_TRACKER) && defined(_DEBUG)) 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); + if(!file) + { + file = "unknown"; + } + if(!function) + { + function = ""; + } + // remove eol if already present + std::size_t len = std::strlen(message); + 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 defined(MODPLUG_TRACKER) - char buf2[LOGBUF_SIZE]; - char *verbose_message = nullptr; - if(file || function) + static uint64_t s_lastlogtime = 0; + uint64 cur = GetTimeMS(); + uint64 diff = cur - s_lastlogtime; + s_lastlogtime = cur; + #ifdef LOG_TO_FILE { - snprintf(buf2, LOGBUF_SIZE, "%s(%i): %s", file?file:"", line, message); - buf2[LOGBUF_SIZE - 1] = '\0'; - verbose_message = buf2; - } else - { - verbose_message = message; - } - OutputDebugString(verbose_message); - #ifdef LOG_TO_FILE - FILE *f = fopen("c:\\mptrack.log", "a"); - if(f) + static FILE * s_logfile = nullptr; + char verbose_message[LOGBUF_SIZE]; + snprintf(verbose_message, LOGBUF_SIZE, "%s+%s %s(%i): %s [%s]\n", TimeAsAsString(cur).c_str(), TimeDiffAsString(diff).c_str(), file, line, message, function); + verbose_message[LOGBUF_SIZE - 1] = '\0'; + if(!s_logfile) { - fwrite(verbose_message, 1, strlen(verbose_message), f); - fclose(f); + s_logfile = fopen("mptrack.log", "a"); } - #endif //LOG_TO_FILE - #else // !MODPLUG_TRACKER - std::size_t len = std::strlen(message); - // remove eol if already present - if(len > 0 && message[len-1] == '\n') - { - message[len-1] = '\0'; - len--; + if(s_logfile) + { + fprintf(s_logfile, "%s", verbose_message); + fflush(s_logfile); + } } - if(len > 0 && message[len-1] == '\r') + #endif // LOG_TO_FILE { - message[len-1] = '\0'; - len--; + char verbose_message[LOGBUF_SIZE]; + snprintf(verbose_message, LOGBUF_SIZE, "%s(%i): +%s %s [%s]\n", file, line, TimeDiffAsString(diff).c_str(), message, function); + verbose_message[LOGBUF_SIZE - 1] = '\0'; + OutputDebugString(verbose_message); } - 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; - } + #else // !MODPLUG_TRACKER + std::clog + << "openmpt: DEBUG: " + << file << "(" << line << ")" << ": " + << std::string(message) + << " [" << function << "]" + << std::endl; #endif // MODPLUG_TRACKER #else UNREFERENCED_PARAMETER(file); Modified: trunk/OpenMPT/common/typedefs.h =================================================================== --- trunk/OpenMPT/common/typedefs.h 2013-07-05 16:55:57 UTC (rev 2502) +++ trunk/OpenMPT/common/typedefs.h 2013-07-05 19:59:35 UTC (rev 2503) @@ -346,8 +346,8 @@ }; #define Log Logger(__FILE__, __LINE__, __FUNCTION__) #else // !NO_LOGGING -inline void MPT_PRINTF_FUNC(1,2) Log(const char *format, ...) {} -class Logger { public: void MPT_PRINTF_FUNC(2,3) operator () (const char *format, ...) {} }; +static inline void MPT_PRINTF_FUNC(1,2) Log(const char * /*format*/, ...) {} +class Logger { public: void MPT_PRINTF_FUNC(2,3) 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 Index: trunk/OpenMPT/mptrack =================================================================== --- trunk/OpenMPT/mptrack 2013-07-05 16:55:57 UTC (rev 2502) +++ trunk/OpenMPT/mptrack 2013-07-05 19:59:35 UTC (rev 2503) Property changes on: trunk/OpenMPT/mptrack ___________________________________________________________________ Added: svn:global-ignores ## -0,0 +1 ## +mptrack.log This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |