================
@@ -335,6 +336,47 @@ TEST_F(LogChannelEnabledTest, log_options) {
             logAndTakeOutput("Hello World"));
 }
 
+TEST_F(LogChannelEnabledTest, JSONLOutput) {
+  std::string Err;
+
+  // With only the JSON flag set, every line must parse as JSON and contain
+  // exactly the message we logged.
+  EXPECT_TRUE(
+      EnableChannel(getLogHandler(), LLDB_LOG_OPTION_JSON, "chan", {}, Err));
+  llvm::StringRef Msg = logAndTakeOutput("Hello \"World\"\nsecond line");
+  ASSERT_TRUE(Msg.ends_with("\n"));
+  ASSERT_EQ(Msg.count('\n'), 1u) << "expected one JSON object per line";
+  llvm::Expected<llvm::json::Value> Parsed = llvm::json::parse(Msg);
+  ASSERT_TRUE(static_cast<bool>(Parsed)) << llvm::toString(Parsed.takeError());
+  llvm::json::Object *Obj = Parsed->getAsObject();
+  ASSERT_NE(Obj, nullptr);
+  llvm::StringRef Got = Obj->getString("message").value_or("");
+  EXPECT_EQ(Got, "Hello \"World\"\nsecond line");
+  EXPECT_EQ(Obj->size(), 1u);
+
+  // Combining JSON with metadata flags: each metadata flag becomes a JSON
+  // field instead of a prefix on the line.
----------------
DavidSpickett wrote:

No, but actually I misunderstood how this worked so that's not your fault.

I thought that there was a mode where all the metadata stuff was prepended onto 
the message as normal, then all that was wrapped up as the "message" in the 
JSON object. But clearly once you've got some form of JSON object, you might as 
well put the metadata as fields in that.

I misread this:
> each metadata flag becomes a JSON field instead of a prefix on the line.

Because I read it as if there was a mode where the JSON "message" contained 
those prefixes, but there isn't one. The prefix thing happens only when you're 
not using JSON.

I think you at first had just JSON enabled, and that would be fine. JSON only, 
then JSON + all optional extras.

https://github.com/llvm/llvm-project/pull/205758
_______________________________________________
lldb-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to