Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Log.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 | |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 10 | // Project includes |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 11 | #include "lldb/Utility/Log.h" |
| 12 | |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 13 | #include "lldb/Utility/NameMatches.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 14 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 15 | #include "lldb/Utility/VASPrintf.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 16 | |
| 17 | // Other libraries and framework includes |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/STLExtras.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
| 20 | #include "llvm/Support/Chrono.h" |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Signals.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Threading.h" |
Pavel Labath | 774103c | 2016-11-02 12:18:42 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
| 26 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | // C Includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | // C++ Includes |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 29 | #include <cstdarg> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 30 | #include <cstdio> |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 31 | #include <cstdlib> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 32 | #include <map> |
Saleem Abdulrasool | 16ff860 | 2016-05-18 01:59:10 +0000 | [diff] [blame] | 33 | #include <mutex> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | #include <string> |
| 35 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
| 38 | |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 39 | namespace { |
| 40 | struct ChannelAndLog { |
| 41 | Log log; |
| 42 | Log::Channel &channel; |
| 43 | |
| 44 | ChannelAndLog(Log::Channel &channel) : channel(channel) {} |
| 45 | }; |
| 46 | typedef llvm::StringMap<ChannelAndLog> ChannelMap; |
| 47 | } |
| 48 | |
| 49 | static llvm::ManagedStatic<ChannelMap> g_channel_map; |
| 50 | |
| 51 | static void ListCategories(Stream &stream, const ChannelMap::value_type &entry) { |
| 52 | stream.Format("Logging categories for '{0}':\n", entry.first()); |
| 53 | stream.Format(" all - all available logging categories\n"); |
| 54 | stream.Format(" default - default set of logging categories\n"); |
| 55 | for (const auto &category : entry.second.channel.categories) |
| 56 | stream.Format(" {0} - {1}\n", category.name, category.description); |
| 57 | } |
| 58 | |
| 59 | static uint32_t GetFlags(Stream &stream, const ChannelMap::value_type &entry, |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 60 | llvm::ArrayRef<const char *> categories) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 61 | bool list_categories = false; |
| 62 | uint32_t flags = 0; |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 63 | for (const char *category : categories) { |
| 64 | if (llvm::StringRef("all").equals_lower(category)) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 65 | flags |= UINT32_MAX; |
| 66 | continue; |
| 67 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 68 | if (llvm::StringRef("default").equals_lower(category)) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 69 | flags |= entry.second.channel.default_flags; |
| 70 | continue; |
| 71 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 72 | auto cat = llvm::find_if( |
| 73 | entry.second.channel.categories, |
| 74 | [&](const Log::Category &c) { return c.name.equals_lower(category); }); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 75 | if (cat != entry.second.channel.categories.end()) { |
| 76 | flags |= cat->flag; |
| 77 | continue; |
| 78 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 79 | stream.Format("error: unrecognized log category '{0}'\n", category); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 80 | list_categories = true; |
| 81 | } |
| 82 | if (list_categories) |
| 83 | ListCategories(stream, entry); |
| 84 | return flags; |
| 85 | } |
| 86 | |
| 87 | void Log::Channel::Enable(Log &log, |
| 88 | const std::shared_ptr<llvm::raw_ostream> &stream_sp, |
Pavel Labath | 88d081b | 2017-02-27 11:05:39 +0000 | [diff] [blame] | 89 | uint32_t options, uint32_t flags) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 90 | log.GetMask().Set(flags); |
| 91 | if (log.GetMask().Get()) { |
Pavel Labath | a96eac6 | 2017-03-06 19:10:19 +0000 | [diff] [blame] | 92 | log.GetOptions().Reset(options); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 93 | log.SetStream(stream_sp); |
| 94 | log_ptr.store(&log, std::memory_order_release); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void Log::Channel::Disable(uint32_t flags) { |
| 99 | Log *log = log_ptr.load(std::memory_order_acquire); |
| 100 | if (!log) |
| 101 | return; |
| 102 | log->GetMask().Clear(flags); |
| 103 | if (!log->GetMask().Get()) { |
| 104 | log->SetStream(nullptr); |
| 105 | log_ptr.store(nullptr, std::memory_order_release); |
| 106 | } |
| 107 | } |
| 108 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 109 | Log::Log() : m_stream_sp(), m_options(0), m_mask_bits(0) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 111 | Log::Log(const std::shared_ptr<llvm::raw_ostream> &stream_sp) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 112 | : m_stream_sp(stream_sp), m_options(0), m_mask_bits(0) {} |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | |
Eugene Zelenko | a74f37a | 2016-03-10 23:57:12 +0000 | [diff] [blame] | 114 | Log::~Log() = default; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 116 | Flags &Log::GetOptions() { return m_options; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 117 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 118 | const Flags &Log::GetOptions() const { return m_options; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 120 | Flags &Log::GetMask() { return m_mask_bits; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 121 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 122 | const Flags &Log::GetMask() const { return m_mask_bits; } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 124 | void Log::PutCString(const char *cstr) { Printf("%s", cstr); } |
Zachary Turner | c156427 | 2016-11-16 21:15:24 +0000 | [diff] [blame] | 125 | void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); } |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 126 | |
| 127 | //---------------------------------------------------------------------- |
| 128 | // Simple variable argument logging with flags. |
| 129 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 130 | void Log::Printf(const char *format, ...) { |
| 131 | va_list args; |
| 132 | va_start(args, format); |
| 133 | VAPrintf(format, args); |
| 134 | va_end(args); |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | |
| 137 | //---------------------------------------------------------------------- |
| 138 | // All logging eventually boils down to this function call. If we have |
| 139 | // a callback registered, then we call the logging callback. If we have |
| 140 | // a valid file handle, we also log to the file. |
| 141 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | void Log::VAPrintf(const char *format, va_list args) { |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 143 | llvm::SmallString<64> FinalMessage; |
| 144 | llvm::raw_svector_ostream Stream(FinalMessage); |
| 145 | WriteHeader(Stream, "", ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 147 | llvm::SmallString<64> Content; |
| 148 | lldb_private::VASprintf(Content, format, args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 150 | Stream << Content << "\n"; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 151 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 152 | WriteMessage(FinalMessage.str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | // Log only if all of the bits are set |
| 157 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 158 | void Log::LogIf(uint32_t bits, const char *format, ...) { |
| 159 | if (!m_options.AllSet(bits)) |
| 160 | return; |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 161 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 162 | va_list args; |
| 163 | va_start(args, format); |
| 164 | VAPrintf(format, args); |
| 165 | va_end(args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | //---------------------------------------------------------------------- |
| 169 | // Printing of errors that are not fatal. |
| 170 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 171 | void Log::Error(const char *format, ...) { |
| 172 | va_list args; |
| 173 | va_start(args, format); |
| 174 | VAError(format, args); |
| 175 | va_end(args); |
Zachary Turner | 610e529 | 2015-05-07 21:39:33 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 178 | void Log::VAError(const char *format, va_list args) { |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 179 | llvm::SmallString<64> Content; |
| 180 | VASprintf(Content, format, args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 182 | Printf("error: %s", Content.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 186 | // Printing of warnings that are not fatal only if verbose mode is |
| 187 | // enabled. |
| 188 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 189 | void Log::Verbose(const char *format, ...) { |
| 190 | if (!m_options.Test(LLDB_LOG_OPTION_VERBOSE)) |
| 191 | return; |
Zachary Turner | c159265 | 2015-04-29 22:55:28 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | va_list args; |
| 194 | va_start(args, format); |
| 195 | VAPrintf(format, args); |
| 196 | va_end(args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | //---------------------------------------------------------------------- |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 200 | // Printing of warnings that are not fatal. |
| 201 | //---------------------------------------------------------------------- |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | void Log::Warning(const char *format, ...) { |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 203 | llvm::SmallString<64> Content; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 204 | va_list args; |
| 205 | va_start(args, format); |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 206 | VASprintf(Content, format, args); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | va_end(args); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 209 | Printf("warning: %s", Content.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 212 | void Log::Register(llvm::StringRef name, Channel &channel) { |
| 213 | auto iter = g_channel_map->try_emplace(name, channel); |
| 214 | assert(iter.second == true); |
| 215 | (void)iter; |
| 216 | } |
| 217 | |
| 218 | void Log::Unregister(llvm::StringRef name) { |
| 219 | auto iter = g_channel_map->find(name); |
| 220 | assert(iter != g_channel_map->end()); |
| 221 | iter->second.channel.Disable(UINT32_MAX); |
| 222 | g_channel_map->erase(iter); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 225 | bool Log::EnableLogChannel( |
| 226 | const std::shared_ptr<llvm::raw_ostream> &log_stream_sp, |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 227 | uint32_t log_options, llvm::StringRef channel, |
| 228 | llvm::ArrayRef<const char *> categories, Stream &error_stream) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 229 | auto iter = g_channel_map->find(channel); |
| 230 | if (iter == g_channel_map->end()) { |
| 231 | error_stream.Format("Invalid log channel '{0}'.\n", channel); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 234 | uint32_t flags = categories.empty() |
| 235 | ? iter->second.channel.default_flags |
| 236 | : GetFlags(error_stream, *iter, categories); |
Pavel Labath | 88d081b | 2017-02-27 11:05:39 +0000 | [diff] [blame] | 237 | iter->second.channel.Enable(iter->second.log, log_stream_sp, log_options, |
| 238 | flags); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 239 | return true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 242 | bool Log::DisableLogChannel(llvm::StringRef channel, |
| 243 | llvm::ArrayRef<const char *> categories, |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 244 | Stream &error_stream) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 245 | auto iter = g_channel_map->find(channel); |
| 246 | if (iter == g_channel_map->end()) { |
| 247 | error_stream.Format("Invalid log channel '{0}'.\n", channel); |
| 248 | return false; |
| 249 | } |
Pavel Labath | 5e33690 | 2017-03-01 10:08:40 +0000 | [diff] [blame] | 250 | uint32_t flags = categories.empty() |
| 251 | ? UINT32_MAX |
| 252 | : GetFlags(error_stream, *iter, categories); |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 253 | iter->second.channel.Disable(flags); |
| 254 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 257 | bool Log::ListChannelCategories(llvm::StringRef channel, Stream &stream) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 258 | auto ch = g_channel_map->find(channel); |
| 259 | if (ch == g_channel_map->end()) { |
| 260 | stream.Format("Invalid log channel '{0}'.\n", channel); |
| 261 | return false; |
| 262 | } |
| 263 | ListCategories(stream, *ch); |
| 264 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void Log::DisableAllLogChannels(Stream *feedback_strm) { |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 268 | for (auto &entry : *g_channel_map) |
| 269 | entry.second.channel.Disable(UINT32_MAX); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 272 | void Log::ListAllLogChannels(Stream *strm) { |
Pavel Labath | 3474ebc | 2017-02-27 12:21:16 +0000 | [diff] [blame] | 273 | if (g_channel_map->empty()) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | strm->PutCString("No logging channels are currently registered.\n"); |
| 275 | return; |
| 276 | } |
| 277 | |
Pavel Labath | fb0d22d | 2017-02-17 13:27:42 +0000 | [diff] [blame] | 278 | for (const auto &channel : *g_channel_map) |
| 279 | ListCategories(*strm, channel); |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 280 | } |
Pavel Labath | 0cfd7dc9 | 2017-01-13 10:41:59 +0000 | [diff] [blame] | 281 | bool Log::GetVerbose() const { return m_options.Test(LLDB_LOG_OPTION_VERBOSE); } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 282 | |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 283 | void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file, |
| 284 | llvm::StringRef function) { |
| 285 | static uint32_t g_sequence_id = 0; |
| 286 | // Add a sequence ID if requested |
| 287 | if (m_options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE)) |
| 288 | OS << ++g_sequence_id << " "; |
| 289 | |
| 290 | // Timestamp if requested |
| 291 | if (m_options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) { |
| 292 | auto now = std::chrono::duration<double>( |
| 293 | std::chrono::system_clock::now().time_since_epoch()); |
| 294 | OS << llvm::formatv("{0:f9} ", now.count()); |
| 295 | } |
| 296 | |
| 297 | // Add the process and thread if requested |
| 298 | if (m_options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD)) |
| 299 | OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(), |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 300 | llvm::get_threadid()); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 301 | |
| 302 | // Add the thread name if requested |
| 303 | if (m_options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) { |
| 304 | llvm::SmallString<32> thread_name; |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 305 | llvm::get_thread_name(thread_name); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 306 | if (!thread_name.empty()) |
| 307 | OS << thread_name; |
| 308 | } |
| 309 | |
| 310 | if (m_options.Test(LLDB_LOG_OPTION_BACKTRACE)) |
| 311 | llvm::sys::PrintStackTrace(OS); |
| 312 | |
| 313 | if (m_options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) && |
| 314 | (!file.empty() || !function.empty())) { |
| 315 | file = llvm::sys::path::filename(file).take_front(40); |
| 316 | function = function.take_front(40); |
| 317 | OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str()); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | void Log::WriteMessage(const std::string &message) { |
| 322 | // Make a copy of our stream shared pointer in case someone disables our |
| 323 | // log while we are logging and releases the stream |
Pavel Labath | ba95a28 | 2017-02-21 09:58:23 +0000 | [diff] [blame] | 324 | auto stream_sp = GetStream(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 325 | if (!stream_sp) |
| 326 | return; |
| 327 | |
| 328 | if (m_options.Test(LLDB_LOG_OPTION_THREADSAFE)) { |
| 329 | static std::recursive_mutex g_LogThreadedMutex; |
| 330 | std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex); |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 331 | *stream_sp << message; |
| 332 | stream_sp->flush(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 333 | } else { |
Pavel Labath | 5fae71c | 2017-02-10 11:49:21 +0000 | [diff] [blame] | 334 | *stream_sp << message; |
| 335 | stream_sp->flush(); |
Pavel Labath | 107d9bb | 2017-01-18 11:00:26 +0000 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | |
| 339 | void Log::Format(llvm::StringRef file, llvm::StringRef function, |
| 340 | const llvm::formatv_object_base &payload) { |
| 341 | std::string message_string; |
| 342 | llvm::raw_string_ostream message(message_string); |
| 343 | WriteHeader(message, file, function); |
| 344 | message << payload << "\n"; |
| 345 | WriteMessage(message.str()); |
| 346 | } |