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/Log.h"
13 #include "lldb/Core/StreamFile.h"
14 #include "lldb/Core/StreamString.h"
15 #include "lldb/Interpreter/Args.h"
16 
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/ADT/StringRef.h"
19 
20 using namespace lldb;
21 using namespace lldb_private::lldb_server;
22 using namespace llvm;
23 
24 bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
25                                        const StringRef &log_channels,
26                                        uint32_t log_options) {
27   lldb::StreamSP log_stream_sp;
28   if (log_file.empty()) {
29     log_stream_sp.reset(new StreamFile(stdout, false));
30   } else {
31     uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate |
32                        File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
33     if (!(log_options & LLDB_LOG_OPTION_APPEND))
34       options |= File::eOpenOptionTruncate;
35 
36     log_stream_sp.reset(new StreamFile(log_file.c_str(), options));
37   }
38 
39   SmallVector<StringRef, 32> channel_array;
40   log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
41   for (auto channel_with_categories : channel_array) {
42     StreamString error_stream;
43     Args channel_then_categories(channel_with_categories);
44     std::string channel(channel_then_categories.GetArgumentAtIndex(0));
45     channel_then_categories.Shift(); // Shift off the channel
46 
47     bool success = Log::EnableLogChannel(
48         log_stream_sp, log_options, channel.c_str(),
49         channel_then_categories.GetConstArgumentVector(), error_stream);
50     if (!success) {
51       fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n",
52               log_file.c_str(), channel_with_categories.str().c_str());
53       return false;
54     }
55   }
56   return true;
57 }
58