2010-06-09 00:52:24 +08:00
|
|
|
//===-- Log.cpp -------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-06-09 00:52:24 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
|
|
|
#include "lldb/Utility/VASPrintf.h"
|
2014-09-10 04:54:56 +08:00
|
|
|
|
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"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
|
|
|
#include "llvm/ADT/iterator.h"
|
2017-04-07 02:12:24 +08:00
|
|
|
|
2016-11-02 20:18:42 +08:00
|
|
|
#include "llvm/Support/Chrono.h"
|
2018-11-12 07:16:43 +08:00
|
|
|
#include "llvm/Support/ManagedStatic.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"
|
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <chrono>
|
2016-11-02 20:18:42 +08:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <mutex>
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <utility>
|
2017-04-07 02:12:24 +08:00
|
|
|
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <assert.h>
|
2018-04-10 21:33:45 +08:00
|
|
|
#if defined(_WIN32)
|
2018-11-12 07:16:43 +08:00
|
|
|
#include <process.h>
|
2017-04-07 02:12:24 +08:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2017-10-24 03:41:17 +08:00
|
|
|
#include <pthread.h>
|
2017-04-07 02:12:24 +08:00
|
|
|
#endif
|
2016-11-02 20:18:42 +08:00
|
|
|
|
2010-06-09 00:52:24 +08:00
|
|
|
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-15 17:06:58 +08:00
|
|
|
void Log::ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry) {
|
|
|
|
stream << llvm::formatv("Logging categories for '{0}':\n", entry.first());
|
|
|
|
stream << " all - all available logging categories\n";
|
|
|
|
stream << " 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-03-15 17:06:58 +08:00
|
|
|
stream << llvm::formatv(" {0} - {1}\n", category.name,
|
|
|
|
category.description);
|
2017-02-17 21:27:42 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 17:06:58 +08:00
|
|
|
uint32_t Log::GetFlags(llvm::raw_ostream &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-15 17:06:58 +08:00
|
|
|
stream << llvm::formatv("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
|
|
|
|
2015-04-30 06:55:28 +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.
|
|
|
|
void Log::Printf(const char *format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
VAPrintf(format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2018-05-01 00:49:04 +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.
|
2015-04-30 06:55:28 +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.
|
2015-04-30 06:55:28 +08:00
|
|
|
void Log::Error(const char *format, ...) {
|
2010-06-09 00:52:24 +08:00
|
|
|
va_list args;
|
2015-04-30 06:55:28 +08:00
|
|
|
va_start(args, format);
|
2015-05-08 05:39:33 +08:00
|
|
|
VAError(format, args);
|
2015-04-30 06:55:28 +08:00
|
|
|
va_end(args);
|
2015-05-08 05:39:33 +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
|
|
|
}
|
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Printing of warnings that are not fatal only if verbose mode is enabled.
|
2015-04-30 06:55:28 +08:00
|
|
|
void Log::Verbose(const char *format, ...) {
|
2017-03-09 18:16:07 +08:00
|
|
|
if (!GetVerbose())
|
2015-04-30 06:55:28 +08:00
|
|
|
return;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-04-30 06:55:28 +08:00
|
|
|
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.
|
2015-04-30 06:55:28 +08:00
|
|
|
void Log::Warning(const char *format, ...) {
|
2017-03-04 04:56:28 +08:00
|
|
|
llvm::SmallString<64> Content;
|
2010-06-09 00:52:24 +08:00
|
|
|
va_list args;
|
2015-04-30 06:55:28 +08:00
|
|
|
va_start(args, format);
|
2017-03-04 04:56:28 +08:00
|
|
|
VASprintf(Content, format, args);
|
2015-04-30 06:55:28 +08:00
|
|
|
va_end(args);
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-03-04 04:56:28 +08:00
|
|
|
Printf("warning: %s", Content.c_str());
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
|
2017-10-24 03:41:17 +08:00
|
|
|
void Log::Initialize() {
|
|
|
|
#ifdef LLVM_ON_UNIX
|
|
|
|
pthread_atfork(nullptr, nullptr, &Log::DisableLoggingChild);
|
|
|
|
#endif
|
|
|
|
InitializeLldbChannel();
|
|
|
|
}
|
|
|
|
|
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);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
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,
|
2017-03-15 17:06:58 +08:00
|
|
|
llvm::ArrayRef<const char *> categories, llvm::raw_ostream &error_stream) {
|
2017-02-17 21:27:42 +08:00
|
|
|
auto iter = g_channel_map->find(channel);
|
|
|
|
if (iter == g_channel_map->end()) {
|
2017-03-15 17:06:58 +08:00
|
|
|
error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
|
2010-06-09 00:52:24 +08:00
|
|
|
return false;
|
|
|
|
}
|
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-03-15 17:06:58 +08:00
|
|
|
llvm::raw_ostream &error_stream) {
|
2017-02-17 21:27:42 +08:00
|
|
|
auto iter = g_channel_map->find(channel);
|
|
|
|
if (iter == g_channel_map->end()) {
|
2017-03-15 17:06:58 +08:00
|
|
|
error_stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
|
2017-02-17 21:27:42 +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()
|
|
|
|
? 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-03-15 17:06:58 +08:00
|
|
|
bool Log::ListChannelCategories(llvm::StringRef channel,
|
|
|
|
llvm::raw_ostream &stream) {
|
2017-02-17 21:27:42 +08:00
|
|
|
auto ch = g_channel_map->find(channel);
|
|
|
|
if (ch == g_channel_map->end()) {
|
2017-03-15 17:06:58 +08:00
|
|
|
stream << llvm::formatv("Invalid log channel '{0}'.\n", channel);
|
2017-02-17 21:27:42 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-03-15 17:06:58 +08:00
|
|
|
void Log::DisableAllLogChannels() {
|
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
|
|
|
}
|
|
|
|
|
2017-03-15 17:06:58 +08:00
|
|
|
void Log::ListAllLogChannels(llvm::raw_ostream &stream) {
|
2017-02-27 20:21:16 +08:00
|
|
|
if (g_channel_map->empty()) {
|
2017-03-15 17:06:58 +08:00
|
|
|
stream << "No logging channels are currently registered.\n";
|
2016-09-07 04:57:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-17 21:27:42 +08:00
|
|
|
for (const auto &channel : *g_channel_map)
|
2017-03-15 17:06:58 +08:00
|
|
|
ListCategories(stream, channel);
|
2015-05-27 21:34:04 +08:00
|
|
|
}
|
2017-03-15 17:06:58 +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);
|
2018-07-13 19:49:28 +08:00
|
|
|
|
|
|
|
llvm::SmallString<12> format_str;
|
|
|
|
llvm::raw_svector_ostream format_os(format_str);
|
|
|
|
format_os << "{0,-" << llvm::alignTo<16>(thread_name.size()) << "} ";
|
|
|
|
OS << llvm::formatv(format_str.c_str(), 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
|
|
|
}
|
|
|
|
|
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) {
|
2018-05-01 00:49:04 +08:00
|
|
|
// 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());
|
|
|
|
}
|
2017-10-24 03:41:17 +08:00
|
|
|
|
|
|
|
void Log::DisableLoggingChild() {
|
|
|
|
// Disable logging by clearing out the atomic variable after forking -- if we
|
|
|
|
// forked while another thread held the channel mutex, we would deadlock when
|
|
|
|
// trying to write to the log.
|
|
|
|
for (auto &c: *g_channel_map)
|
|
|
|
c.second.m_channel.log_ptr.store(nullptr, std::memory_order_relaxed);
|
|
|
|
}
|