1 //===-- Log.cpp -------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
21 // Other libraries and framework 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"
29 #include "lldb/Host/TimeValue.h"
30 #include "lldb/Host/Mutex.h"
31 #include "lldb/Interpreter/Args.h"
33 using namespace lldb_private;
42 Log::Log (const StreamSP &stream_sp) :
43 m_stream_sp(stream_sp),
60 Log::GetOptions() const
78 //----------------------------------------------------------------------
79 // All logging eventually boils down to this function call. If we have
80 // a callback registered, then we call the logging callback. If we have
81 // a valid file handle, we also log to the file.
82 //----------------------------------------------------------------------
84 Log::PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args)
86 // Make a copy of our stream shared pointer in case someone disables our
87 // log while we are logging and releases the stream
88 StreamSP stream_sp(m_stream_sp);
91 static uint32_t g_sequence_id = 0;
93 // Enabling the thread safe logging actually deadlocks right now.
94 // Need to fix this at some point.
95 // static Mutex g_LogThreadedMutex(Mutex::eMutexTypeRecursive);
96 // Mutex::Locker locker (g_LogThreadedMutex);
98 // Add a sequence ID if requested
99 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_SEQUENCE))
100 header.Printf ("%u ", ++g_sequence_id);
102 // Timestamp if requested
103 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_TIMESTAMP))
105 TimeValue now = TimeValue::Now();
106 header.Printf ("%9d.%6.6d ", now.seconds(), now.nanoseconds());
109 // Add the process and thread if requested
110 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
111 header.Printf ("[%4.4x/%4.4" PRIx64 "]: ", getpid(), Host::GetCurrentThreadID());
113 // Add the process and thread if requested
114 if (m_options.Test (LLDB_LOG_OPTION_PREPEND_THREAD_NAME))
116 std::string thread_name (Host::GetThreadName (getpid(), Host::GetCurrentThreadID()));
117 if (!thread_name.empty())
118 header.Printf ("%s ", thread_name.c_str());
121 header.PrintfVarArg (format, args);
122 stream_sp->Printf("%s\n", header.GetData());
124 if (m_options.Test (LLDB_LOG_OPTION_BACKTRACE))
125 Host::Backtrace (*stream_sp, 1024);
132 Log::PutCString (const char *cstr)
138 //----------------------------------------------------------------------
139 // Simple variable argument logging with flags.
140 //----------------------------------------------------------------------
142 Log::Printf(const char *format, ...)
145 va_start (args, format);
146 PrintfWithFlagsVarArg (0, format, args);
151 Log::VAPrintf (const char *format, va_list args)
153 PrintfWithFlagsVarArg (0, format, args);
157 //----------------------------------------------------------------------
158 // Simple variable argument logging with flags.
159 //----------------------------------------------------------------------
161 Log::PrintfWithFlags (uint32_t flags, const char *format, ...)
164 va_start (args, format);
165 PrintfWithFlagsVarArg (flags, format, args);
169 //----------------------------------------------------------------------
170 // Print debug strings if and only if the global debug option is set to
172 //----------------------------------------------------------------------
174 Log::Debug (const char *format, ...)
176 if (GetOptions().Test(LLDB_LOG_OPTION_DEBUG))
179 va_start (args, format);
180 PrintfWithFlagsVarArg (LLDB_LOG_FLAG_DEBUG, format, args);
186 //----------------------------------------------------------------------
187 // Print debug strings if and only if the global debug option is set to
189 //----------------------------------------------------------------------
191 Log::DebugVerbose (const char *format, ...)
193 if (GetOptions().AllSet (LLDB_LOG_OPTION_DEBUG | LLDB_LOG_OPTION_VERBOSE))
196 va_start (args, format);
197 PrintfWithFlagsVarArg (LLDB_LOG_FLAG_DEBUG | LLDB_LOG_FLAG_VERBOSE, format, args);
203 //----------------------------------------------------------------------
204 // Log only if all of the bits are set
205 //----------------------------------------------------------------------
207 Log::LogIf (uint32_t bits, const char *format, ...)
209 if (m_options.AllSet (bits))
212 va_start (args, format);
213 PrintfWithFlagsVarArg (0, format, args);
219 //----------------------------------------------------------------------
220 // Printing of errors that are not fatal.
221 //----------------------------------------------------------------------
223 Log::Error (const char *format, ...)
225 char *arg_msg = NULL;
227 va_start (args, format);
228 ::vasprintf (&arg_msg, format, args);
233 PrintfWithFlags (LLDB_LOG_FLAG_ERROR, "error: %s", arg_msg);
238 //----------------------------------------------------------------------
239 // Printing of errors that ARE fatal. Exit with ERR exit code
241 //----------------------------------------------------------------------
243 Log::FatalError (int err, const char *format, ...)
245 char *arg_msg = NULL;
247 va_start (args, format);
248 ::vasprintf (&arg_msg, format, args);
253 PrintfWithFlags (LLDB_LOG_FLAG_ERROR | LLDB_LOG_FLAG_FATAL, "error: %s", arg_msg);
260 //----------------------------------------------------------------------
261 // Printing of warnings that are not fatal only if verbose mode is
263 //----------------------------------------------------------------------
265 Log::Verbose (const char *format, ...)
267 if (m_options.Test(LLDB_LOG_OPTION_VERBOSE))
270 va_start (args, format);
271 PrintfWithFlagsVarArg (LLDB_LOG_FLAG_VERBOSE, format, args);
276 //----------------------------------------------------------------------
277 // Printing of warnings that are not fatal only if verbose mode is
279 //----------------------------------------------------------------------
281 Log::WarningVerbose (const char *format, ...)
283 if (m_options.Test(LLDB_LOG_OPTION_VERBOSE))
285 char *arg_msg = NULL;
287 va_start (args, format);
288 ::vasprintf (&arg_msg, format, args);
293 PrintfWithFlags (LLDB_LOG_FLAG_WARNING | LLDB_LOG_FLAG_VERBOSE, "warning: %s", arg_msg);
298 //----------------------------------------------------------------------
299 // Printing of warnings that are not fatal.
300 //----------------------------------------------------------------------
302 Log::Warning (const char *format, ...)
304 char *arg_msg = NULL;
306 va_start (args, format);
307 ::vasprintf (&arg_msg, format, args);
312 PrintfWithFlags (LLDB_LOG_FLAG_WARNING, "warning: %s", arg_msg);
317 typedef std::map <ConstString, Log::Callbacks> CallbackMap;
318 typedef CallbackMap::iterator CallbackMapIter;
320 typedef std::map <ConstString, LogChannelSP> LogChannelMap;
321 typedef LogChannelMap::iterator LogChannelMapIter;
324 // Surround our callback map with a singleton function so we don't have any
325 // global initializers.
329 static CallbackMap g_callback_map;
330 return g_callback_map;
333 static LogChannelMap &
336 static LogChannelMap g_channel_map;
337 return g_channel_map;
341 Log::RegisterLogChannel (const ConstString &channel, const Log::Callbacks &log_callbacks)
343 GetCallbackMap().insert(std::make_pair(channel, log_callbacks));
347 Log::UnregisterLogChannel (const ConstString &channel)
349 return GetCallbackMap().erase(channel) != 0;
353 Log::GetLogChannelCallbacks (const ConstString &channel, Log::Callbacks &log_callbacks)
355 CallbackMap &callback_map = GetCallbackMap ();
356 CallbackMapIter pos = callback_map.find(channel);
357 if (pos != callback_map.end())
359 log_callbacks = pos->second;
362 ::memset (&log_callbacks, 0, sizeof(log_callbacks));
367 Log::EnableAllLogChannels
369 StreamSP &log_stream_sp,
370 uint32_t log_options,
371 const char **categories,
372 Stream *feedback_strm
375 CallbackMap &callback_map = GetCallbackMap ();
376 CallbackMapIter pos, end = callback_map.end();
378 for (pos = callback_map.begin(); pos != end; ++pos)
379 pos->second.enable (log_stream_sp, log_options, categories, feedback_strm);
381 LogChannelMap &channel_map = GetChannelMap ();
382 LogChannelMapIter channel_pos, channel_end = channel_map.end();
383 for (channel_pos = channel_map.begin(); channel_pos != channel_end; ++channel_pos)
385 channel_pos->second->Enable (log_stream_sp, log_options, feedback_strm, categories);
391 Log::AutoCompleteChannelName (const char *channel_name, StringList &matches)
393 LogChannelMap &map = GetChannelMap ();
394 LogChannelMapIter pos, end = map.end();
395 for (pos = map.begin(); pos != end; ++pos)
397 const char *pos_channel_name = pos->first.GetCString();
398 if (channel_name && channel_name[0])
400 if (NameMatches (channel_name, eNameMatchStartsWith, pos_channel_name))
402 matches.AppendString(pos_channel_name);
406 matches.AppendString(pos_channel_name);
412 Log::DisableAllLogChannels (Stream *feedback_strm)
414 CallbackMap &callback_map = GetCallbackMap ();
415 CallbackMapIter pos, end = callback_map.end();
416 const char *categories[1] = {NULL};
418 for (pos = callback_map.begin(); pos != end; ++pos)
419 pos->second.disable (categories, feedback_strm);
421 LogChannelMap &channel_map = GetChannelMap ();
422 LogChannelMapIter channel_pos, channel_end = channel_map.end();
423 for (channel_pos = channel_map.begin(); channel_pos != channel_end; ++channel_pos)
424 channel_pos->second->Disable (categories, feedback_strm);
430 Log::Callbacks log_callbacks = { DisableLog, EnableLog, ListLogCategories };
431 Log::RegisterLogChannel (ConstString("lldb"), log_callbacks);
437 DisableAllLogChannels (NULL);
441 Log::ListAllLogChannels (Stream *strm)
443 CallbackMap &callback_map = GetCallbackMap ();
444 LogChannelMap &channel_map = GetChannelMap ();
446 if (callback_map.empty() && channel_map.empty())
448 strm->PutCString ("No logging channels are currently registered.\n");
452 CallbackMapIter pos, end = callback_map.end();
453 for (pos = callback_map.begin(); pos != end; ++pos)
454 pos->second.list_categories (strm);
458 for (idx = 0; (name = PluginManager::GetLogChannelCreateNameAtIndex (idx)) != NULL; ++idx)
460 LogChannelSP log_channel_sp(LogChannel::FindPlugin (name));
462 log_channel_sp->ListCategories (strm);
467 Log::GetVerbose() const
469 // FIXME: This has to be centralized between the stream and the log...
470 if (m_options.Test(LLDB_LOG_OPTION_VERBOSE))
473 // Make a copy of our stream shared pointer in case someone disables our
474 // log while we are logging and releases the stream
475 StreamSP stream_sp(m_stream_sp);
477 return stream_sp->GetVerbose();
481 //------------------------------------------------------------------
482 // Returns true if the debug flag bit is set in this stream.
483 //------------------------------------------------------------------
485 Log::GetDebug() const
487 // Make a copy of our stream shared pointer in case someone disables our
488 // log while we are logging and releases the stream
489 StreamSP stream_sp(m_stream_sp);
491 return stream_sp->GetDebug();
497 LogChannel::FindPlugin (const char *plugin_name)
499 LogChannelSP log_channel_sp;
500 LogChannelMap &channel_map = GetChannelMap ();
501 ConstString log_channel_name (plugin_name);
502 LogChannelMapIter pos = channel_map.find (log_channel_name);
503 if (pos == channel_map.end())
505 ConstString const_plugin_name (plugin_name);
506 LogChannelCreateInstance create_callback = PluginManager::GetLogChannelCreateCallbackForPluginName (const_plugin_name);
509 log_channel_sp.reset(create_callback());
512 // Cache the one and only loaded instance of each log channel
513 // plug-in after it has been loaded once.
514 channel_map[log_channel_name] = log_channel_sp;
520 // We have already loaded an instance of this log channel class,
521 // so just return the cached instance.
522 log_channel_sp = pos->second;
524 return log_channel_sp;
527 LogChannel::LogChannel () :
532 LogChannel::~LogChannel ()