Path: blob/main/contrib/llvm-project/lldb/tools/lldb-server/LLDBServerUtilities.cpp
34879 views
//===-- LLDBServerUtilities.cpp ---------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "LLDBServerUtilities.h"910#include "lldb/Utility/Args.h"11#include "lldb/Utility/Log.h"12#include "lldb/Utility/StreamString.h"1314#include "llvm/ADT/SmallVector.h"15#include "llvm/ADT/StringRef.h"16#include "llvm/Support/FileSystem.h"1718using namespace lldb;19using namespace lldb_private::lldb_server;20using namespace lldb_private;21using namespace llvm;2223class TestLogHandler : public LogHandler {24public:25TestLogHandler(std::shared_ptr<llvm::raw_ostream> stream_sp)26: m_stream_sp(stream_sp) {}2728void Emit(llvm::StringRef message) override {29(*m_stream_sp) << message;30m_stream_sp->flush();31}3233private:34std::shared_ptr<raw_ostream> m_stream_sp;35};3637static std::shared_ptr<TestLogHandler> GetLogStream(StringRef log_file) {38if (!log_file.empty()) {39std::error_code EC;40auto stream_sp = std::make_shared<raw_fd_ostream>(41log_file, EC, sys::fs::OF_TextWithCRLF | sys::fs::OF_Append);42if (!EC)43return std::make_shared<TestLogHandler>(stream_sp);44errs() << llvm::formatv(45"Failed to open log file `{0}`: {1}\nWill log to stderr instead.\n",46log_file, EC.message());47}48// No need to delete the stderr stream.49return std::make_shared<TestLogHandler>(50std::shared_ptr<raw_ostream>(&errs(), [](raw_ostream *) {}));51}5253bool LLDBServerUtilities::SetupLogging(const std::string &log_file,54const StringRef &log_channels,55uint32_t log_options) {5657auto log_stream_sp = GetLogStream(log_file);5859SmallVector<StringRef, 32> channel_array;60log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false);61for (auto channel_with_categories : channel_array) {62std::string error;63llvm::raw_string_ostream error_stream(error);64Args channel_then_categories(channel_with_categories);65std::string channel(channel_then_categories.GetArgumentAtIndex(0));66channel_then_categories.Shift(); // Shift off the channel6768bool success = Log::EnableLogChannel(69log_stream_sp, log_options, channel,70channel_then_categories.GetArgumentArrayRef(), error_stream);71if (!success) {72errs() << formatv("Unable to setup logging for channel \"{0}\": {1}",73channel, error_stream.str());74return false;75}76}77return true;78}798081