1061da546Spatrick //===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "LLDBServerUtilities.h"
10061da546Spatrick
11061da546Spatrick #include "lldb/Utility/Args.h"
12061da546Spatrick #include "lldb/Utility/Log.h"
13061da546Spatrick #include "lldb/Utility/StreamString.h"
14061da546Spatrick
15061da546Spatrick #include "llvm/ADT/SmallVector.h"
16061da546Spatrick #include "llvm/ADT/StringRef.h"
17061da546Spatrick #include "llvm/Support/FileSystem.h"
18061da546Spatrick
19061da546Spatrick using namespace lldb;
20061da546Spatrick using namespace lldb_private::lldb_server;
21*f6aab3d8Srobert using namespace lldb_private;
22061da546Spatrick using namespace llvm;
23061da546Spatrick
24*f6aab3d8Srobert class TestLogHandler : public LogHandler {
25*f6aab3d8Srobert public:
TestLogHandler(std::shared_ptr<llvm::raw_ostream> stream_sp)26*f6aab3d8Srobert TestLogHandler(std::shared_ptr<llvm::raw_ostream> stream_sp)
27*f6aab3d8Srobert : m_stream_sp(stream_sp) {}
28*f6aab3d8Srobert
Emit(llvm::StringRef message)29*f6aab3d8Srobert void Emit(llvm::StringRef message) override {
30*f6aab3d8Srobert (*m_stream_sp) << message;
31*f6aab3d8Srobert m_stream_sp->flush();
32*f6aab3d8Srobert }
33*f6aab3d8Srobert
34*f6aab3d8Srobert private:
35*f6aab3d8Srobert std::shared_ptr<raw_ostream> m_stream_sp;
36*f6aab3d8Srobert };
37*f6aab3d8Srobert
GetLogStream(StringRef log_file)38*f6aab3d8Srobert static std::shared_ptr<TestLogHandler> GetLogStream(StringRef log_file) {
39061da546Spatrick if (!log_file.empty()) {
40061da546Spatrick std::error_code EC;
41*f6aab3d8Srobert auto stream_sp = std::make_shared<raw_fd_ostream>(
42be691f3bSpatrick log_file, EC, sys::fs::OF_TextWithCRLF | sys::fs::OF_Append);
43061da546Spatrick if (!EC)
44*f6aab3d8Srobert return std::make_shared<TestLogHandler>(stream_sp);
45061da546Spatrick errs() << llvm::formatv(
46061da546Spatrick "Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",
47061da546Spatrick log_file, EC.message());
48061da546Spatrick }
49061da546Spatrick // No need to delete the stderr stream.
50*f6aab3d8Srobert return std::make_shared<TestLogHandler>(
51*f6aab3d8Srobert std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {}));
52061da546Spatrick }
53061da546Spatrick
SetupLogging(const std::string & log_file,const StringRef & log_channels,uint32_t log_options)54061da546Spatrick bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
55061da546Spatrick const StringRef &log_channels,
56061da546Spatrick uint32_t log_options) {
57061da546Spatrick
58061da546Spatrick auto log_stream_sp = GetLogStream(log_file);
59061da546Spatrick
60061da546Spatrick SmallVector<StringRef, 32> channel_array;
61061da546Spatrick log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
62061da546Spatrick for (auto channel_with_categories : channel_array) {
63061da546Spatrick std::string error;
64061da546Spatrick llvm::raw_string_ostream error_stream(error);
65061da546Spatrick Args channel_then_categories(channel_with_categories);
66061da546Spatrick std::string channel(channel_then_categories.GetArgumentAtIndex(0));
67061da546Spatrick channel_then_categories.Shift(); // Shift off the channel
68061da546Spatrick
69061da546Spatrick bool success = Log::EnableLogChannel(
70061da546Spatrick log_stream_sp, log_options, channel,
71061da546Spatrick channel_then_categories.GetArgumentArrayRef(), error_stream);
72061da546Spatrick if (!success) {
73061da546Spatrick errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
74061da546Spatrick channel, error_stream.str());
75061da546Spatrick return false;
76061da546Spatrick }
77061da546Spatrick }
78061da546Spatrick return true;
79061da546Spatrick }
80