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