5#include <source_location>
14 static std::string
Escape(
const std::string &value)
21 case 92: ret +=
"\\\\";
break;
22 case 34: ret +=
"\\\"";
break;
23 case 8: ret +=
"\\b";
break;
24 case 12: ret +=
"\\f";
break;
25 case 10: ret +=
"\\n";
break;
26 case 13: ret +=
"\\r";
break;
27 case 9: ret +=
"\\t";
break;
28 default: ret += c;
break;
34 static void Indent(std::ostream &os,
int indent)
36 for (
int i = 0; i < indent; i++) os <<
" ";
39 static void Comma(std::ostream &os,
bool &first,
int indent)
41 if (!first) os <<
",\n";
47 const std::string &name,
const std::string &value)
49 Comma(os, first, indent);
50 os <<
"\"" << name <<
"\": \"" <<
Escape(value) <<
"\"";
54 const std::string &name,
bool value)
56 Comma(os, first, indent);
57 os <<
"\"" << name <<
"\": " << (value ?
"true" :
"false");
61 const std::string &name,
int value)
63 Comma(os, first, indent);
64 os <<
"\"" << name <<
"\": " << value;
70 for (std::size_t i = 0; i < values.size(); i++)
73 os <<
"\"" <<
Escape(values[i]) <<
"\"";
79 const std::string &name,
80 const std::vector<std::string> &values)
82 Comma(os, first, indent);
83 os <<
"\"" << name <<
"\": ";
88 const std::source_location &location)
90 Comma(os, first, indent);
91 os <<
"\"source\": {";
92 os <<
"\"file\": \"" <<
Escape(location.file_name()) <<
"\", ";
93 os <<
"\"line\": " << location.line() <<
", ";
94 os <<
"\"function\": \"" <<
Escape(location.function_name()) <<
"\"";
static void WriteStringField(std::ostream &os, bool &first, int indent, const std::string &name, const std::string &value)
static void Comma(std::ostream &os, bool &first, int indent)
static void WriteBoolField(std::ostream &os, bool &first, int indent, const std::string &name, bool value)
static void WriteSourceField(std::ostream &os, bool &first, int indent, const std::source_location &location)
static std::string Escape(const std::string &value)
static void WriteIntField(std::ostream &os, bool &first, int indent, const std::string &name, int value)
static void Indent(std::ostream &os, int indent)
static void WriteStringArrayField(std::ostream &os, bool &first, int indent, const std::string &name, const std::vector< std::string > &values)
static void WriteStringArray(std::ostream &os, const std::vector< std::string > &values)