blob: e784a3a82684b1832b08153f167928fb23a5be75 [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
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000012#include "lldb/Core/StreamFile.h"
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000013#include "lldb/Interpreter/Args.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000014#include "lldb/Utility/Log.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"
Pavel Labath5fae71c2017-02-10 11:49:21 +000019#include "llvm/Support/FileSystem.h"
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000020
21using namespace lldb;
22using namespace lldb_private::lldb_server;
23using namespace llvm;
24
Pavel Labath5fae71c2017-02-10 11:49:21 +000025static 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
Kate Stoneb9c1b512016-09-06 20:57:50 +000040bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
41 const StringRef &log_channels,
42 uint32_t log_options) {
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000043
Pavel Labath5fae71c2017-02-10 11:49:21 +000044 auto log_stream_sp = GetLogStream(log_file);
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000045
Kate Stoneb9c1b512016-09-06 20:57:50 +000046 SmallVector<StringRef, 32> channel_array;
47 log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
48 for (auto channel_with_categories : channel_array) {
Pavel Labath775588c2017-03-15 09:06:58 +000049 std::string error;
50 llvm::raw_string_ostream error_stream(error);
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 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
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 bool success = Log::EnableLogChannel(
Pavel Labath5e336902017-03-01 10:08:40 +000056 log_stream_sp, log_options, channel,
57 channel_then_categories.GetArgumentArrayRef(), error_stream);
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 if (!success) {
Pavel Labath775588c2017-03-15 09:06:58 +000059 errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
60 channel, error_stream.str());
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 return false;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000062 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 }
64 return true;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000065}