@@ -145,6 +145,19 @@ struct LogManager::Impl {
145
145
_rotatableFileBackend->set_formatter (TextFormatter ());
146
146
}
147
147
148
+ template <class Formatter >
149
+ void setFormatterToAllBackends () {
150
+ _consoleBackend->set_formatter (Formatter ());
151
+ _globalLogCacheBackend->set_formatter (Formatter ());
152
+ _startupWarningsBackend->set_formatter (Formatter ());
153
+ if (_rotatableFileBackend)
154
+ _rotatableFileBackend->set_formatter (Formatter ());
155
+ #ifndef _WIN32
156
+ if (_syslogBackend)
157
+ _syslogBackend->set_formatter (Formatter ());
158
+ #endif
159
+ }
160
+
148
161
LogDomain _globalDomain{std::make_unique<LogDomainGlobal>()};
149
162
// I think that, technically, these are logging front ends
150
163
// and that they get to hold or wrap a backend
@@ -155,6 +168,7 @@ struct LogManager::Impl {
155
168
#endif
156
169
boost::shared_ptr<RamLogBackend> _globalLogCacheBackend;
157
170
boost::shared_ptr<RamLogBackend> _startupWarningsBackend;
171
+ LogFormat _format{LogFormat::kDefault };
158
172
bool _defaultBackendsAttached{false };
159
173
};
160
174
@@ -182,6 +196,24 @@ LogDomain& LogManager::getGlobalDomain() {
182
196
return _impl->_globalDomain ;
183
197
}
184
198
199
+ void LogManager::setOutputFormat (LogFormat format) {
200
+ if (_impl->_format != format) {
201
+ switch (format) {
202
+ case LogFormat::kText :
203
+ _impl->setFormatterToAllBackends <TextFormatter>();
204
+ break ;
205
+
206
+ case LogFormat::kJson :
207
+ _impl->setFormatterToAllBackends <JsonFormatter>();
208
+ break ;
209
+
210
+ default :
211
+ break ;
212
+ };
213
+ _impl->_format = format;
214
+ }
215
+ }
216
+
185
217
void LogManager::detachDefaultBackends () {
186
218
invariant (isDefaultBackendsAttached ());
187
219
0 commit comments