forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.h
139 lines (116 loc) · 3.85 KB
/
Logger.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* Copyright (C) 2013 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#ifndef RM_LOGGER_H_
#define RM_LOGGER_H_
#include <Windows.h>
#include <cstdarg>
#include <string>
#include <list>
#include <chrono>
class Section;
class Skin;
class Measure;
// Singleton class to handle and store log messages and control the log file.
class Logger
{
public:
enum class Level
{
Error = 1,
Warning = 2,
Notice = 3,
Debug = 4
};
struct Entry
{
Level level;
std::wstring timestamp;
std::wstring source;
std::wstring message;
};
static Logger& GetInstance();
void SetLogFilePath(std::wstring path) { m_LogFilePath = path; }
void StartLogFile();
void StopLogFile();
void DeleteLogFile();
bool IsLogToFile() { return m_LogToFile; }
void SetLogToFile(bool logToFile);
void Log(Logger::Entry* entry);
void Log(Level level, const WCHAR* source, const WCHAR* msg);
void LogVF(Level level, const WCHAR* source, const WCHAR* format, va_list args);
void LogSkinVF(Logger::Level level, Skin* skin, const WCHAR* format, va_list args);
void LogSkinSVF(Logger::Level level, Skin* skin, const WCHAR* section, const WCHAR* format, va_list args);
void LogSection(Logger::Level level, Section* section, const WCHAR* message);
void LogSectionVF(Logger::Level level, Section* section, const WCHAR* format, va_list args);
void LogMeasureVF(Logger::Level level, Measure* section, const WCHAR* format, va_list args);
const std::wstring& GetLogFilePath() { return m_LogFilePath; }
const std::list<Entry>& GetEntries() { return m_Entries; }
private:
void LogInternal(Level level, std::chrono::system_clock::time_point timestamp, const WCHAR* source, const WCHAR* msg);
// Appends |entry| to the log file.
void WriteToLogFile(Entry& entry);
Logger();
~Logger();
Logger(const Logger& other) = delete;
Logger& operator=(Logger other) = delete;
bool m_LogToFile;
std::wstring m_LogFilePath;
std::list<Entry> m_Entries;
CRITICAL_SECTION m_CsLog;
CRITICAL_SECTION m_CsLogDelay;
};
// Convenience functions.
inline Logger& GetLogger() { return Logger::GetInstance(); }
#define RM_LOGGER_DEFINE_LOG_FUNCTIONS(name) \
inline void Log ## name(const WCHAR* msg) \
{ \
GetLogger().Log(Logger::Level::name, L"", msg); \
} \
\
inline void Log ## name ## F(const WCHAR* format, ...) \
{ \
va_list args; \
va_start(args, format); \
GetLogger().LogVF(Logger::Level::name, L"", format, args); \
va_end(args); \
} \
inline void Log ## name ## SF(Skin* skin, const WCHAR* section, const WCHAR* format, ...) \
{ \
va_list args; \
va_start(args, format); \
GetLogger().LogSkinSVF(Logger::Level::name, skin, section, 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::name, section, format, args); \
va_end(args); \
} \
\
inline void Log ## name ## F(Skin* skin, const WCHAR* format, ...) \
{ \
va_list args; \
va_start(args, format); \
GetLogger().LogSkinVF(Logger::Level::name, skin, format, args); \
va_end(args); \
} \
\
inline void Log ## name ## F(Measure* measure, const WCHAR* format, ...) \
{ \
va_list args; \
va_start(args, format); \
GetLogger().LogMeasureVF(Logger::Level::name, measure, format, args); \
va_end(args); \
}
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