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);
288 void WriteHeader(llvm::raw_ostream &
OS, llvm::StringRef file,
289 llvm::StringRef function);
292 void Format(llvm::StringRef file, llvm::StringRef function,
293 const llvm::formatv_object_base &payload);
296 llvm::sys::ScopedReader lock(
m_mutex);
300 void Enable(
const std::shared_ptr<LogHandler> &handler_sp, uint32_t options,
305 bool Dump(llvm::raw_ostream &stream);
311 const Log::ChannelMap::value_type &entry,
312 llvm::function_ref<
void(llvm::StringRef, llvm::StringRef)> lambda);
315 const ChannelMap::value_type &entry);
317 const ChannelMap::value_type &entry,
318 llvm::ArrayRef<const char *> categories);
333 std::is_same<Log::MaskType, std::underlying_type_t<Cat>>::value);
359#define LLDB_LOG(log, ...) \
361 ::lldb_private::Log *log_private = (log); \
363 log_private->Format(__FILE__, __func__, __VA_ARGS__); \
366#define LLDB_LOGF(log, ...) \
368 ::lldb_private::Log *log_private = (log); \
370 log_private->Formatf(__FILE__, __func__, __VA_ARGS__); \
373#define LLDB_LOGV(log, ...) \
375 ::lldb_private::Log *log_private = (log); \
376 if (log_private && log_private->GetVerbose()) \
377 log_private->Format(__FILE__, __func__, __VA_ARGS__); \
382#define LLDB_LOG_ERROR(log, error, ...) \
384 ::lldb_private::Log *log_private = (log); \
385 ::llvm::Error error_private = (error); \
386 if (log_private && error_private) { \
387 log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
390 ::llvm::consumeError(::std::move(error_private)); \
396#define LLDB_LOG_ERRORV(log, error, ...) \
398 ::lldb_private::Log *log_private = (log); \
399 ::llvm::Error error_private = (error); \
400 if (log_private && log_private->GetVerbose() && error_private) { \
401 log_private->FormatError(::std::move(error_private), __FILE__, __func__, \
404 ::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)
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)
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 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 operator=(const Log &)=delete
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
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(* LogOutputCallback)(const char *, void *baton)
llvm::StringLiteral description
constexpr Category(llvm::StringLiteral name, llvm::StringLiteral description, Cat mask)