Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Roberts <vieuxtech@gmail.com>2020-03-13 19:45:39 +0300
committerMyles Borins <mylesborins@google.com>2020-03-24 09:55:08 +0300
commitb5b7bf5ea4938b94af0a8497bdbe5b8b44c61a2c (patch)
tree56a62649745210a58c17ee56f2745c86d3afd535 /src/node_report.h
parent5adaf1092a275c112d92a01182bad64c1e4c9e46 (diff)
src,cli: support compact (one-line) JSON reports
Multi-line JSON is more human readable, but harder for log aggregators to consume, they usually require a log message per line, particularly for JSON. Compact output will be consumable by aggregators such as EFK (Elastic Search-Fluentd-Kibana), LogDNA, DataDog, etc. PR-URL: https://github.com/nodejs/node/pull/32254 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Diffstat (limited to 'src/node_report.h')
-rw-r--r--src/node_report.h46
1 files changed, 34 insertions, 12 deletions
diff --git a/src/node_report.h b/src/node_report.h
index 46b69b9681d..808e302223e 100644
--- a/src/node_report.h
+++ b/src/node_report.h
@@ -65,17 +65,29 @@ extern double prog_start_time;
// JSON compiler definitions.
class JSONWriter {
public:
- explicit JSONWriter(std::ostream& out) : out_(out) {}
+ JSONWriter(std::ostream& out, bool compact)
+ : out_(out), compact_(compact) {}
+ private:
inline void indent() { indent_ += 2; }
inline void deindent() { indent_ -= 2; }
inline void advance() {
+ if (compact_) return;
for (int i = 0; i < indent_; i++) out_ << ' ';
}
+ inline void write_one_space() {
+ if (compact_) return;
+ out_ << ' ';
+ }
+ inline void write_new_line() {
+ if (compact_) return;
+ out_ << '\n';
+ }
+ public:
inline void json_start() {
if (state_ == kAfterValue) out_ << ',';
- out_ << '\n';
+ write_new_line();
advance();
out_ << '{';
indent();
@@ -83,7 +95,7 @@ class JSONWriter {
}
inline void json_end() {
- out_ << '\n';
+ write_new_line();
deindent();
advance();
out_ << '}';
@@ -92,10 +104,12 @@ class JSONWriter {
template <typename T>
inline void json_objectstart(T key) {
if (state_ == kAfterValue) out_ << ',';
- out_ << '\n';
+ write_new_line();
advance();
write_string(key);
- out_ << ": {";
+ out_ << ':';
+ write_one_space();
+ out_ << '{';
indent();
state_ = kObjectStart;
}
@@ -103,23 +117,29 @@ class JSONWriter {
template <typename T>
inline void json_arraystart(T key) {
if (state_ == kAfterValue) out_ << ',';
- out_ << '\n';
+ write_new_line();
advance();
write_string(key);
- out_ << ": [";
+ out_ << ':';
+ write_one_space();
+ out_ << '[';
indent();
state_ = kObjectStart;
}
inline void json_objectend() {
- out_ << '\n';
+ write_new_line();
deindent();
advance();
out_ << '}';
+ if (indent_ == 0) {
+ // Top-level object is complete, so end the line.
+ out_ << '\n';
+ }
state_ = kAfterValue;
}
inline void json_arrayend() {
- out_ << '\n';
+ write_new_line();
deindent();
advance();
out_ << ']';
@@ -128,10 +148,11 @@ class JSONWriter {
template <typename T, typename U>
inline void json_keyvalue(const T& key, const U& value) {
if (state_ == kAfterValue) out_ << ',';
- out_ << '\n';
+ write_new_line();
advance();
write_string(key);
- out_ << ": ";
+ out_ << ':';
+ write_one_space();
write_value(value);
state_ = kAfterValue;
}
@@ -139,7 +160,7 @@ class JSONWriter {
template <typename U>
inline void json_element(const U& value) {
if (state_ == kAfterValue) out_ << ',';
- out_ << '\n';
+ write_new_line();
advance();
write_value(value);
state_ = kAfterValue;
@@ -177,6 +198,7 @@ class JSONWriter {
enum JSONState { kObjectStart, kAfterValue };
std::ostream& out_;
+ bool compact_;
int indent_ = 0;
int state_ = kObjectStart;
};