1 //===-- Log.h ---------------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef liblldb_Log_h_
11 #define liblldb_Log_h_
20 // Other libraries and framework includes
22 #include "lldb/Core/ConstString.h"
23 #include "lldb/Core/Flags.h"
24 #include "lldb/Core/Logging.h"
25 #include "lldb/Core/PluginInterface.h"
26 #include "lldb/lldb-private.h"
28 #include "llvm/Support/FormatVariadic.h"
30 //----------------------------------------------------------------------
32 //----------------------------------------------------------------------
33 #define LLDB_LOG_OPTION_THREADSAFE (1u << 0)
34 #define LLDB_LOG_OPTION_VERBOSE (1u << 1)
35 #define LLDB_LOG_OPTION_DEBUG (1u << 2)
36 #define LLDB_LOG_OPTION_PREPEND_SEQUENCE (1u << 3)
37 #define LLDB_LOG_OPTION_PREPEND_TIMESTAMP (1u << 4)
38 #define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD (1u << 5)
39 #define LLDB_LOG_OPTION_PREPEND_THREAD_NAME (1U << 6)
40 #define LLDB_LOG_OPTION_BACKTRACE (1U << 7)
41 #define LLDB_LOG_OPTION_APPEND (1U << 8)
43 //----------------------------------------------------------------------
45 //----------------------------------------------------------------------
46 namespace lldb_private {
50 //------------------------------------------------------------------
51 // Callback definitions for abstracted plug-in log access.
52 //------------------------------------------------------------------
53 typedef void (*DisableCallback)(const char **categories,
54 Stream *feedback_strm);
55 typedef Log *(*EnableCallback)(lldb::StreamSP &log_stream_sp,
56 uint32_t log_options, const char **categories,
57 Stream *feedback_strm);
58 typedef void (*ListCategoriesCallback)(Stream *strm);
61 DisableCallback disable;
62 EnableCallback enable;
63 ListCategoriesCallback list_categories;
66 //------------------------------------------------------------------
67 // Static accessors for logging channels
68 //------------------------------------------------------------------
69 static void RegisterLogChannel(const ConstString &channel,
70 const Log::Callbacks &log_callbacks);
72 static bool UnregisterLogChannel(const ConstString &channel);
74 static bool GetLogChannelCallbacks(const ConstString &channel,
75 Log::Callbacks &log_callbacks);
77 static bool EnableLogChannel(lldb::StreamSP &log_stream_sp,
78 uint32_t log_options, const char *channel,
79 const char **categories, Stream &error_stream);
81 static void EnableAllLogChannels(lldb::StreamSP &log_stream_sp,
83 const char **categories,
84 Stream *feedback_strm);
86 static void DisableAllLogChannels(Stream *feedback_strm);
88 static void ListAllLogChannels(Stream *strm);
90 static void Initialize();
92 static void Terminate();
94 //------------------------------------------------------------------
96 //------------------------------------------------------------------
97 static void AutoCompleteChannelName(const char *channel_name,
100 //------------------------------------------------------------------
102 //------------------------------------------------------------------
105 Log(const lldb::StreamSP &stream_sp);
109 void PutCString(const char *cstr);
110 void PutString(llvm::StringRef str);
112 template <typename... Args> void Format(const char *fmt, Args &&... args) {
113 PutString(llvm::formatv(fmt, std::forward<Args>(args)...).str());
116 // CLEANUP: Add llvm::raw_ostream &Stream() function.
117 void Printf(const char *format, ...) __attribute__((format(printf, 2, 3)));
119 void VAPrintf(const char *format, va_list args);
121 void LogIf(uint32_t mask, const char *fmt, ...)
122 __attribute__((format(printf, 3, 4)));
124 void Debug(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
126 void Error(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
128 void VAError(const char *format, va_list args);
130 void Verbose(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
132 void Warning(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
136 const Flags &GetOptions() const;
140 const Flags &GetMask() const;
142 bool GetVerbose() const;
144 bool GetDebug() const;
146 void SetStream(const lldb::StreamSP &stream_sp) { m_stream_sp = stream_sp; }
149 //------------------------------------------------------------------
151 //------------------------------------------------------------------
152 lldb::StreamSP m_stream_sp;
157 DISALLOW_COPY_AND_ASSIGN(Log);
160 class LogChannel : public PluginInterface {
164 ~LogChannel() override;
166 static lldb::LogChannelSP FindPlugin(const char *plugin_name);
168 // categories is an array of chars that ends with a NULL element.
169 virtual void Disable(const char **categories, Stream *feedback_strm) = 0;
172 Enable(lldb::StreamSP &log_stream_sp, uint32_t log_options,
173 Stream *feedback_strm, // Feedback stream for argument errors etc
174 const char **categories) = 0; // The categories to enable within this
175 // logging stream, if empty, enable
178 virtual void ListCategories(Stream *strm) = 0;
181 std::unique_ptr<Log> m_log_ap;
184 DISALLOW_COPY_AND_ASSIGN(LogChannel);
187 } // namespace lldb_private
189 #endif // liblldb_Log_h_