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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | // C Includes |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
| 14 | #include <stdarg.h> |
| 15 | #include <stdlib.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 16 | |
| 17 | // C++ Includes |
| 18 | #include <map> |
| 19 | #include <string> |
| 20 | |
| 21 | // Other libraries and framework includes |
| 22 | // Project includes |
| 23 | #include "lldb/Core/Debugger.h" |
| 24 | #include "lldb/Core/Log.h" |
| 25 | #include "lldb/Core/PluginManager.h" |
| 26 | #include "lldb/Core/StreamFile.h" |
| 27 | #include "lldb/Core/StreamString.h" |
| 28 | #include "lldb/Host/Host.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 29 | #include "lldb/Host/Mutex.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 30 | #include "lldb/Host/ThisThread.h" |
| 31 | #include "lldb/Host/TimeValue.h" |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 32 | #include "lldb/Interpreter/Args.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 33 | |
| 34 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | Log::Log () : |
| 39 | m_stream_sp(), |
| 40 | m_options(0), |
| 41 | m_mask_bits(0) |
| 42 | { |
| 43 | } |
| 44 | |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 45 | Log::Log (const StreamSP &stream_sp) : |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | m_stream_sp(stream_sp), |
| 47 | m_options(0), |
| 48 | m_mask_bits(0) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | Log::~Log () |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | Flags & |
| 57 | Log::GetOptions() |
| 58 | { |
| 59 | return m_options; |
| 60 | } |
| 61 | |
| 62 | const Flags & |
| 63 | Log::GetOptions() const |
| 64 | { |
| 65 | return m_options; |
| 66 | } |
| 67 | |
| 68 | Flags & |
| 69 | Log::GetMask() |
| 70 | { |
| 71 | return m_mask_bits; |
| 72 | } |
| 73 | |
| 74 | const Flags & |
| 75 | Log::GetMask() const |
| 76 | { |
| 77 | return m_mask_bits; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | //---------------------------------------------------------------------- |
| 82 | // All logging eventually boils down to this function call. If we have |
| 83 | // a callback registered, then we call the logging callback. If we have |
| 84 | // a valid file handle, we also log to the file. |
| 85 | //---------------------------------------------------------------------- |
| 86 | void |
| 87 | Log::PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args) |
| 88 | { |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 89 | // Make a copy of our stream shared pointer in case someone disables our |
| 90 | // log while we are logging and releases the stream |
| 91 | StreamSP stream_sp(m_stream_sp); |
| 92 | if (stream_sp) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | { |
| 94 | static uint32_t g_sequence_id = 0; |
| 95 | StreamString header; |
Greg Clayton | 64195a2 | 2011-02-23 00:35:02 +0000 | [diff] [blame] | 96 | // Enabling the thread safe logging actually deadlocks right now. |
| 97 | // Need to fix this at some point. |
| 98 | // static Mutex g_LogThreadedMutex(Mutex::eMutexTypeRecursive); |
| 99 | // Mutex::Locker locker (g_LogThreadedMutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | |
| 101 | // Add a sequence ID if requested |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 102 | if (m_options.Test (LLDB_LOG_OPTION_PREPEND_SEQUENCE)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 103 | header.Printf ("%u ", ++g_sequence_id); |
| 104 | |
| 105 | // Timestamp if requested |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 106 | if (m_options.Test (LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | { |
Virgile Bello | 0a3b151 | 2013-09-04 13:56:11 +0000 | [diff] [blame] | 108 | TimeValue now = TimeValue::Now(); |
Jason Molenda | 3c2daca | 2013-09-11 21:00:37 +0000 | [diff] [blame] | 109 | header.Printf ("%9d.%6.6d ", now.seconds(), now.nanoseconds()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Add the process and thread if requested |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 113 | if (m_options.Test (LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD)) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 114 | header.Printf ("[%4.4x/%4.4" PRIx64 "]: ", getpid(), Host::GetCurrentThreadID()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 115 | |
Ed Maste | cc913d14 | 2014-05-21 13:46:46 +0000 | [diff] [blame] | 116 | // Add the thread name if requested |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 117 | if (m_options.Test (LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | { |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame^] | 119 | llvm::SmallString<32> thread_name; |
| 120 | ThisThread::GetName(thread_name); |
Greg Clayton | 8571963 | 2013-02-27 22:51:58 +0000 | [diff] [blame] | 121 | if (!thread_name.empty()) |
| 122 | header.Printf ("%s ", thread_name.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | header.PrintfVarArg (format, args); |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 126 | stream_sp->Printf("%s\n", header.GetData()); |
Greg Clayton | 3a18e31 | 2012-10-08 22:41:53 +0000 | [diff] [blame] | 127 | |
| 128 | if (m_options.Test (LLDB_LOG_OPTION_BACKTRACE)) |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 129 | Host::Backtrace (*stream_sp, 1024); |
| 130 | stream_sp->Flush(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void |
| 136 | Log::PutCString (const char *cstr) |
| 137 | { |
| 138 | Printf ("%s", cstr); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | //---------------------------------------------------------------------- |
| 143 | // Simple variable argument logging with flags. |
| 144 | //---------------------------------------------------------------------- |
| 145 | void |
| 146 | Log::Printf(const char *format, ...) |
| 147 | { |
| 148 | va_list args; |
| 149 | va_start (args, format); |
| 150 | PrintfWithFlagsVarArg (0, format, args); |
| 151 | va_end (args); |
| 152 | } |
| 153 | |
| 154 | void |
| 155 | Log::VAPrintf (const char *format, va_list args) |
| 156 | { |
| 157 | PrintfWithFlagsVarArg (0, format, args); |
| 158 | } |
| 159 | |
| 160 | |
| 161 | //---------------------------------------------------------------------- |
| 162 | // Simple variable argument logging with flags. |
| 163 | //---------------------------------------------------------------------- |
| 164 | void |
| 165 | Log::PrintfWithFlags (uint32_t flags, const char *format, ...) |
| 166 | { |
| 167 | va_list args; |
| 168 | va_start (args, format); |
| 169 | PrintfWithFlagsVarArg (flags, format, args); |
| 170 | va_end (args); |
| 171 | } |
| 172 | |
| 173 | //---------------------------------------------------------------------- |
| 174 | // Print debug strings if and only if the global debug option is set to |
| 175 | // a non-zero value. |
| 176 | //---------------------------------------------------------------------- |
| 177 | void |
| 178 | Log::Debug (const char *format, ...) |
| 179 | { |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 180 | if (GetOptions().Test(LLDB_LOG_OPTION_DEBUG)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | { |
| 182 | va_list args; |
| 183 | va_start (args, format); |
| 184 | PrintfWithFlagsVarArg (LLDB_LOG_FLAG_DEBUG, format, args); |
| 185 | va_end (args); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
| 190 | //---------------------------------------------------------------------- |
| 191 | // Print debug strings if and only if the global debug option is set to |
| 192 | // a non-zero value. |
| 193 | //---------------------------------------------------------------------- |
| 194 | void |
| 195 | Log::DebugVerbose (const char *format, ...) |
| 196 | { |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 197 | if (GetOptions().AllSet (LLDB_LOG_OPTION_DEBUG | LLDB_LOG_OPTION_VERBOSE)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 198 | { |
| 199 | va_list args; |
| 200 | va_start (args, format); |
| 201 | PrintfWithFlagsVarArg (LLDB_LOG_FLAG_DEBUG | LLDB_LOG_FLAG_VERBOSE, format, args); |
| 202 | va_end (args); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | //---------------------------------------------------------------------- |
| 208 | // Log only if all of the bits are set |
| 209 | //---------------------------------------------------------------------- |
| 210 | void |
| 211 | Log::LogIf (uint32_t bits, const char *format, ...) |
| 212 | { |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 213 | if (m_options.AllSet (bits)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | { |
| 215 | va_list args; |
| 216 | va_start (args, format); |
| 217 | PrintfWithFlagsVarArg (0, format, args); |
| 218 | va_end (args); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | |
| 223 | //---------------------------------------------------------------------- |
| 224 | // Printing of errors that are not fatal. |
| 225 | //---------------------------------------------------------------------- |
| 226 | void |
| 227 | Log::Error (const char *format, ...) |
| 228 | { |
| 229 | char *arg_msg = NULL; |
| 230 | va_list args; |
| 231 | va_start (args, format); |
| 232 | ::vasprintf (&arg_msg, format, args); |
| 233 | va_end (args); |
| 234 | |
| 235 | if (arg_msg != NULL) |
| 236 | { |
| 237 | PrintfWithFlags (LLDB_LOG_FLAG_ERROR, "error: %s", arg_msg); |
| 238 | free (arg_msg); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | //---------------------------------------------------------------------- |
| 243 | // Printing of errors that ARE fatal. Exit with ERR exit code |
| 244 | // immediately. |
| 245 | //---------------------------------------------------------------------- |
| 246 | void |
| 247 | Log::FatalError (int err, const char *format, ...) |
| 248 | { |
| 249 | char *arg_msg = NULL; |
| 250 | va_list args; |
| 251 | va_start (args, format); |
| 252 | ::vasprintf (&arg_msg, format, args); |
| 253 | va_end (args); |
| 254 | |
| 255 | if (arg_msg != NULL) |
| 256 | { |
| 257 | PrintfWithFlags (LLDB_LOG_FLAG_ERROR | LLDB_LOG_FLAG_FATAL, "error: %s", arg_msg); |
| 258 | ::free (arg_msg); |
| 259 | } |
| 260 | ::exit (err); |
| 261 | } |
| 262 | |
| 263 | |
| 264 | //---------------------------------------------------------------------- |
| 265 | // Printing of warnings that are not fatal only if verbose mode is |
| 266 | // enabled. |
| 267 | //---------------------------------------------------------------------- |
| 268 | void |
| 269 | Log::Verbose (const char *format, ...) |
| 270 | { |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 271 | if (m_options.Test(LLDB_LOG_OPTION_VERBOSE)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 272 | { |
| 273 | va_list args; |
| 274 | va_start (args, format); |
| 275 | PrintfWithFlagsVarArg (LLDB_LOG_FLAG_VERBOSE, format, args); |
| 276 | va_end (args); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | //---------------------------------------------------------------------- |
| 281 | // Printing of warnings that are not fatal only if verbose mode is |
| 282 | // enabled. |
| 283 | //---------------------------------------------------------------------- |
| 284 | void |
| 285 | Log::WarningVerbose (const char *format, ...) |
| 286 | { |
Greg Clayton | 73b472d | 2010-10-27 03:32:59 +0000 | [diff] [blame] | 287 | if (m_options.Test(LLDB_LOG_OPTION_VERBOSE)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 288 | { |
| 289 | char *arg_msg = NULL; |
| 290 | va_list args; |
| 291 | va_start (args, format); |
| 292 | ::vasprintf (&arg_msg, format, args); |
| 293 | va_end (args); |
| 294 | |
| 295 | if (arg_msg != NULL) |
| 296 | { |
| 297 | PrintfWithFlags (LLDB_LOG_FLAG_WARNING | LLDB_LOG_FLAG_VERBOSE, "warning: %s", arg_msg); |
| 298 | free (arg_msg); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | //---------------------------------------------------------------------- |
| 303 | // Printing of warnings that are not fatal. |
| 304 | //---------------------------------------------------------------------- |
| 305 | void |
| 306 | Log::Warning (const char *format, ...) |
| 307 | { |
| 308 | char *arg_msg = NULL; |
| 309 | va_list args; |
| 310 | va_start (args, format); |
| 311 | ::vasprintf (&arg_msg, format, args); |
| 312 | va_end (args); |
| 313 | |
| 314 | if (arg_msg != NULL) |
| 315 | { |
| 316 | PrintfWithFlags (LLDB_LOG_FLAG_WARNING, "warning: %s", arg_msg); |
| 317 | free (arg_msg); |
| 318 | } |
| 319 | } |
| 320 | |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 321 | typedef std::map <ConstString, Log::Callbacks> CallbackMap; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | typedef CallbackMap::iterator CallbackMapIter; |
| 323 | |
| 324 | typedef std::map <ConstString, LogChannelSP> LogChannelMap; |
| 325 | typedef LogChannelMap::iterator LogChannelMapIter; |
| 326 | |
| 327 | |
| 328 | // Surround our callback map with a singleton function so we don't have any |
| 329 | // global initializers. |
| 330 | static CallbackMap & |
| 331 | GetCallbackMap () |
| 332 | { |
| 333 | static CallbackMap g_callback_map; |
| 334 | return g_callback_map; |
| 335 | } |
| 336 | |
| 337 | static LogChannelMap & |
| 338 | GetChannelMap () |
| 339 | { |
| 340 | static LogChannelMap g_channel_map; |
| 341 | return g_channel_map; |
| 342 | } |
| 343 | |
| 344 | void |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 345 | Log::RegisterLogChannel (const ConstString &channel, const Log::Callbacks &log_callbacks) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 346 | { |
| 347 | GetCallbackMap().insert(std::make_pair(channel, log_callbacks)); |
| 348 | } |
| 349 | |
| 350 | bool |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 351 | Log::UnregisterLogChannel (const ConstString &channel) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 352 | { |
| 353 | return GetCallbackMap().erase(channel) != 0; |
| 354 | } |
| 355 | |
| 356 | bool |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 357 | Log::GetLogChannelCallbacks (const ConstString &channel, Log::Callbacks &log_callbacks) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | { |
| 359 | CallbackMap &callback_map = GetCallbackMap (); |
| 360 | CallbackMapIter pos = callback_map.find(channel); |
| 361 | if (pos != callback_map.end()) |
| 362 | { |
| 363 | log_callbacks = pos->second; |
| 364 | return true; |
| 365 | } |
Greg Clayton | 72b77eb | 2011-02-04 21:13:05 +0000 | [diff] [blame] | 366 | ::memset (&log_callbacks, 0, sizeof(log_callbacks)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 367 | return false; |
| 368 | } |
| 369 | |
| 370 | void |
| 371 | Log::EnableAllLogChannels |
| 372 | ( |
| 373 | StreamSP &log_stream_sp, |
| 374 | uint32_t log_options, |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 375 | const char **categories, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | Stream *feedback_strm |
| 377 | ) |
| 378 | { |
| 379 | CallbackMap &callback_map = GetCallbackMap (); |
| 380 | CallbackMapIter pos, end = callback_map.end(); |
| 381 | |
| 382 | for (pos = callback_map.begin(); pos != end; ++pos) |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 383 | pos->second.enable (log_stream_sp, log_options, categories, feedback_strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 384 | |
| 385 | LogChannelMap &channel_map = GetChannelMap (); |
| 386 | LogChannelMapIter channel_pos, channel_end = channel_map.end(); |
| 387 | for (channel_pos = channel_map.begin(); channel_pos != channel_end; ++channel_pos) |
| 388 | { |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 389 | channel_pos->second->Enable (log_stream_sp, log_options, feedback_strm, categories); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | } |
| 393 | |
| 394 | void |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 395 | Log::AutoCompleteChannelName (const char *channel_name, StringList &matches) |
| 396 | { |
| 397 | LogChannelMap &map = GetChannelMap (); |
| 398 | LogChannelMapIter pos, end = map.end(); |
| 399 | for (pos = map.begin(); pos != end; ++pos) |
| 400 | { |
| 401 | const char *pos_channel_name = pos->first.GetCString(); |
| 402 | if (channel_name && channel_name[0]) |
| 403 | { |
| 404 | if (NameMatches (channel_name, eNameMatchStartsWith, pos_channel_name)) |
| 405 | { |
| 406 | matches.AppendString(pos_channel_name); |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | matches.AppendString(pos_channel_name); |
| 411 | |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | void |
Caroline Tice | 20ad3c4 | 2010-10-29 21:48:37 +0000 | [diff] [blame] | 416 | Log::DisableAllLogChannels (Stream *feedback_strm) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 417 | { |
| 418 | CallbackMap &callback_map = GetCallbackMap (); |
| 419 | CallbackMapIter pos, end = callback_map.end(); |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 420 | const char *categories[1] = {NULL}; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 421 | |
| 422 | for (pos = callback_map.begin(); pos != end; ++pos) |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 423 | pos->second.disable (categories, feedback_strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 424 | |
| 425 | LogChannelMap &channel_map = GetChannelMap (); |
| 426 | LogChannelMapIter channel_pos, channel_end = channel_map.end(); |
| 427 | for (channel_pos = channel_map.begin(); channel_pos != channel_end; ++channel_pos) |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 428 | channel_pos->second->Disable (categories, feedback_strm); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | void |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 432 | Log::Initialize() |
| 433 | { |
| 434 | Log::Callbacks log_callbacks = { DisableLog, EnableLog, ListLogCategories }; |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 435 | Log::RegisterLogChannel (ConstString("lldb"), log_callbacks); |
Greg Clayton | 99d0faf | 2010-11-18 23:32:35 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void |
| 439 | Log::Terminate () |
| 440 | { |
| 441 | DisableAllLogChannels (NULL); |
| 442 | } |
| 443 | |
| 444 | void |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 445 | Log::ListAllLogChannels (Stream *strm) |
| 446 | { |
| 447 | CallbackMap &callback_map = GetCallbackMap (); |
| 448 | LogChannelMap &channel_map = GetChannelMap (); |
| 449 | |
| 450 | if (callback_map.empty() && channel_map.empty()) |
| 451 | { |
| 452 | strm->PutCString ("No logging channels are currently registered.\n"); |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | CallbackMapIter pos, end = callback_map.end(); |
| 457 | for (pos = callback_map.begin(); pos != end; ++pos) |
| 458 | pos->second.list_categories (strm); |
| 459 | |
| 460 | uint32_t idx = 0; |
| 461 | const char *name; |
| 462 | for (idx = 0; (name = PluginManager::GetLogChannelCreateNameAtIndex (idx)) != NULL; ++idx) |
| 463 | { |
| 464 | LogChannelSP log_channel_sp(LogChannel::FindPlugin (name)); |
| 465 | if (log_channel_sp) |
| 466 | log_channel_sp->ListCategories (strm); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | bool |
| 471 | Log::GetVerbose() const |
| 472 | { |
Jim Ingham | d8d148e | 2011-01-22 01:24:30 +0000 | [diff] [blame] | 473 | // FIXME: This has to be centralized between the stream and the log... |
| 474 | if (m_options.Test(LLDB_LOG_OPTION_VERBOSE)) |
| 475 | return true; |
| 476 | |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 477 | // Make a copy of our stream shared pointer in case someone disables our |
| 478 | // log while we are logging and releases the stream |
| 479 | StreamSP stream_sp(m_stream_sp); |
| 480 | if (stream_sp) |
| 481 | return stream_sp->GetVerbose(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 482 | return false; |
| 483 | } |
| 484 | |
| 485 | //------------------------------------------------------------------ |
| 486 | // Returns true if the debug flag bit is set in this stream. |
| 487 | //------------------------------------------------------------------ |
| 488 | bool |
| 489 | Log::GetDebug() const |
| 490 | { |
Greg Clayton | e98008c | 2014-02-13 23:34:38 +0000 | [diff] [blame] | 491 | // Make a copy of our stream shared pointer in case someone disables our |
| 492 | // log while we are logging and releases the stream |
| 493 | StreamSP stream_sp(m_stream_sp); |
| 494 | if (stream_sp) |
| 495 | return stream_sp->GetDebug(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 496 | return false; |
| 497 | } |
| 498 | |
| 499 | |
| 500 | LogChannelSP |
| 501 | LogChannel::FindPlugin (const char *plugin_name) |
| 502 | { |
| 503 | LogChannelSP log_channel_sp; |
| 504 | LogChannelMap &channel_map = GetChannelMap (); |
| 505 | ConstString log_channel_name (plugin_name); |
| 506 | LogChannelMapIter pos = channel_map.find (log_channel_name); |
| 507 | if (pos == channel_map.end()) |
| 508 | { |
Greg Clayton | 57abc5d | 2013-05-10 21:47:16 +0000 | [diff] [blame] | 509 | ConstString const_plugin_name (plugin_name); |
| 510 | LogChannelCreateInstance create_callback = PluginManager::GetLogChannelCreateCallbackForPluginName (const_plugin_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 511 | if (create_callback) |
| 512 | { |
| 513 | log_channel_sp.reset(create_callback()); |
| 514 | if (log_channel_sp) |
| 515 | { |
| 516 | // Cache the one and only loaded instance of each log channel |
| 517 | // plug-in after it has been loaded once. |
| 518 | channel_map[log_channel_name] = log_channel_sp; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | else |
| 523 | { |
| 524 | // We have already loaded an instance of this log channel class, |
| 525 | // so just return the cached instance. |
| 526 | log_channel_sp = pos->second; |
| 527 | } |
| 528 | return log_channel_sp; |
| 529 | } |
| 530 | |
| 531 | LogChannel::LogChannel () : |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 532 | m_log_ap () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | { |
| 534 | } |
| 535 | |
| 536 | LogChannel::~LogChannel () |
| 537 | { |
| 538 | } |
| 539 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | |