9#ifndef LLDB_UTILITY_LOG_H
10#define LLDB_UTILITY_LOG_H
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/StringMap.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/FormatVariadic.h"
21#include "llvm/Support/ManagedStatic.h"
22#include "llvm/Support/RWMutex.h"
36#define LLDB_LOG_OPTION_VERBOSE (1u << 1)
37#define LLDB_LOG_OPTION_PREPEND_SEQUENCE (1u << 3)
38#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP (1u << 4)
39#define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD (1u << 5)
40#define LLDB_LOG_OPTION_PREPEND_THREAD_NAME (1U << 6)
41#define LLDB_LOG_OPTION_BACKTRACE (1U << 7)
42#define LLDB_LOG_OPTION_APPEND (1U << 8)
43#define LLDB_LOG_OPTION_PREPEND_FILE_FUNCTION (1U << 9)
51 virtual void Emit(llvm::StringRef message) = 0;
53 virtual bool isA(
const void *ClassID)
const {
return ClassID == &
ID; }
65 void Emit(llvm::StringRef message)
override;
68 bool isA(
const void *ClassID)
const override {
return ClassID == &
ID; }
81 void Emit(llvm::StringRef message)
override;
83 bool isA(
const void *ClassID)
const override {
return ClassID == &
ID; }
96 void Emit(llvm::StringRef message)
override;
97 void Dump(llvm::raw_ostream &stream)
const;
99 bool isA(
const void *ClassID)
const override {
return ClassID == &
ID; }
119 std::shared_ptr<LogHandler> second_log_handler);
121 void Emit(llvm::StringRef message)
override;
123 bool isA(
const void *ClassID)
const override {
return ClassID == &
ID; }
143 template <MaskType Bit>
152 template <
typename Cat>
157 std::is_same<Log::MaskType, std::underlying_type_t<Cat>>::value);
171 template <
typename Cat>
177 std::is_same<Log::MaskType, std::underlying_type_t<Cat>>::value);
185 Log *log =
log_ptr.load(std::memory_order_relaxed);
186 if (log && ((log->
GetMask() & mask) != 0))
194 static void Register(llvm::StringRef name, Channel &channel);
199 uint32_t log_options, llvm::StringRef channel,
200 llvm::ArrayRef<const char *> categories,
201 llvm::raw_ostream &error_stream);
204 llvm::ArrayRef<const char *> categories,
205 llvm::raw_ostream &error_stream);
208 llvm::raw_ostream &output_stream,
209 llvm::raw_ostream &error_stream);
212 llvm::raw_ostream &stream);
219 llvm::StringRef channel,
220 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda);
237 template <
typename...
Args>
238 void Format(llvm::StringRef file, llvm::StringRef function,
239 const char *format,
Args &&... args) {
240 Format(file, function, llvm::formatv(format, std::forward<Args>(args)...));
243 template <
typename...
Args>
245 llvm::StringRef function,
const char *format,
248 llvm::formatv(format, llvm::toString(std::move(
error)),
249 std::forward<Args>(args)...));
252 void Formatf(llvm::StringRef file, llvm::StringRef function,
253 const char *format, ...) __attribute__((format(printf, 4, 5)));
256 void Printf(const
char *format, ...) __attribute__((format(printf, 2, 3)));
258 void Error(const
char *fmt, ...) __attribute__((format(printf, 2, 3)));
260 void Verbose(const
char *fmt, ...) __attribute__((format(printf, 2, 3)));
262 void Warning(const
char *fmt, ...) __attribute__((format(printf, 2, 3)));
270 void VAPrintf(const
char *format, va_list args);
271 void VAError(const
char *format, va_list args);
273 const
char *format, va_list args);
276 std::optional<
MaskType> flags = std::nullopt,
277 uint32_t options = 0);
294 void WriteHeader(llvm::raw_ostream &
OS, llvm::StringRef file,
295 llvm::StringRef function);
298 void Format(llvm::StringRef file, llvm::StringRef function,
299 const llvm::formatv_object_base &payload);
302 llvm::sys::ScopedReader lock(
m_mutex);
306 bool Dump(llvm::raw_ostream &stream);
312 const Log::ChannelMap::value_type &entry,
313 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda);
316 const ChannelMap::value_type &entry);
318 const ChannelMap::value_type &entry,
319 llvm::ArrayRef<const char *> categories);
334 std::is_same<Log::MaskType, std::underlying_type_t<Cat>>::value);
369#define LLDB_LOG(log, ...) \
371 ::lldb_private::Log *log_private = (log); \
373 log_private->Format(__FILE__, __func__, __VA_ARGS__); \
376#define LLDB_LOGF(log, ...) \
378 ::lldb_private::Log *log_private = (log); \
380 log_private->Formatf(__FILE__, __func__, __VA_ARGS__); \
383#define LLDB_LOGV(log, ...) \
385 ::lldb_private::Log *log_private = (log); \
386 if (log_private && log_private->GetVerbose()) \
387 log_private->Format(__FILE__, __func__, __VA_ARGS__); \
392#define LLDB_LOG_ERROR(log, error, ...) \
394 ::lldb_private::Log *log_private = (log); \
395 ::llvm::Error error_private = (error); \
397 log_private = lldb_private::GetLLDBErrorLog(); \
398 if (log_private && error_private) { \
399 log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
402 ::llvm::consumeError(::std::move(error_private)); \
408#define LLDB_LOG_ERRORV(log, error, ...) \
410 ::lldb_private::Log *log_private = (log); \
411 ::llvm::Error error_private = (error); \
412 if (log_private && log_private->GetVerbose() && error_private) { \
413 log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
416 ::llvm::consumeError(::std::move(error_private)); \
static llvm::raw_ostream & error(Stream &strm)
A command line argument class.
lldb::LogOutputCallback m_callback
static bool classof(const LogHandler *obj)
bool isA(const void *ClassID) const override
void Emit(llvm::StringRef message) override
virtual void Emit(llvm::StringRef message)=0
virtual bool isA(const void *ClassID) const
static bool classof(const LogHandler *obj)
virtual ~LogHandler()=default
constexpr Channel(llvm::ArrayRef< Log::Category > categories, Cat default_flags)
Log * GetLog(MaskType mask)
const llvm::ArrayRef< Category > categories
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)
static constexpr MaskType ChannelFlag
llvm::StringMap< Log > ChannelMap
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 FormatError(llvm::Error error, llvm::StringRef file, llvm::StringRef function, const char *format, Args &&... args)
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 operator=(const Log &)=delete
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
bool isA(const void *ClassID) const override
void Emit(llvm::StringRef message) override
static bool classof(const LogHandler *obj)
size_t NormalizeIndex(size_t i) const
size_t GetNumMessages() const
size_t GetFirstMessageIndex() const
~StreamLogHandler() override
llvm::raw_fd_ostream m_stream
void Emit(llvm::StringRef message) override
bool isA(const void *ClassID) const override
static bool classof(const LogHandler *obj)
A T-style log handler that multiplexes messages to two log handlers.
void Emit(llvm::StringRef message) override
std::shared_ptr< LogHandler > m_first_log_handler
bool isA(const void *ClassID) const override
static bool classof(const LogHandler *obj)
std::shared_ptr< LogHandler > m_second_log_handler
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Log::Channel & LogChannelFor()=delete
void SetLLDBErrorLog(Log *log)
Getter and setter for the error log (see g_error_log).
void(* LogOutputCallback)(const char *, void *baton)
llvm::StringLiteral description
constexpr Category(llvm::StringLiteral name, llvm::StringLiteral description, Cat mask)