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
SetupLogging(const std::string & log_file,const StringRef & log_channels,uint32_t log_options)25 LLDBServerUtilities::SetupLogging(const std::string& log_file,
26 const StringRef& log_channels,
27 uint32_t log_options)
28 {
29 lldb::StreamSP log_stream_sp;
30 if (log_file.empty())
31 {
32 log_stream_sp.reset(new StreamFile(stdout, false));
33 }
34 else
35 {
36 uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate |
37 File::eOpenOptionCloseOnExec | File::eOpenOptionAppend;
38 if (!(log_options & LLDB_LOG_OPTION_APPEND))
39 options |= File::eOpenOptionTruncate;
40
41 log_stream_sp.reset(new StreamFile(log_file.c_str(), options));
42 }
43
44 SmallVector<StringRef, 32> channel_array;
45 log_channels.split(channel_array, ":");
46 for (auto channel_with_categories : channel_array)
47 {
48 StreamString error_stream;
49 Args channel_then_categories(channel_with_categories);
50 std::string channel(channel_then_categories.GetArgumentAtIndex(0));
51 channel_then_categories.Shift (); // Shift off the channel
52
53 bool success = Log::EnableLogChannel(log_stream_sp,
54 log_options,
55 channel.c_str(),
56 channel_then_categories.GetConstArgumentVector(),
57 error_stream);
58 if (!success)
59 {
60 fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n",
61 log_file.c_str(),
62 channel_with_categories.str().c_str());
63 return false;
64 }
65 }
66 return true;
67 }
68