1 //===-- 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" 14 #include "lldb/Interpreter/Args.h" 15 #include "lldb/Utility/StreamString.h" 16 17 #include "llvm/ADT/SmallVector.h" 18 #include "llvm/ADT/StringRef.h" 19 #include "llvm/Support/FileSystem.h" 20 21 using namespace lldb; 22 using namespace lldb_private::lldb_server; 23 using namespace llvm; 24 25 static 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 40 bool LLDBServerUtilities::SetupLogging(const std::string &log_file, 41 const StringRef &log_channels, 42 uint32_t log_options) { 43 44 auto log_stream_sp = GetLogStream(log_file); 45 46 SmallVector<StringRef, 32> channel_array; 47 log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false); 48 for (auto channel_with_categories : channel_array) { 49 StreamString error_stream; 50 Args channel_then_categories(channel_with_categories); 51 std::string channel(channel_then_categories.GetArgumentAtIndex(0)); 52 channel_then_categories.Shift(); // Shift off the channel 53 54 bool success = Log::EnableLogChannel( 55 log_stream_sp, log_options, channel, 56 channel_then_categories.GetArgumentArrayRef(), error_stream); 57 if (!success) { 58 fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n", 59 log_file.c_str(), channel_with_categories.str().c_str()); 60 return false; 61 } 62 } 63 return true; 64 } 65