Logger: Tweaks

This commit is contained in:
Birunthan Mohanathas 2013-08-02 15:00:36 +03:00
parent a1f15d9adc
commit 7960dfe2ab
2 changed files with 43 additions and 142 deletions

View File

@ -21,6 +21,8 @@
#include "DialogAbout.h"
#include "Litestep.h"
#include "Rainmeter.h"
#include "Section.h"
#include "MeterWindow.h"
#include "System.h"
#include "resource.h"
@ -211,7 +213,7 @@ void Logger::Log(Level level, const WCHAR* source, const WCHAR* msg)
}
}
void Logger::LogF(Level level, const WCHAR* source, const WCHAR* format, va_list args)
void Logger::LogVF(Level level, const WCHAR* source, const WCHAR* format, va_list args)
{
WCHAR* buffer = new WCHAR[1024];
@ -232,7 +234,7 @@ void Logger::LogF(Level level, const WCHAR* source, const WCHAR* format, va_list
delete [] buffer;
}
void LogSection(Logger::Level level, Section* section, const WCHAR* format, va_list args)
void Logger::LogSectionVF(Logger::Level level, Section* section, const WCHAR* format, va_list args)
{
std::wstring source;
if (section)
@ -244,117 +246,16 @@ void LogSection(Logger::Level level, Section* section, const WCHAR* format, va_l
source += L" - ";
}
source += L"[";
source += L'[';
source += section->GetOriginalName();
source += L"]";
source += L']';
}
GetLogger().LogF(level, source.c_str(), format, args);
GetLogger().LogVF(level, source.c_str(), format, args);
}
void LogMeterWindow(Logger::Level level, MeterWindow* meterWindow, const WCHAR* format, va_list args)
void Logger::LogMeterWindowVF(Logger::Level level, MeterWindow* meterWindow, const WCHAR* format, va_list args)
{
std::wstring source;
if (meterWindow)
{
source = meterWindow->GetSkinPath();
}
GetLogger().LogF(level, source.c_str(), format, args);
}
void LogErrorF(const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
GetLogger().LogF(Logger::Level::Error, L"", format, args);
va_end(args);
}
void LogErrorF(Section* section, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogSection(Logger::Level::Error, section, format, args);
va_end(args);
}
void LogErrorF(MeterWindow* meterWindow, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogMeterWindow(Logger::Level::Error, meterWindow, format, args);
va_end(args);
}
void LogWarningF(const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
GetLogger().LogF(Logger::Level::Warning, L"", format, args);
va_end(args);
}
void LogWarningF(Section* section, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogSection(Logger::Level::Warning, section, format, args);
va_end(args);
}
void LogWarningF(MeterWindow* meterWindow, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogMeterWindow(Logger::Level::Warning, meterWindow, format, args);
va_end(args);
}
void LogNoticeF(const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
GetLogger().LogF(Logger::Level::Notice, L"", format, args);
va_end(args);
}
void LogNoticeF(Section* section, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogSection(Logger::Level::Notice, section, format, args);
va_end(args);
}
void LogNoticeF(MeterWindow* meterWindow, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogMeterWindow(Logger::Level::Notice, meterWindow, format, args);
va_end(args);
}
void LogDebugF(const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
GetLogger().LogF(Logger::Level::Debug, L"", format, args);
va_end(args);
}
void LogDebugF(Section* section, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogSection(Logger::Level::Debug, section, format, args);
va_end(args);
}
void LogDebugF(MeterWindow* meterWindow, const WCHAR* format, ...)
{
va_list args;
va_start(args, format);
LogMeterWindow(Logger::Level::Debug, meterWindow, format, args);
va_end(args);
const WCHAR* sourceSz = meterWindow ? meterWindow->GetSkinPath().c_str() : L"";
GetLogger().LogVF(level, sourceSz, format, args);
}

View File

@ -23,8 +23,9 @@
#include <cstdarg>
#include <string>
#include <list>
#include "Section.h"
#include "MeterWindow.h"
class Section;
class MeterWindow;
// Singleton class to handle and store log messages and control the log file.
class Logger
@ -58,7 +59,9 @@ public:
void SetLogToFile(bool logToFile);
void Log(Level level, const WCHAR* source, const WCHAR* msg);
void LogF(Level level, const WCHAR* source, const WCHAR* format, va_list args);
void LogVF(Level level, const WCHAR* source, const WCHAR* format, va_list args);
void LogMeterWindowVF(Logger::Level level, MeterWindow* meterWindow, const WCHAR* format, va_list args);
void LogSectionVF(Logger::Level level, Section* section, const WCHAR* format, va_list args);
const std::wstring& GetLogFilePath() { return m_LogFilePath; }
@ -85,42 +88,39 @@ private:
// Convenience functions.
inline Logger& GetLogger() { return Logger::GetInstance(); }
#define RM_LOGGER_DEFINE_LOG_FUNCTION(name) \
#define RM_LOGGER_DEFINE_LOG_FUNCTIONS(name) \
inline void Log ## name(const WCHAR* msg) \
{ \
GetLogger().Log(Logger::Level::name, L"", msg); \
} \
/* \
template<typename... Args> \
inline void Log ## name ## F(const WCHAR* format, Args... args) \
\
inline void Log ## name ## F(const WCHAR* format, ...) \
{ \
GetInstance().LogF(Logger::Level::name, args...); \
va_list args; \
va_start(args, format); \
GetLogger().LogVF(Logger::Level::Warning, L"", format, args); \
va_end(args); \
} \
\
inline void Log ## name ## F(Section* section, const WCHAR* format, ...) \
{ \
va_list args; \
va_start(args, format); \
GetLogger().LogSectionVF(Logger::Level::Error, section, format, args); \
va_end(args); \
} \
\
inline void Log ## name ## F(MeterWindow* meterWindow, const WCHAR* format, ...) \
{ \
va_list args; \
va_start(args, format); \
GetLogger().LogMeterWindowVF(Logger::Level::Error, meterWindow, format, args); \
va_end(args); \
}
*/
RM_LOGGER_DEFINE_LOG_FUNCTION(Error)
RM_LOGGER_DEFINE_LOG_FUNCTION(Warning)
RM_LOGGER_DEFINE_LOG_FUNCTION(Notice)
RM_LOGGER_DEFINE_LOG_FUNCTION(Debug)
// FIXME: Temporary solution until VS support variadic templates.
void LogSection(Logger::Level level, Section* section, const WCHAR* format, va_list args);
void LogMeterWindow(Logger::Level level, MeterWindow* meterWindow, const WCHAR* format, va_list args);
void LogErrorF(const WCHAR* format, ...);
void LogErrorF(Section* section, const WCHAR* format, ...);
void LogErrorF(MeterWindow* meterWindow, const WCHAR* format, ...);
void LogWarningF(const WCHAR* format, ...);
void LogWarningF(Section* section, const WCHAR* format, ...);
void LogWarningF(MeterWindow* meterWindow, const WCHAR* format, ...);
void LogNoticeF(const WCHAR* format, ...);
void LogNoticeF(Section* section, const WCHAR* format, ...);
void LogNoticeF(MeterWindow* meterWindow, const WCHAR* format, ...);
void LogDebugF(const WCHAR* format, ...);
void LogDebugF(Section* section, const WCHAR* format, ...);
void LogDebugF(MeterWindow* meterWindow, const WCHAR* format, ...);
RM_LOGGER_DEFINE_LOG_FUNCTIONS(Error)
RM_LOGGER_DEFINE_LOG_FUNCTIONS(Warning)
RM_LOGGER_DEFINE_LOG_FUNCTIONS(Notice)
RM_LOGGER_DEFINE_LOG_FUNCTIONS(Debug)
#endif