blob: d2f0b57300d70e928c19536cb6b9fd952336217f [file] [log] [blame]
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +00001//===-- 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"
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000014#include "lldb/Interpreter/Args.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000015#include "lldb/Utility/StreamString.h"
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000016
17#include "llvm/ADT/SmallVector.h"
18#include "llvm/ADT/StringRef.h"
19
20using namespace lldb;
21using namespace lldb_private::lldb_server;
22using namespace llvm;
23
Kate Stoneb9c1b512016-09-06 20:57:50 +000024bool 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;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 log_stream_sp.reset(new StreamFile(log_file.c_str(), options));
37 }
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 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
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 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;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000054 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 }
56 return true;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000057}