77// ===----------------------------------------------------------------------===//
88
99#include " DAP.h"
10+ #include " DAPLog.h"
1011#include " Handler/ResponseHandler.h"
1112#include " JSONUtils.h"
1213#include " LLDBUtils.h"
1920#include " lldb/API/SBProcess.h"
2021#include " lldb/API/SBStream.h"
2122#include " lldb/Utility/IOObject.h"
23+ #include " lldb/Utility/Log.h"
2224#include " lldb/Utility/Status.h"
2325#include " lldb/lldb-defines.h"
2426#include " lldb/lldb-enumerations.h"
5052#endif
5153
5254using namespace lldb_dap ;
55+ using namespace lldb_private ;
5356
5457namespace {
5558#ifdef _WIN32
@@ -61,13 +64,12 @@ const char DEV_NULL[] = "/dev/null";
6164
6265namespace lldb_dap {
6366
64- DAP::DAP (std::string name, llvm::StringRef path, std::ofstream *log ,
65- lldb::IOObjectSP input, lldb::IOObjectSP output, ReplMode repl_mode,
67+ DAP::DAP (std::string name, llvm::StringRef path, lldb::IOObjectSP input ,
68+ lldb::IOObjectSP output, ReplMode repl_mode,
6669 std::vector<std::string> pre_init_commands)
67- : name(std::move(name)), debug_adapter_path(path), log(log),
68- input (std::move(input)), output(std::move(output)),
69- broadcaster(" lldb-dap" ), exception_breakpoints(),
70- pre_init_commands(std::move(pre_init_commands)),
70+ : name(std::move(name)), debug_adapter_path(path), input(std::move(input)),
71+ output (std::move(output)), broadcaster(" lldb-dap" ),
72+ exception_breakpoints(), pre_init_commands(std::move(pre_init_commands)),
7173 focus_tid(LLDB_INVALID_THREAD_ID), stop_at_entry(false ), is_attach(false ),
7274 enable_auto_variable_summaries(false ),
7375 enable_synthetic_child_debugging(false ),
@@ -245,6 +247,8 @@ void DAP::SendJSON(const std::string &json_str) {
245247 output.write_full (llvm::utostr (json_str.size ()));
246248 output.write_full (" \r\n\r\n " );
247249 output.write_full (json_str);
250+
251+ LLDB_LOG (GetLog (DAPLog::Transport), " {0} <-- {1}" , name, json_str);
248252}
249253
250254// Serialize the JSON value into a string and send the JSON packet to
@@ -256,15 +260,6 @@ void DAP::SendJSON(const llvm::json::Value &json) {
256260 static std::mutex mutex;
257261 std::lock_guard<std::mutex> locker (mutex);
258262 SendJSON (json_str);
259-
260- if (log) {
261- auto now = std::chrono::duration<double >(
262- std::chrono::system_clock::now ().time_since_epoch ());
263- *log << llvm::formatv (" {0:f9} {1} <-- " , now.count (), name).str ()
264- << std::endl
265- << " Content-Length: " << json_str.size () << " \r\n\r\n "
266- << llvm::formatv (" {0:2}" , json).str () << std::endl;
267- }
268263}
269264
270265// Read a JSON packet from the "in" stream.
@@ -273,28 +268,22 @@ std::string DAP::ReadJSON() {
273268 std::string json_str;
274269 int length;
275270
276- if (!input.read_expected (log, " Content-Length: " ))
271+ if (!input.read_expected (" Content-Length: " ))
277272 return json_str;
278273
279- if (!input.read_line (log, length_str))
274+ if (!input.read_line (length_str))
280275 return json_str;
281276
282277 if (!llvm::to_integer (length_str, length))
283278 return json_str;
284279
285- if (!input.read_expected (log, " \r\n " ))
280+ if (!input.read_expected (" \r\n " ))
286281 return json_str;
287282
288- if (!input.read_full (log, length, json_str))
283+ if (!input.read_full (length, json_str))
289284 return json_str;
290285
291- if (log) {
292- auto now = std::chrono::duration<double >(
293- std::chrono::system_clock::now ().time_since_epoch ());
294- *log << llvm::formatv (" {0:f9} {1} --> " , now.count (), name).str ()
295- << std::endl
296- << " Content-Length: " << length << " \r\n\r\n " ;
297- }
286+ LLDB_LOG (GetLog (DAPLog::Transport), " {0} --> {1}" , name, json_str);
298287 return json_str;
299288}
300289
@@ -729,24 +718,14 @@ PacketStatus DAP::GetNextObject(llvm::json::Object &object) {
729718 llvm::Expected<llvm::json::Value> json_value = llvm::json::parse (json_sref);
730719 if (!json_value) {
731720 auto error = json_value.takeError ();
732- if (log) {
733- std::string error_str;
734- llvm::raw_string_ostream strm (error_str);
735- strm << error;
736- *log << " error: failed to parse JSON: " << error_str << std::endl
737- << json << std::endl;
738- }
721+ LLDB_LOG_ERROR (GetLog (DAPLog::Protocol), std::move (error),
722+ " failed to parse JSON: {0}" );
739723 return PacketStatus::JSONMalformed;
740724 }
741725
742- if (log) {
743- *log << llvm::formatv (" {0:2}" , *json_value).str () << std::endl;
744- }
745-
746726 llvm::json::Object *object_ptr = json_value->getAsObject ();
747727 if (!object_ptr) {
748- if (log)
749- *log << " error: json packet isn't a object" << std::endl;
728+ LLDB_LOG (GetLog (DAPLog::Protocol), " error: json packet isn't a object" );
750729 return PacketStatus::JSONNotObject;
751730 }
752731 object = *object_ptr;
@@ -764,9 +743,8 @@ bool DAP::HandleObject(const llvm::json::Object &object) {
764743 return true ; // Success
765744 }
766745
767- if (log)
768- *log << " error: unhandled command \" " << command.data () << " \" "
769- << std::endl;
746+ LLDB_LOG (GetLog (DAPLog::Protocol), " error: unhandled command '{0}'" ,
747+ command);
770748 return false ; // Fail
771749 }
772750
0 commit comments