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