-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cc
More file actions
203 lines (172 loc) · 5.41 KB
/
main.cc
File metadata and controls
203 lines (172 loc) · 5.41 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <sstream>
#include <string>
#include <cstring>
struct BaseLogFormatter
{
virtual ~BaseLogFormatter() = default;
virtual std::ostringstream& Evaluate(const char* data, std::ostringstream&) const = 0;
};
// Knows how to format stored arguments, writing to an output stream
// Each instance of this class maps to a unique sequence of log arguments
template <typename ...T>
struct LogFormatter : BaseLogFormatter
{
LogFormatter(const char* formatString) : mFormatString(formatString) {}
const char* mFormatString;
template<typename Arg>
const char* FormatArg(std::ostringstream& outputStream, const char* argsData) const
{
const Arg* arg = reinterpret_cast<const Arg*>(argsData);
outputStream << *arg;
return argsData + sizeof(Arg);
}
template<typename ... Args>
typename std::enable_if<sizeof...(Args) == 0>::type
Format(std::ostringstream& outputStream, const char* formatString, const char*) const
{
outputStream << formatString;
}
template<typename Arg, typename ... Args>
void Format(auto& outputStream, const char* formatString, const char* argsData) const
{
const char* firstPlaceholder = std::strstr(formatString, "%");
// write from format to first placeholder
outputStream.write(formatString, firstPlaceholder - formatString);
// write corresponding argument
argsData = FormatArg<Arg>(outputStream, argsData);
// Move to the next data item
Format<Args...>(outputStream, firstPlaceholder + 1, argsData);
}
// given input data, write to stream
std::ostringstream& Evaluate(const char* argsData, std::ostringstream& outputStream) const override
{
Format<T...>(outputStream, mFormatString, argsData);
return outputStream;
}
};
// Knows how to write log data to a memory stream, given the static data
struct LogWriter
{
struct Header
{
// the log data (i.e. args)
char mBuffer[128];
// the object which knows how to format this data
const BaseLogFormatter* mLogFormatter;
};
static LogWriter& GetLogWriter()
{
static LogWriter logWriter;
return logWriter;
}
// return a new object which knows how to store data
template<typename T, typename ...Args>
T* CreateLogFormatter(Args&&... args)
{
return new T(std::forward<Args>(args)...);
}
// front end to write all arguments to a buffer
template<typename ... Args>
void Write(const BaseLogFormatter& logFormatter, const Args... args)
{
size_t argSize = GetArgsSize(args...);
char* buffer = GetLogBuffer(logFormatter, argSize);
CopyArgs(buffer, args...);
}
Header& GetNextHeader()
{
// incomplete
static Header sHeader;
return sHeader;
}
char* GetLogBuffer(const BaseLogFormatter& logFormatter, size_t sizeRequired)
{
// incomplete
Header& header = GetNextHeader();
header.mLogFormatter = &logFormatter;
return header.mBuffer;
}
// Note: we could have an option for non-trivially copyable args (SFINAE)
template <typename T>
char* CopyArg(char* buffer, T arg)
{
memcpy(buffer, &arg, sizeof(arg));
return buffer + sizeof(arg);
}
// base case for the format string
inline char* CopyArgs(char* buffer)
{
return buffer; // nothing to copy here
}
// write a single arg to the buffer and continue with the tail
template<typename Arg, typename ... Args>
char* CopyArgs(char* buffer, const Arg& arg, const Args&... args)
{
buffer = CopyArg(buffer, arg);
return CopyArgs(buffer, args...);
}
inline size_t GetArgsSize()
{
return 0;
}
template<typename Arg>
size_t GetArgSize(const Arg& arg)
{
return sizeof(arg);
}
template<typename Arg, typename... Args>
size_t GetArgsSize(const Arg& arg, const Args... args)
{
return GetArgSize(arg) + GetArgsSize(args...);
}
};
// front-end method to write a log entry
template <typename... Args>
void WriteLog(BaseLogFormatter** logFormatter, const char* formatString, const Args&... args)
{
LogWriter logWriter = LogWriter::GetLogWriter();
// find the object that knows how to write the args to the log buffer
if (*logFormatter == nullptr)
*logFormatter = logWriter.CreateLogFormatter<LogFormatter<Args...>>(formatString);
// write the args to the log buffer
logWriter.Write(**logFormatter, args...);
}
// Knows how to consume a record from the producer, given the static data
struct LogConsumer
{
static LogConsumer& GetLogConsumer()
{
static LogConsumer logConsumer;
return logConsumer;
}
void Consume(const LogWriter::Header& header, std::ostringstream& outputStream)
{
header.mLogFormatter->Evaluate(header.mBuffer, outputStream);
}
};
// util
template <typename... Types>
constexpr unsigned sizeof_args(Types&&...)
{
return sizeof...(Types);
}
// util
constexpr size_t CountPlaceholders(const char* formatString)
{
if (formatString[0] == '\0')
return 0;
return (formatString[0] == '%' ? 1u : 0u) + CountPlaceholders(formatString + 1);
}
// API
#define LOG(formatString, ...) \
static_assert(CountPlaceholders(formatString) == sizeof_args(__VA_ARGS__), "Number of arguments mismatch"); \
static BaseLogFormatter* sLogFormatter = nullptr; \
WriteLog(&sLogFormatter, formatString, ##__VA_ARGS__);
int main()
{
LOG("Hello int=% char=% float=%", 1, 'a', 42.3);
std::ostringstream outputStream;
LogConsumer::GetLogConsumer().Consume(LogWriter::GetLogWriter().GetNextHeader(), outputStream);
std::cout << outputStream.str() << std::endl;
}