12#include "llvm/ADT/SmallString.h"
13#include "llvm/ADT/Twine.h"
14#include "llvm/ADT/iterator.h"
16#include "llvm/Support/Casting.h"
17#include "llvm/Support/Chrono.h"
18#include "llvm/Support/ManagedStatic.h"
19#include "llvm/Support/Path.h"
20#include "llvm/Support/Signals.h"
21#include "llvm/Support/Threading.h"
22#include "llvm/Support/raw_ostream.h"
51 const Log::ChannelMap::value_type &entry,
52 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda) {
53 lambda(
"all",
"all available logging categories");
54 lambda(
"default",
"default set of logging categories");
55 for (
const auto &category : entry.second.m_channel.categories)
56 lambda(category.name, category.description);
60 const ChannelMap::value_type &entry) {
61 stream << llvm::formatv(
"Logging categories for '{0}':\n", entry.first());
63 [&stream](llvm::StringRef name, llvm::StringRef description) {
64 stream << llvm::formatv(
" {0} - {1}\n", name, description);
69 const ChannelMap::value_type &entry,
70 llvm::ArrayRef<const char *> categories) {
71 bool list_categories =
false;
73 for (
const char *category : categories) {
74 if (llvm::StringRef(
"all").equals_insensitive(category)) {
75 flags |= std::numeric_limits<Log::MaskType>::max();
78 if (llvm::StringRef(
"default").equals_insensitive(category)) {
79 flags |= entry.second.m_channel.default_flags;
82 auto cat = llvm::find_if(entry.second.m_channel.categories,
84 return c.name.equals_insensitive(category);
86 if (cat != entry.second.m_channel.categories.end()) {
90 stream << llvm::formatv(
"error: unrecognized log category '{0}'\n",
92 list_categories =
true;
99void Log::Enable(
const std::shared_ptr<LogHandler> &handler_sp,
100 std::optional<Log::MaskType> flags, uint32_t options) {
101 llvm::sys::ScopedWriter lock(
m_mutex);
106 MaskType mask =
m_mask.fetch_or(*flags, std::memory_order_relaxed);
108 m_options.store(options, std::memory_order_relaxed);
115 llvm::sys::ScopedWriter lock(
m_mutex);
118 flags = std::numeric_limits<MaskType>::max();
120 MaskType mask =
m_mask.fetch_and(~(*flags), std::memory_order_relaxed);
121 if (!(mask & ~(*flags))) {
128 llvm::sys::ScopedReader lock(
m_mutex);
130 llvm::dyn_cast_or_null<RotatingLogHandler>(
m_handler.get())) {
131 handler->Dump(output_stream);
138 return m_options.load(std::memory_order_relaxed);
142 return m_mask.load(std::memory_order_relaxed);
148 std::string FinalMessage;
149 llvm::raw_string_ostream
Stream(FinalMessage);
158 va_start(args, format);
164 llvm::SmallString<64> Content;
170 const char *format, ...) {
172 va_start(args, format);
178 const char *format, va_list args) {
179 llvm::SmallString<64> Content;
181 Format(file, function, llvm::formatv(
"{0}", Content));
187 va_start(args, format);
193 llvm::SmallString<64> Content;
196 Printf(
"error: %s", Content.c_str());
205 va_start(args, format);
212 llvm::SmallString<64> Content;
214 va_start(args, format);
218 Printf(
"warning: %s", Content.c_str());
223 assert(iter.second ==
true);
230 iter->second.Disable(std::numeric_limits<MaskType>::max());
235 uint32_t log_options, llvm::StringRef channel,
236 llvm::ArrayRef<const char *> categories,
237 llvm::raw_ostream &error_stream) {
240 error_stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
244 auto flags = categories.empty() ? std::optional<MaskType>{}
245 :
GetFlags(error_stream, *iter, categories);
247 iter->second.Enable(log_handler_sp, flags, log_options);
252 llvm::ArrayRef<const char *> categories,
253 llvm::raw_ostream &error_stream) {
256 error_stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
260 auto flags = categories.empty() ? std::optional<MaskType>{}
261 :
GetFlags(error_stream, *iter, categories);
263 iter->second.Disable(flags);
268 llvm::raw_ostream &output_stream,
269 llvm::raw_ostream &error_stream) {
272 error_stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
275 if (!iter->second.Dump(output_stream)) {
276 error_stream << llvm::formatv(
277 "log channel '{0}' does not support dumping.\n", channel);
284 llvm::raw_ostream &stream) {
287 stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
296 entry.second.Disable(std::numeric_limits<MaskType>::max());
300 llvm::StringRef channel,
301 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda) {
310 std::vector<llvm::StringRef> result;
312 result.push_back(channel.first());
318 stream <<
"No logging channels are currently registered.\n";
331 llvm::StringRef function) {
333 static uint32_t g_sequence_id = 0;
336 OS << ++g_sequence_id <<
" ";
340 auto now = std::chrono::duration<double>(
341 std::chrono::system_clock::now().time_since_epoch());
342 OS << llvm::formatv(
"{0:f9} ", now.count());
347 OS << llvm::formatv(
"[{0,0+4}/{1,0+4}] ", getpid(),
348 llvm::get_threadid());
352 llvm::SmallString<32> thread_name;
353 llvm::get_thread_name(thread_name);
355 llvm::SmallString<12> format_str;
356 llvm::raw_svector_ostream format_os(format_str);
357 format_os <<
"{0,-" << llvm::alignTo<16>(thread_name.size()) <<
"} ";
358 OS << llvm::formatv(format_str.c_str(), thread_name);
362 llvm::sys::PrintStackTrace(
OS);
365 (!file.empty() || !function.empty())) {
366 file = llvm::sys::path::filename(file).take_front(40);
367 function = function.take_front(40);
368 OS << llvm::formatv(
"{0,-60:60} ", (file +
":" + function).str());
380 handler_sp->Emit(message);
384 const llvm::formatv_object_base &payload) {
385 std::string message_string;
386 llvm::raw_string_ostream message(message_string);
388 message << payload <<
"\n";
394 : m_stream(fd, should_close, buffer_size == 0) {
396 m_stream.SetBufferSize(buffer_size);
402 std::lock_guard<std::mutex> guard(
m_mutex);
408 std::lock_guard<std::mutex> guard(
m_mutex);
417 : m_callback(callback), m_baton(baton) {}
424 : m_messages(std::make_unique<std::string[]>(size)), m_size(size) {}
427 std::lock_guard<std::mutex> guard(
m_mutex);
445 std::lock_guard<std::mutex> guard(
m_mutex);
448 for (
size_t i = start_idx; i < stop_idx; ++i) {
456 std::shared_ptr<LogHandler> second_log_handler)
457 : m_first_log_handler(first_log_handler),
458 m_second_log_handler(second_log_handler) {
static std::atomic< Log * > g_error_log
#define LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION
#define LLDB_LOG_OPTION_BACKTRACE
#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP
#define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD
#define LLDB_LOG_OPTION_PREPEND_SEQUENCE
#define LLDB_LOG_OPTION_VERBOSE
#define LLDB_LOG_OPTION_PREPEND_THREAD_NAME
lldb::LogOutputCallback m_callback
CallbackLogHandler(lldb::LogOutputCallback callback, void *baton)
void Emit(llvm::StringRef message) override
bool Test(ValueType bit) const
Test a single flag bit.
std::atomic< Log * > log_ptr
const MaskType default_flags
llvm::sys::RWMutex m_mutex
static void ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry)
std::shared_ptr< LogHandler > m_handler
void WriteMessage(llvm::StringRef message)
void PutCString(const char *cstr)
static void ForEachCategory(const Log::ChannelMap::value_type &entry, llvm::function_ref< void(llvm::StringRef, llvm::StringRef)> lambda)
void Formatf(llvm::StringRef file, llvm::StringRef function, const char *format,...) __attribute__((format(printf
uint64_t MaskType
The underlying type of all log channel enums.
static bool DisableLogChannel(llvm::StringRef channel, llvm::ArrayRef< const char * > categories, llvm::raw_ostream &error_stream)
static void Register(llvm::StringRef name, Channel &channel)
static void ListAllLogChannels(llvm::raw_ostream &stream)
void VAFormatf(llvm::StringRef file, llvm::StringRef function, const char *format, va_list args)
void WriteHeader(llvm::raw_ostream &OS, llvm::StringRef file, llvm::StringRef function)
static bool DumpLogChannel(llvm::StringRef channel, llvm::raw_ostream &output_stream, llvm::raw_ostream &error_stream)
void VAPrintf(const char *format, va_list args)
void void void Error(const char *fmt,...) __attribute__((format(printf
void Disable(std::optional< MaskType > flags=std::nullopt)
void Format(llvm::StringRef file, llvm::StringRef function, const char *format, Args &&... args)
static llvm::ManagedStatic< ChannelMap > g_channel_map
std::shared_ptr< LogHandler > GetHandler()
void void void void void Warning(const char *fmt,...) __attribute__((format(printf
std::atomic< MaskType > m_mask
static void Unregister(llvm::StringRef name)
void Enable(const std::shared_ptr< LogHandler > &handler_sp, std::optional< MaskType > flags=std::nullopt, uint32_t options=0)
static bool ListChannelCategories(llvm::StringRef channel, llvm::raw_ostream &stream)
static void DisableAllLogChannels()
static void ForEachChannelCategory(llvm::StringRef channel, llvm::function_ref< void(llvm::StringRef, llvm::StringRef)> lambda)
Calls the given lambda for every category in the given channel.
void void void void void const Flags GetOptions() const
static bool EnableLogChannel(const std::shared_ptr< LogHandler > &log_handler_sp, uint32_t log_options, llvm::StringRef channel, llvm::ArrayRef< const char * > categories, llvm::raw_ostream &error_stream)
void VAError(const char *format, va_list args)
static std::vector< llvm::StringRef > ListChannels()
Returns the list of log channels.
static Log::MaskType GetFlags(llvm::raw_ostream &stream, const ChannelMap::value_type &entry, llvm::ArrayRef< const char * > categories)
void PutString(llvm::StringRef str)
bool Dump(llvm::raw_ostream &stream)
void void Printf(const char *format,...) __attribute__((format(printf
Prefer using LLDB_LOGF whenever possible.
void void void void Verbose(const char *fmt,...) __attribute__((format(printf
std::atomic< uint32_t > m_options
void Dump(llvm::raw_ostream &stream) const
std::unique_ptr< std::string[]> m_messages
void Emit(llvm::StringRef message) override
size_t NormalizeIndex(size_t i) const
size_t GetNumMessages() const
RotatingLogHandler(size_t size)
size_t GetFirstMessageIndex() const
~StreamLogHandler() override
llvm::raw_fd_ostream m_stream
StreamLogHandler(int fd, bool should_close, size_t buffer_size=0)
void Emit(llvm::StringRef message) override
A stream class that can stream formatted output to a file.
void Emit(llvm::StringRef message) override
std::shared_ptr< LogHandler > m_first_log_handler
TeeLogHandler(std::shared_ptr< LogHandler > first_log_handler, std::shared_ptr< LogHandler > second_log_handler)
std::shared_ptr< LogHandler > m_second_log_handler
#define UNUSED_IF_ASSERT_DISABLED(x)
A class that represents a running process on the host machine.
bool VASprintf(llvm::SmallVectorImpl< char > &buf, const char *fmt, va_list args)
void SetLLDBErrorLog(Log *log)
Getter and setter for the error log (see g_error_log).
void(* LogOutputCallback)(const char *, void *baton)