Coverage Report

Created: 2023-09-21 18:56

/Users/buildslave/jenkins/workspace/coverage/llvm-project/lldb/tools/lldb-server/LLDBServerUtilities.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "LLDBServerUtilities.h"
10
11
#include "lldb/Utility/Args.h"
12
#include "lldb/Utility/Log.h"
13
#include "lldb/Utility/StreamString.h"
14
15
#include "llvm/ADT/SmallVector.h"
16
#include "llvm/ADT/StringRef.h"
17
#include "llvm/Support/FileSystem.h"
18
19
using namespace lldb;
20
using namespace lldb_private::lldb_server;
21
using namespace lldb_private;
22
using namespace llvm;
23
24
class TestLogHandler : public LogHandler {
25
public:
26
  TestLogHandler(std::shared_ptr<llvm::raw_ostream> stream_sp)
27
2
      : m_stream_sp(stream_sp) {}
28
29
0
  void Emit(llvm::StringRef message) override {
30
0
    (*m_stream_sp) << message;
31
0
    m_stream_sp->flush();
32
0
  }
33
34
private:
35
  std::shared_ptr<raw_ostream> m_stream_sp;
36
};
37
38
2
static std::shared_ptr<TestLogHandler> GetLogStream(StringRef log_file) {
39
2
  if (!log_file.empty()) {
40
0
    std::error_code EC;
41
0
    auto stream_sp = std::make_shared<raw_fd_ostream>(
42
0
        log_file, EC, sys::fs::OF_TextWithCRLF | sys::fs::OF_Append);
43
0
    if (!EC)
44
0
      return std::make_shared<TestLogHandler>(stream_sp);
45
0
    errs() << llvm::formatv(
46
0
        "Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",
47
0
        log_file, EC.message());
48
0
  }
49
  // No need to delete the stderr stream.
50
2
  return std::make_shared<TestLogHandler>(
51
2
      std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {}));
52
2
}
53
54
bool LLDBServerUtilities::SetupLogging(const std::string &log_file,
55
                                       const StringRef &log_channels,
56
2
                                       uint32_t log_options) {
57
58
2
  auto log_stream_sp = GetLogStream(log_file);
59
60
2
  SmallVector<StringRef, 32> channel_array;
61
2
  log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);
62
2
  for (auto channel_with_categories : channel_array) {
63
0
    std::string error;
64
0
    llvm::raw_string_ostream error_stream(error);
65
0
    Args channel_then_categories(channel_with_categories);
66
0
    std::string channel(channel_then_categories.GetArgumentAtIndex(0));
67
0
    channel_then_categories.Shift(); // Shift off the channel
68
69
0
    bool success = Log::EnableLogChannel(
70
0
        log_stream_sp, log_options, channel,
71
0
        channel_then_categories.GetArgumentArrayRef(), error_stream);
72
0
    if (!success) {
73
0
      errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",
74
0
                        channel, error_stream.str());
75
0
      return false;
76
0
    }
77
0
  }
78
2
  return true;
79
2
}