xref: /freebsd-src/contrib/llvm-project/lldb/tools/lldb-server/LLDBServerUtilities.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
10b57cec5SDimitry Andric //===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "LLDBServerUtilities.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
120b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
130b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
160b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
170b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric using namespace lldb;
200b57cec5SDimitry Andric using namespace lldb_private::lldb_server;
21*81ad6265SDimitry Andric using namespace lldb_private;
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric 
24*81ad6265SDimitry Andric class TestLogHandler : public LogHandler {
25*81ad6265SDimitry Andric public:
TestLogHandler(std::shared_ptr<llvm::raw_ostream> stream_sp)26*81ad6265SDimitry Andric   TestLogHandler(std::shared_ptr<llvm::raw_ostream> stream_sp)
27*81ad6265SDimitry Andric       : m_stream_sp(stream_sp) {}
28*81ad6265SDimitry Andric 
Emit(llvm::StringRef message)29*81ad6265SDimitry Andric   void Emit(llvm::StringRef message) override {
30*81ad6265SDimitry Andric     (*m_stream_sp) << message;
31*81ad6265SDimitry Andric     m_stream_sp->flush();
32*81ad6265SDimitry Andric   }
33*81ad6265SDimitry Andric 
34*81ad6265SDimitry Andric private:
35*81ad6265SDimitry Andric   std::shared_ptr<raw_ostream> m_stream_sp;
36*81ad6265SDimitry Andric };
37*81ad6265SDimitry Andric 
GetLogStream(StringRef log_file)38*81ad6265SDimitry Andric static std::shared_ptr<TestLogHandler> GetLogStream(StringRef log_file) {
390b57cec5SDimitry Andric   if (!log_file.empty()) {
400b57cec5SDimitry Andric     std::error_code EC;
41*81ad6265SDimitry Andric     auto stream_sp = std::make_shared<raw_fd_ostream>(
42fe6060f1SDimitry Andric         log_file, EC, sys::fs::OF_TextWithCRLF | sys::fs::OF_Append);
430b57cec5SDimitry Andric     if (!EC)
44*81ad6265SDimitry Andric       return std::make_shared<TestLogHandler>(stream_sp);
450b57cec5SDimitry Andric     errs() << llvm::formatv(
460b57cec5SDimitry Andric         "Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",
470b57cec5SDimitry Andric         log_file, EC.message());
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric   // No need to delete the stderr stream.
50*81ad6265SDimitry Andric   return std::make_shared<TestLogHandler>(
51*81ad6265SDimitry Andric       std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {}));
520b57cec5SDimitry Andric }
530b57cec5SDimitry Andric 
SetupLogging(const std::string & log_file,const StringRef & log_channels,uint32_t log_options)540b57cec5SDimitry Andric bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
550b57cec5SDimitry Andric                                        const StringRef &log_channels,
560b57cec5SDimitry Andric                                        uint32_t log_options) {
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   auto log_stream_sp = GetLogStream(log_file);
590b57cec5SDimitry Andric 
600b57cec5SDimitry Andric   SmallVector<StringRef, 32> channel_array;
610b57cec5SDimitry Andric   log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
620b57cec5SDimitry Andric   for (auto channel_with_categories : channel_array) {
630b57cec5SDimitry Andric     std::string error;
640b57cec5SDimitry Andric     llvm::raw_string_ostream error_stream(error);
650b57cec5SDimitry Andric     Args channel_then_categories(channel_with_categories);
660b57cec5SDimitry Andric     std::string channel(channel_then_categories.GetArgumentAtIndex(0));
670b57cec5SDimitry Andric     channel_then_categories.Shift(); // Shift off the channel
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric     bool success = Log::EnableLogChannel(
700b57cec5SDimitry Andric         log_stream_sp, log_options, channel,
710b57cec5SDimitry Andric         channel_then_categories.GetArgumentArrayRef(), error_stream);
720b57cec5SDimitry Andric     if (!success) {
730b57cec5SDimitry Andric       errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
740b57cec5SDimitry Andric                         channel, error_stream.str());
750b57cec5SDimitry Andric       return false;
760b57cec5SDimitry Andric     }
770b57cec5SDimitry Andric   }
780b57cec5SDimitry Andric   return true;
790b57cec5SDimitry Andric }
80