xref: /freebsd/contrib/llvm-project/lldb/tools/lldb-server/LLDBServerUtilities.cpp (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1*0b57cec5SDimitry Andric //===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "LLDBServerUtilities.h"
10*0b57cec5SDimitry Andric 
11*0b57cec5SDimitry Andric #include "lldb/Utility/Args.h"
12*0b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
13*0b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
14*0b57cec5SDimitry Andric 
15*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
16*0b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
17*0b57cec5SDimitry Andric #include "llvm/Support/FileSystem.h"
18*0b57cec5SDimitry Andric 
19*0b57cec5SDimitry Andric using namespace lldb;
20*0b57cec5SDimitry Andric using namespace lldb_private::lldb_server;
21*0b57cec5SDimitry Andric using namespace llvm;
22*0b57cec5SDimitry Andric 
23*0b57cec5SDimitry Andric static std::shared_ptr<raw_ostream> GetLogStream(StringRef log_file) {
24*0b57cec5SDimitry Andric   if (!log_file.empty()) {
25*0b57cec5SDimitry Andric     std::error_code EC;
26*0b57cec5SDimitry Andric     std::shared_ptr<raw_ostream> stream_sp = std::make_shared<raw_fd_ostream>(
27*0b57cec5SDimitry Andric         log_file, EC, sys::fs::F_Text | sys::fs::F_Append);
28*0b57cec5SDimitry Andric     if (!EC)
29*0b57cec5SDimitry Andric       return stream_sp;
30*0b57cec5SDimitry Andric     errs() << llvm::formatv(
31*0b57cec5SDimitry Andric         "Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",
32*0b57cec5SDimitry Andric         log_file, EC.message());
33*0b57cec5SDimitry Andric   }
34*0b57cec5SDimitry Andric   // No need to delete the stderr stream.
35*0b57cec5SDimitry Andric   return std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {});
36*0b57cec5SDimitry Andric }
37*0b57cec5SDimitry Andric 
38*0b57cec5SDimitry Andric bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
39*0b57cec5SDimitry Andric                                        const StringRef &log_channels,
40*0b57cec5SDimitry Andric                                        uint32_t log_options) {
41*0b57cec5SDimitry Andric 
42*0b57cec5SDimitry Andric   auto log_stream_sp = GetLogStream(log_file);
43*0b57cec5SDimitry Andric 
44*0b57cec5SDimitry Andric   SmallVector<StringRef, 32> channel_array;
45*0b57cec5SDimitry Andric   log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
46*0b57cec5SDimitry Andric   for (auto channel_with_categories : channel_array) {
47*0b57cec5SDimitry Andric     std::string error;
48*0b57cec5SDimitry Andric     llvm::raw_string_ostream error_stream(error);
49*0b57cec5SDimitry Andric     Args channel_then_categories(channel_with_categories);
50*0b57cec5SDimitry Andric     std::string channel(channel_then_categories.GetArgumentAtIndex(0));
51*0b57cec5SDimitry Andric     channel_then_categories.Shift(); // Shift off the channel
52*0b57cec5SDimitry Andric 
53*0b57cec5SDimitry Andric     bool success = Log::EnableLogChannel(
54*0b57cec5SDimitry Andric         log_stream_sp, log_options, channel,
55*0b57cec5SDimitry Andric         channel_then_categories.GetArgumentArrayRef(), error_stream);
56*0b57cec5SDimitry Andric     if (!success) {
57*0b57cec5SDimitry Andric       errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
58*0b57cec5SDimitry Andric                         channel, error_stream.str());
59*0b57cec5SDimitry Andric       return false;
60*0b57cec5SDimitry Andric     }
61*0b57cec5SDimitry Andric   }
62*0b57cec5SDimitry Andric   return true;
63*0b57cec5SDimitry Andric }
64