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