7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " DAP.h"
10
+ #include " DAPLog.h"
10
11
#include " Handler/ResponseHandler.h"
11
12
#include " JSONUtils.h"
12
13
#include " LLDBUtils.h"
25
26
#include " llvm/ADT/ArrayRef.h"
26
27
#include " llvm/ADT/ScopeExit.h"
27
28
#include " llvm/ADT/StringExtras.h"
29
+ #include " llvm/ADT/StringRef.h"
28
30
#include " llvm/ADT/Twine.h"
29
31
#include " llvm/Support/Error.h"
30
32
#include " llvm/Support/ErrorHandling.h"
@@ -61,10 +63,10 @@ const char DEV_NULL[] = "/dev/null";
61
63
62
64
namespace lldb_dap {
63
65
64
- DAP::DAP (std::string name , llvm::StringRef path, std::ofstream *log,
66
+ DAP::DAP (llvm::StringRef client_name , llvm::StringRef path, std::ofstream *log,
65
67
lldb::IOObjectSP input, lldb::IOObjectSP output, ReplMode repl_mode,
66
68
std::vector<std::string> pre_init_commands)
67
- : name(std::move(name) ), debug_adapter_path(path), log(log),
69
+ : client_name(client_name ), debug_adapter_path(path), log(log),
68
70
input (std::move(input)), output(std::move(output)),
69
71
broadcaster(" lldb-dap" ), exception_breakpoints(),
70
72
pre_init_commands(std::move(pre_init_commands)),
@@ -239,14 +241,7 @@ void DAP::SendJSON(const llvm::json::Value &json) {
239
241
std::lock_guard<std::mutex> locker (mutex);
240
242
SendJSON (json_str);
241
243
242
- if (log) {
243
- auto now = std::chrono::duration<double >(
244
- std::chrono::system_clock::now ().time_since_epoch ());
245
- *log << llvm::formatv (" {0:f9} {1} <-- " , now.count (), name).str ()
246
- << std::endl
247
- << " Content-Length: " << json_str.size () << " \r\n\r\n "
248
- << llvm::formatv (" {0:2}" , json).str () << std::endl;
249
- }
244
+ DAP_LOG (log, " ({0}) <-- {1}" , client_name, json_str);
250
245
}
251
246
252
247
// Read a JSON packet from the "in" stream.
@@ -270,13 +265,7 @@ std::string DAP::ReadJSON() {
270
265
if (!input.read_full (log, length, json_str))
271
266
return json_str;
272
267
273
- if (log) {
274
- auto now = std::chrono::duration<double >(
275
- std::chrono::system_clock::now ().time_since_epoch ());
276
- *log << llvm::formatv (" {0:f9} {1} --> " , now.count (), name).str ()
277
- << std::endl
278
- << " Content-Length: " << length << " \r\n\r\n " ;
279
- }
268
+ DAP_LOG (log, " ({0}) --> {1}" , client_name, json_str);
280
269
return json_str;
281
270
}
282
271
@@ -711,22 +700,15 @@ PacketStatus DAP::GetNextObject(llvm::json::Object &object) {
711
700
712
701
llvm::StringRef json_sref (json);
713
702
llvm::Expected<llvm::json::Value> json_value = llvm::json::parse (json_sref);
714
- if (auto error = json_value.takeError ()) {
715
- std::string error_str = llvm::toString (std::move (error));
716
- if (log)
717
- *log << " error: failed to parse JSON: " << error_str << std::endl
718
- << json << std::endl;
703
+ if (!json_value) {
704
+ DAP_LOG_ERROR (log, json_value.takeError (),
705
+ " ({1}) failed to parse JSON: {0}" , client_name);
719
706
return PacketStatus::JSONMalformed;
720
707
}
721
708
722
- if (log) {
723
- *log << llvm::formatv (" {0:2}" , *json_value).str () << std::endl;
724
- }
725
-
726
709
llvm::json::Object *object_ptr = json_value->getAsObject ();
727
710
if (!object_ptr) {
728
- if (log)
729
- *log << " error: json packet isn't a object" << std::endl;
711
+ DAP_LOG (log, " ({0}) error: json packet isn't a object" , client_name);
730
712
return PacketStatus::JSONNotObject;
731
713
}
732
714
object = *object_ptr;
@@ -744,9 +726,7 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
744
726
return true ; // Success
745
727
}
746
728
747
- if (log)
748
- *log << " error: unhandled command \" " << command.data () << " \" "
749
- << std::endl;
729
+ DAP_LOG (log, " ({0}) error: unhandled command '{1}'" , client_name, command);
750
730
return false ; // Fail
751
731
}
752
732
0 commit comments