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