Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 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" |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 14 | #include "lldb/Interpreter/Args.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame^] | 15 | #include "lldb/Utility/StreamString.h" |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 16 | |
| 17 | #include "llvm/ADT/SmallVector.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | |
| 20 | using namespace lldb; |
| 21 | using namespace lldb_private::lldb_server; |
| 22 | using namespace llvm; |
| 23 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 24 | bool LLDBServerUtilities::SetupLogging(const std::string &log_file, |
| 25 | const StringRef &log_channels, |
| 26 | uint32_t log_options) { |
| 27 | lldb::StreamSP log_stream_sp; |
| 28 | if (log_file.empty()) { |
| 29 | log_stream_sp.reset(new StreamFile(stdout, false)); |
| 30 | } else { |
| 31 | uint32_t options = File::eOpenOptionWrite | File::eOpenOptionCanCreate | |
| 32 | File::eOpenOptionCloseOnExec | File::eOpenOptionAppend; |
| 33 | if (!(log_options & LLDB_LOG_OPTION_APPEND)) |
| 34 | options |= File::eOpenOptionTruncate; |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 35 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 36 | log_stream_sp.reset(new StreamFile(log_file.c_str(), options)); |
| 37 | } |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 38 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 39 | SmallVector<StringRef, 32> channel_array; |
| 40 | log_channels.split(channel_array, ":", /*MaxSplit*/ -1, /*KeepEmpty*/ false); |
| 41 | for (auto channel_with_categories : channel_array) { |
| 42 | StreamString error_stream; |
| 43 | Args channel_then_categories(channel_with_categories); |
| 44 | std::string channel(channel_then_categories.GetArgumentAtIndex(0)); |
| 45 | channel_then_categories.Shift(); // Shift off the channel |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 46 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 47 | bool success = Log::EnableLogChannel( |
| 48 | log_stream_sp, log_options, channel.c_str(), |
| 49 | channel_then_categories.GetConstArgumentVector(), error_stream); |
| 50 | if (!success) { |
| 51 | fprintf(stderr, "Unable to open log file '%s' for channel \"%s\"\n", |
| 52 | log_file.c_str(), channel_with_categories.str().c_str()); |
| 53 | return false; |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 54 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | } |
| 56 | return true; |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 57 | } |