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"
47 const Log::ChannelMap::value_type &entry,
48 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda) {
49 lambda(
"all",
"all available logging categories");
50 lambda(
"default",
"default set of logging categories");
51 for (
const auto &category : entry.second.m_channel.categories)
52 lambda(category.name, category.description);
56 const ChannelMap::value_type &entry) {
57 stream << llvm::formatv(
"Logging categories for '{0}':\n", entry.first());
59 [&stream](llvm::StringRef name, llvm::StringRef description) {
60 stream << llvm::formatv(
" {0} - {1}\n", name, description);
65 const ChannelMap::value_type &entry,
66 llvm::ArrayRef<const char *> categories) {
67 bool list_categories =
false;
69 for (
const char *category : categories) {
70 if (llvm::StringRef(
"all").equals_insensitive(category)) {
71 flags |= std::numeric_limits<Log::MaskType>::max();
74 if (llvm::StringRef(
"default").equals_insensitive(category)) {
75 flags |= entry.second.m_channel.default_flags;
78 auto cat = llvm::find_if(entry.second.m_channel.categories,
80 return c.name.equals_insensitive(category);
82 if (cat != entry.second.m_channel.categories.end()) {
86 stream << llvm::formatv(
"error: unrecognized log category '{0}'\n",
88 list_categories =
true;
95void Log::Enable(
const std::shared_ptr<LogHandler> &handler_sp,
97 llvm::sys::ScopedWriter lock(
m_mutex);
101 m_options.store(options, std::memory_order_relaxed);
108 llvm::sys::ScopedWriter lock(
m_mutex);
110 MaskType mask =
m_mask.fetch_and(~flags, std::memory_order_relaxed);
111 if (!(mask & ~flags)) {
118 llvm::sys::ScopedReader lock(
m_mutex);
120 llvm::dyn_cast_or_null<RotatingLogHandler>(
m_handler.get())) {
121 handler->Dump(output_stream);
128 return m_options.load(std::memory_order_relaxed);
132 return m_mask.load(std::memory_order_relaxed);
138 std::string FinalMessage;
139 llvm::raw_string_ostream
Stream(FinalMessage);
148 va_start(args, format);
154 llvm::SmallString<64> Content;
160 const char *format, ...) {
162 va_start(args, format);
168 const char *format, va_list args) {
169 llvm::SmallString<64> Content;
171 Format(file, function, llvm::formatv(
"{0}", Content));
177 va_start(args, format);
183 llvm::SmallString<64> Content;
186 Printf(
"error: %s", Content.c_str());
195 va_start(args, format);
202 llvm::SmallString<64> Content;
204 va_start(args, format);
208 Printf(
"warning: %s", Content.c_str());
213 assert(iter.second ==
true);
220 iter->second.Disable(std::numeric_limits<MaskType>::max());
225 uint32_t log_options, llvm::StringRef channel,
226 llvm::ArrayRef<const char *> categories,
227 llvm::raw_ostream &error_stream) {
230 error_stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
234 ? iter->second.m_channel.default_flags
235 :
GetFlags(error_stream, *iter, categories);
236 iter->second.Enable(log_handler_sp, log_options, flags);
241 llvm::ArrayRef<const char *> categories,
242 llvm::raw_ostream &error_stream) {
245 error_stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
249 ? std::numeric_limits<MaskType>::max()
250 :
GetFlags(error_stream, *iter, categories);
251 iter->second.Disable(flags);
256 llvm::raw_ostream &output_stream,
257 llvm::raw_ostream &error_stream) {
260 error_stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
263 if (!iter->second.Dump(output_stream)) {
264 error_stream << llvm::formatv(
265 "log channel '{0}' does not support dumping.\n", channel);
272 llvm::raw_ostream &stream) {
275 stream << llvm::formatv(
"Invalid log channel '{0}'.\n", channel);
284 entry.second.Disable(std::numeric_limits<MaskType>::max());
288 llvm::StringRef channel,
289 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda) {
298 std::vector<llvm::StringRef> result;
300 result.push_back(channel.first());
306 stream <<
"No logging channels are currently registered.\n";
319 llvm::StringRef function) {
321 static uint32_t g_sequence_id = 0;
324 OS << ++g_sequence_id <<
" ";
328 auto now = std::chrono::duration<double>(
329 std::chrono::system_clock::now().time_since_epoch());
330 OS << llvm::formatv(
"{0:f9} ", now.count());
335 OS << llvm::formatv(
"[{0,0+4}/{1,0+4}] ", getpid(),
336 llvm::get_threadid());
340 llvm::SmallString<32> thread_name;
341 llvm::get_thread_name(thread_name);
343 llvm::SmallString<12> format_str;
344 llvm::raw_svector_ostream format_os(format_str);
345 format_os <<
"{0,-" << llvm::alignTo<16>(thread_name.size()) <<
"} ";
346 OS << llvm::formatv(format_str.c_str(), thread_name);
350 llvm::sys::PrintStackTrace(
OS);
353 (!file.empty() || !function.empty())) {
354 file = llvm::sys::path::filename(file).take_front(40);
355 function = function.take_front(40);
356 OS << llvm::formatv(
"{0,-60:60} ", (file +
":" + function).str());
368 handler_sp->Emit(message);
372 const llvm::formatv_object_base &payload) {
373 std::string message_string;
374 llvm::raw_string_ostream message(message_string);
376 message << payload <<
"\n";
382 : m_stream(fd, should_close, buffer_size == 0) {
384 m_stream.SetBufferSize(buffer_size);
390 std::lock_guard<std::mutex> guard(
m_mutex);
396 std::lock_guard<std::mutex> guard(
m_mutex);
405 : m_callback(callback), m_baton(baton) {}
412 : m_messages(std::make_unique<std::string[]>(size)), m_size(size) {}
415 std::lock_guard<std::mutex> guard(
m_mutex);
433 std::lock_guard<std::mutex> guard(
m_mutex);
436 for (
size_t i = start_idx; i < stop_idx; ++i) {
444 std::shared_ptr<LogHandler> second_log_handler)
445 : m_first_log_handler(first_log_handler),
446 m_second_log_handler(second_log_handler) {
#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
llvm::sys::RWMutex m_mutex
static void ListCategories(llvm::raw_ostream &stream, const ChannelMap::value_type &entry)
void Disable(MaskType flags)
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 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)
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.
void Enable(const std::shared_ptr< LogHandler > &handler_sp, uint32_t options, MaskType flags)
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(* LogOutputCallback)(const char *, void *baton)