14 #ifndef THRILL_COMMON_JSON_LOGGER_HEADER 15 #define THRILL_COMMON_JSON_LOGGER_HEADER 21 #include <initializer_list> 34 template <
typename Type>
63 template <
typename Type>
83 template <
typename... Args>
89 template <
typename Type>
97 JsonLogger* super_ =
nullptr;
100 std::unique_ptr<std::ostream>
os_;
111 template <
typename Type>
124 : logger_(logger), os_(os) {
126 lock_ = std::unique_lock<std::mutex>(logger_->mutex_);
135 : logger_(o.logger_), lock_(
std::move(o.lock_)),
136 os_(o.os_), items_(o.items_), sub_dict_(o.sub_dict_)
137 { o.logger_ =
nullptr; }
143 template <
typename Type>
167 if (logger_ && items_ != 0) {
168 assert(items_ % 2 == 0);
169 os_ <<
'}' << std::endl;
172 else if (!logger_ && sub_dict_) {
176 else if (!logger_ && sub_array_) {
183 size_t items()
const {
return items_; }
186 template <
typename Key>
196 template <
typename Key>
217 os_ << (items_ % 2 == 0 ?
',' :
':');
225 case '\\': os_ <<
'\\' <<
'\\';
227 case '"': os_ <<
'\\' <<
'"';
229 case '/': os_ <<
'\\' <<
'/';
231 case '\b': os_ <<
'\\' <<
'b';
233 case '\f': os_ <<
'\\' <<
'f';
235 case '\n': os_ <<
'\\' <<
'n';
237 case '\r': os_ <<
'\\' <<
'r';
239 case '\t': os_ <<
'\\' <<
't';
255 : os_(parent.os_), sub_dict_(true) { }
259 : os_(parent.os_), sub_array_(true) { }
269 bool sub_dict_ =
false;
272 bool sub_array_ =
false;
280 line.
os_ << (value ?
"true" :
"false");
337 for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
343 template <
typename Type, std::
size_t N>
347 for (
size_t i = 0; i <
N; ++i) {
348 if (i != 0) line.
os_ <<
',';
355 template <
typename Type>
359 for (
typename std::initializer_list<Type>::const_iterator it = list.begin();
360 it != list.end(); ++it) {
361 if (it != list.begin())
369 template <
typename Type>
373 for (
typename std::vector<Type>::const_iterator it = vec.begin();
374 it != vec.end(); ++it) {
375 if (it != vec.begin())
383 template <
typename Type, std::
size_t N>
387 for (
typename std::array<Type, N>::const_iterator it = arr.begin();
388 it != arr.end(); ++it) {
389 if (it != arr.begin())
406 template <
typename Type>
409 return Put(*
this, t);
415 template <
typename... Args>
419 std::ostringstream oss;
430 template <
typename Type>
440 #endif // !THRILL_COMMON_JSON_LOGGER_HEADER JsonLine operator<<(const Type &t)
JsonVerbatim(const std::string &str=std::string())
A special class to output verbatim text.
Type[] Array
A template to make writing temporary arrays easy: Array<int>{ 1, 2, 3 }.
JsonLine(JsonLogger *logger, std::ostream &os)
ctor: bind output
JsonLine obj()
return JsonLine has sub-dictionary of this one
JsonLogger()=default
open JsonLogger with ofstream uninitialized to discard log output.
void Close()
close the line
size_t items() const
number of items already put
A special class to output verbatim text.
std::mutex mutex_
mutex to lock logger output
JsonLine line()
create new JsonLine instance which will be written to this logger.
~JsonLine()
destructor: deliver to output
JsonLine(JsonLine &&o)
move-constructor: unlink pointer
std::ostream & os_
reference to output stream
void PutSeparator()
put an items separator (either ',' or ':') and increment counter.
JsonLine(struct ArrayTag, JsonLine &parent)
construct sub-dictionary
std::unique_lock< std::mutex > lock_
lock on the logger output stream
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
JsonVerbatim common_
common items outputted to each line
JsonLine & operator<<(Type const &t)
output any type
void PutEscapedChar(char ch)
JsonLine sub(const Key &key)
return JsonLine has sub-dictionary of this one
std::unique_ptr< std::ostream > os_
direct output stream for top loggers
static JsonLine & Put(JsonLine &line, bool const &value)
size_t items_
items counter for output stream
JsonLogger is a receiver of JSON output objects for logging.
A special class to output verbatim text.
const struct ArrayTag ArrayTag
global const ArrayTag instance
JsonLine arr(const Key &key)
return JsonLine has sub-dictionary of this one
JsonBeginObj(const std::string &str=std::string())
std::ostream & operator<<(std::ostream &os, const DIABase &d)
make ostream-able.
JsonLine is an object used to aggregate a set of key:value pairs for output into a JSON log...
JsonLine(struct DictionaryTag, JsonLine &parent)
construct sub-dictionary