1 //===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "LLDBServerUtilities.h"
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Utility/Args.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/StreamString.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/FileSystem.h"
22 using namespace lldb_private::lldb_server;
25 static std::shared_ptr<raw_ostream> GetLogStream(StringRef log_file) {
26 if (!log_file.empty()) {
28 std::shared_ptr<raw_ostream> stream_sp = std::make_shared<raw_fd_ostream>(
29 log_file, EC, sys::fs::F_Text | sys::fs::F_Append);
32 errs() << llvm::formatv(
33 "Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",
34 log_file, EC.message());
36 // No need to delete the stderr stream.
37 return std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {});
40 bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
41 const StringRef &log_channels,
42 uint32_t log_options) {
44 auto log_stream_sp = GetLogStream(log_file);
46 SmallVector<StringRef, 32> channel_array;
47 log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
48 for (auto channel_with_categories : channel_array) {
50 llvm::raw_string_ostream error_stream(error);
51 Args channel_then_categories(channel_with_categories);
52 std::string channel(channel_then_categories.GetArgumentAtIndex(0));
53 channel_then_categories.Shift(); // Shift off the channel
55 bool success = Log::EnableLogChannel(
56 log_stream_sp, log_options, channel,
57 channel_then_categories.GetArgumentArrayRef(), error_stream);
59 errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
60 channel, error_stream.str());