]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-server/LLDBServerUtilities.cpp
Merge clang 7.0.1 and several follow-up changes
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / tools / lldb-server / LLDBServerUtilities.cpp
1 //===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "LLDBServerUtilities.h"
11
12 #include "lldb/Core/StreamFile.h"
13 #include "lldb/Utility/Args.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/StreamString.h"
16
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/FileSystem.h"
20
21 using namespace lldb;
22 using namespace lldb_private::lldb_server;
23 using namespace llvm;
24
25 static std::shared_ptr<raw_ostream> GetLogStream(StringRef log_file) {
26   if (!log_file.empty()) {
27     std::error_code EC;
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);
30     if (!EC)
31       return stream_sp;
32     errs() << llvm::formatv(
33         "Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",
34         log_file, EC.message());
35   }
36   // No need to delete the stderr stream.
37   return std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {});
38 }
39
40 bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
41                                        const StringRef &log_channels,
42                                        uint32_t log_options) {
43
44   auto log_stream_sp = GetLogStream(log_file);
45
46   SmallVector<StringRef, 32> channel_array;
47   log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
48   for (auto channel_with_categories : channel_array) {
49     std::string error;
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
54
55     bool success = Log::EnableLogChannel(
56         log_stream_sp, log_options, channel,
57         channel_then_categories.GetArgumentArrayRef(), error_stream);
58     if (!success) {
59       errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
60                         channel, error_stream.str());
61       return false;
62     }
63   }
64   return true;
65 }