This is an archive of the discontinued LLVM Phabricator instance.

[lldb][lldb-vscode] Pretty print JSON to log files
ClosedPublic

Authored by DavidSpickett on Aug 3 2023, 2:45 AM.

Details

Summary

This makes anlysing test failures much more easy.

For SendJSON this is simple, just use llvm::format instead.

For GetNextObject/ReadJSON it's a bit more tricky.

  • Print the "Content-Length:" line in ReadJSON, but not the json.
  • Back in GetNextObject, if the JSON doesn't parse, it'll be printed as a normal string along with an error message.
  • If we didn't error before, we have a JSON value so we pretty print it.
  • Finally, if it isn't an object we'll log an error for that, not including the JSON.

Before:

<--
Content-Length: 81

{"command":"disconnect","request_seq":5,"seq":0,"success":true,"type":"response"}

After:

<--
Content-Length: 81

{
  "command": "disconnect",
  "request_seq": 5,
  "seq": 0,
  "success": true,
  "type": "response"
}

There appear to be some responses that include strings that are themselves JSON,
and this won't pretty print those but I think it's still worth doing.

Diff Detail

Event Timeline

DavidSpickett created this revision.Aug 3 2023, 2:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 2:45 AM
DavidSpickett requested review of this revision.Aug 3 2023, 2:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 2:45 AM

The one disadvantage here is that pretty printing adds more characters, so before you could copy paste the single JSON line and (I presume) it would have the same number of characters.

If that's a required use case then instead I can do the printing in the testing wrapper in lldb, by just going line by line and anything beginning with {, attempt to parse as JSON. I just figured that this formatting may be useful outside of the lldb context.

wallace accepted this revision.Aug 3 2023, 7:43 AM

nice improvement for sure

This revision is now accepted and ready to land.Aug 3 2023, 7:43 AM
This revision was automatically updated to reflect the committed changes.