2010-06-09 00:52:24 +08:00
|
|
|
//===-- Log.cpp -------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Project includes
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
|
|
|
|
2015-03-19 05:31:45 +08:00
|
|
|
#include "lldb/Utility/NameMatches.h"
|
2017-02-03 05:39:50 +08:00
|
|
|
#include "lldb/Utility/StreamString.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/VASPrintf.h"
|
2014-09-10 04:54:56 +08:00
|
|
|
|
2016-11-02 20:18:42 +08:00
|
|
|
// Other libraries and framework includes
|
2017-02-17 21:27:42 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2016-11-02 20:18:42 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/Support/Chrono.h"
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2016-11-02 20:18:42 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2016-11-02 20:18:42 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
// C Includes
|
|
|
|
// C++ Includes
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
llvm::ManagedStatic<Log::ChannelMap> Log::g_channel_map;
|
2017-02-17 21:27:42 +08:00
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
void Log::ListCategories(Stream &stream, const ChannelMap::value_type &entry) {
|
2017-02-17 21:27:42 +08:00
|
|
|
stream.Format("Logging categories for '{0}':\n", entry.first());
|
|
|
|
stream.Format(" all - all available logging categories\n");
|
|
|
|
stream.Format(" default - default set of logging categories\n");
|
2017-03-09 18:16:07 +08:00
|
|
|
for (const auto &category : entry.second.m_channel.categories)
|
2017-02-17 21:27:42 +08:00
|
|
|
stream.Format(" {0} - {1}\n", category.name, category.description);
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
uint32_t Log::GetFlags(Stream &stream, const ChannelMap::value_type &entry,
|
2017-03-01 18:08:40 +08:00
|
|
|
llvm::ArrayRef<const char *> categories) {
|
2017-02-17 21:27:42 +08:00
|
|
|
bool list_categories = false;
|
|
|
|
uint32_t flags = 0;
|
2017-03-01 18:08:40 +08:00
|
|
|
for (const char *category : categories) {
|
|
|
|
if (llvm::StringRef("all").equals_lower(category)) {
|
2017-02-17 21:27:42 +08:00
|
|
|
flags |= UINT32_MAX;
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-01 18:08:40 +08:00
|
|
|
if (llvm::StringRef("default").equals_lower(category)) {
|
2017-03-09 18:16:07 +08:00
|
|
|
flags |= entry.second.m_channel.default_flags;
|
2017-02-17 21:27:42 +08:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-01 18:08:40 +08:00
|
|
|
auto cat = llvm::find_if(
|
2017-03-09 18:16:07 +08:00
|
|
|
entry.second.m_channel.categories,
|
2017-03-01 18:08:40 +08:00
|
|
|
[&](const Log::Category &c) { return c.name.equals_lower(category); });
|
2017-03-09 18:16:07 +08:00
|
|
|
if (cat != entry.second.m_channel.categories.end()) {
|
2017-02-17 21:27:42 +08:00
|
|
|
flags |= cat->flag;
|
|
|
|
continue;
|
|
|
|
}
|
2017-03-01 18:08:40 +08:00
|
|
|
stream.Format("error: unrecognized log category '{0}'\n", category);
|
2017-02-17 21:27:42 +08:00
|
|
|
list_categories = true;
|
|
|
|
}
|
|
|
|
if (list_categories)
|
|
|
|
ListCategories(stream, entry);
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
void Log::Enable(const std::shared_ptr<llvm::raw_ostream> &stream_sp,
|
|
|
|
uint32_t options, uint32_t flags) {
|
|
|
|
llvm::sys::ScopedWriter lock(m_mutex);
|
2017-02-17 21:27:42 +08:00
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
uint32_t mask = m_mask.fetch_or(flags, std::memory_order_relaxed);
|
|
|
|
if (mask | flags) {
|
|
|
|
m_options.store(options, std::memory_order_relaxed);
|
|
|
|
m_stream_sp = stream_sp;
|
|
|
|
m_channel.log_ptr.store(this, std::memory_order_relaxed);
|
2017-02-17 21:27:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
void Log::Disable(uint32_t flags) {
|
|
|
|
llvm::sys::ScopedWriter lock(m_mutex);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
uint32_t mask = m_mask.fetch_and(~flags, std::memory_order_relaxed);
|
|
|
|
if (!(mask & ~flags)) {
|
|
|
|
m_stream_sp.reset();
|
|
|
|
m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
const Flags Log::GetOptions() const {
|
|
|
|
return m_options.load(std::memory_order_relaxed);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
const Flags Log::GetMask() const {
|
|
|
|
return m_mask.load(std::memory_order_relaxed);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::PutCString(const char *cstr) { Printf("%s", cstr); }
|
2016-11-17 05:15:24 +08:00
|
|
|
void Log::PutString(llvm::StringRef str) { PutCString(str.str().c_str()); }
|
2015-04-30 06:55:28 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Simple variable argument logging with flags.
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::Printf(const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VAPrintf(format, args);
|
|
|
|
va_end(args);
|
2015-04-30 06:55:28 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// All logging eventually boils down to this function call. If we have
|
|
|
|
// a callback registered, then we call the logging callback. If we have
|
|
|
|
// a valid file handle, we also log to the file.
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::VAPrintf(const char *format, va_list args) {
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::SmallString<64> FinalMessage;
|
|
|
|
llvm::raw_svector_ostream Stream(FinalMessage);
|
|
|
|
WriteHeader(Stream, "", "");
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::SmallString<64> Content;
|
|
|
|
lldb_private::VASprintf(Content, format, args);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
Stream << Content << "\n";
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
WriteMessage(FinalMessage.str());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Printing of errors that are not fatal.
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::Error(const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VAError(format, args);
|
|
|
|
va_end(args);
|
2015-05-08 05:39:33 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::VAError(const char *format, va_list args) {
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::SmallString<64> Content;
|
|
|
|
VASprintf(Content, format, args);
|
2015-04-30 06:55:28 +08:00
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
Printf("error: %s", Content.c_str());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Printing of warnings that are not fatal only if verbose mode is
|
|
|
|
// enabled.
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::Verbose(const char *format, ...) {
|
2017-03-09 18:16:07 +08:00
|
|
|
if (!GetVerbose())
|
2016-09-07 04:57:50 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VAPrintf(format, args);
|
|
|
|
va_end(args);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Printing of warnings that are not fatal.
|
|
|
|
//----------------------------------------------------------------------
|
2016-09-07 04:57:50 +08:00
|
|
|
void Log::Warning(const char *format, ...) {
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::SmallString<64> Content;
|
2016-09-07 04:57:50 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2017-03-04 04:56:28 +08:00
|
|
|
VASprintf(Content, format, args);
|
2016-09-07 04:57:50 +08:00
|
|
|
va_end(args);
|
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
Printf("warning: %s", Content.c_str());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 21:27:42 +08:00
|
|
|
void Log::Register(llvm::StringRef name, Channel &channel) {
|
|
|
|
auto iter = g_channel_map->try_emplace(name, channel);
|
|
|
|
assert(iter.second == true);
|
|
|
|
(void)iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::Unregister(llvm::StringRef name) {
|
|
|
|
auto iter = g_channel_map->find(name);
|
|
|
|
assert(iter != g_channel_map->end());
|
2017-03-09 18:16:07 +08:00
|
|
|
iter->second.Disable(UINT32_MAX);
|
2017-02-17 21:27:42 +08:00
|
|
|
g_channel_map->erase(iter);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-10 19:49:21 +08:00
|
|
|
bool Log::EnableLogChannel(
|
|
|
|
const std::shared_ptr<llvm::raw_ostream> &log_stream_sp,
|
2017-03-01 18:08:40 +08:00
|
|
|
uint32_t log_options, llvm::StringRef channel,
|
|
|
|
llvm::ArrayRef<const char *> categories, Stream &error_stream) {
|
2017-02-17 21:27:42 +08:00
|
|
|
auto iter = g_channel_map->find(channel);
|
|
|
|
if (iter == g_channel_map->end()) {
|
|
|
|
error_stream.Format("Invalid log channel '{0}'.\n", channel);
|
2010-06-09 00:52:24 +08:00
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2017-03-01 18:08:40 +08:00
|
|
|
uint32_t flags = categories.empty()
|
2017-03-09 18:16:07 +08:00
|
|
|
? iter->second.m_channel.default_flags
|
2017-03-01 18:08:40 +08:00
|
|
|
: GetFlags(error_stream, *iter, categories);
|
2017-03-09 18:16:07 +08:00
|
|
|
iter->second.Enable(log_stream_sp, log_options, flags);
|
2017-02-17 21:27:42 +08:00
|
|
|
return true;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2017-03-01 18:08:40 +08:00
|
|
|
bool Log::DisableLogChannel(llvm::StringRef channel,
|
|
|
|
llvm::ArrayRef<const char *> categories,
|
2017-02-17 21:27:42 +08:00
|
|
|
Stream &error_stream) {
|
|
|
|
auto iter = g_channel_map->find(channel);
|
|
|
|
if (iter == g_channel_map->end()) {
|
|
|
|
error_stream.Format("Invalid log channel '{0}'.\n", channel);
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2017-03-01 18:08:40 +08:00
|
|
|
uint32_t flags = categories.empty()
|
|
|
|
? UINT32_MAX
|
|
|
|
: GetFlags(error_stream, *iter, categories);
|
2017-03-09 18:16:07 +08:00
|
|
|
iter->second.Disable(flags);
|
2017-02-17 21:27:42 +08:00
|
|
|
return true;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2017-02-17 21:27:42 +08:00
|
|
|
bool Log::ListChannelCategories(llvm::StringRef channel, Stream &stream) {
|
|
|
|
auto ch = g_channel_map->find(channel);
|
|
|
|
if (ch == g_channel_map->end()) {
|
|
|
|
stream.Format("Invalid log channel '{0}'.\n", channel);
|
|
|
|
return false;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2017-02-17 21:27:42 +08:00
|
|
|
ListCategories(stream, *ch);
|
|
|
|
return true;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Log::DisableAllLogChannels(Stream *feedback_strm) {
|
2017-02-17 21:27:42 +08:00
|
|
|
for (auto &entry : *g_channel_map)
|
2017-03-09 18:16:07 +08:00
|
|
|
entry.second.Disable(UINT32_MAX);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Log::ListAllLogChannels(Stream *strm) {
|
2017-02-27 20:21:16 +08:00
|
|
|
if (g_channel_map->empty()) {
|
2016-09-07 04:57:50 +08:00
|
|
|
strm->PutCString("No logging channels are currently registered.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-17 21:27:42 +08:00
|
|
|
for (const auto &channel : *g_channel_map)
|
|
|
|
ListCategories(*strm, channel);
|
2015-05-27 21:34:04 +08:00
|
|
|
}
|
2017-03-09 18:16:07 +08:00
|
|
|
bool Log::GetVerbose() const {
|
|
|
|
return m_options.load(std::memory_order_relaxed) & LLDB_LOG_OPTION_VERBOSE;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
void Log::WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file,
|
|
|
|
llvm::StringRef function) {
|
2017-03-09 18:16:07 +08:00
|
|
|
Flags options = GetOptions();
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
static uint32_t g_sequence_id = 0;
|
|
|
|
// Add a sequence ID if requested
|
2017-03-09 18:16:07 +08:00
|
|
|
if (options.Test(LLDB_LOG_OPTION_PREPEND_SEQUENCE))
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
OS << ++g_sequence_id << " ";
|
|
|
|
|
|
|
|
// Timestamp if requested
|
2017-03-09 18:16:07 +08:00
|
|
|
if (options.Test(LLDB_LOG_OPTION_PREPEND_TIMESTAMP)) {
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
auto now = std::chrono::duration<double>(
|
|
|
|
std::chrono::system_clock::now().time_since_epoch());
|
|
|
|
OS << llvm::formatv("{0:f9} ", now.count());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the process and thread if requested
|
2017-03-09 18:16:07 +08:00
|
|
|
if (options.Test(LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD))
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
OS << llvm::formatv("[{0,0+4}/{1,0+4}] ", getpid(),
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::get_threadid());
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
|
|
|
|
// Add the thread name if requested
|
2017-03-09 18:16:07 +08:00
|
|
|
if (options.Test(LLDB_LOG_OPTION_PREPEND_THREAD_NAME)) {
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
llvm::SmallString<32> thread_name;
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::get_thread_name(thread_name);
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
if (!thread_name.empty())
|
|
|
|
OS << thread_name;
|
|
|
|
}
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
if (options.Test(LLDB_LOG_OPTION_BACKTRACE))
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
llvm::sys::PrintStackTrace(OS);
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
if (options.Test(LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION) &&
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
(!file.empty() || !function.empty())) {
|
|
|
|
file = llvm::sys::path::filename(file).take_front(40);
|
|
|
|
function = function.take_front(40);
|
|
|
|
OS << llvm::formatv("{0,-60:60} ", (file + ":" + function).str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::WriteMessage(const std::string &message) {
|
|
|
|
// Make a copy of our stream shared pointer in case someone disables our
|
|
|
|
// log while we are logging and releases the stream
|
2017-02-21 17:58:23 +08:00
|
|
|
auto stream_sp = GetStream();
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
if (!stream_sp)
|
|
|
|
return;
|
|
|
|
|
2017-03-09 18:16:07 +08:00
|
|
|
Flags options = GetOptions();
|
|
|
|
if (options.Test(LLDB_LOG_OPTION_THREADSAFE)) {
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
static std::recursive_mutex g_LogThreadedMutex;
|
|
|
|
std::lock_guard<std::recursive_mutex> guard(g_LogThreadedMutex);
|
2017-02-10 19:49:21 +08:00
|
|
|
*stream_sp << message;
|
|
|
|
stream_sp->flush();
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
} else {
|
2017-02-10 19:49:21 +08:00
|
|
|
*stream_sp << message;
|
|
|
|
stream_sp->flush();
|
Add a more succinct logging syntax
This adds the LLDB_LOG macro, which enables one to write more succinct log
statements.
if (log)
log->Printf("log something: %d", var);
becomes
LLDB_LOG(log, "log something: {0}, var);
The macro still internally does the "if(log)" dance, so the arguments are only
evaluated if logging is enabled, meaning it has the same overhead as the
previous syntax.
Additionally, the log statements will be automatically prefixed with the file
and function generating the log (if the corresponding new argument to the "log
enable" command is enabled), so one does not need to manually specify this in
the log statement.
It also uses the new llvm formatv syntax, which means we don't have to worry
about PRIx64 macros and similar, and we can log complex object (llvm::StringRef,
lldb_private::Error, ...) more easily.
Differential Revision: https://reviews.llvm.org/D27459
llvm-svn: 292360
2017-01-18 19:00:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::Format(llvm::StringRef file, llvm::StringRef function,
|
|
|
|
const llvm::formatv_object_base &payload) {
|
|
|
|
std::string message_string;
|
|
|
|
llvm::raw_string_ostream message(message_string);
|
|
|
|
WriteHeader(message, file, function);
|
|
|
|
message << payload << "\n";
|
|
|
|
WriteMessage(message.str());
|
|
|
|
}
|