1 //===-- Log.h ---------------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_Log_h_
11 #define liblldb_Log_h_
12 
13 // C Includes
14 #include <stdbool.h>
15 #include <stdint.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 
20 // C++ Includes
21 // Other libraries and framework includes
22 // Project includes
23 #include "lldb/lldb-private.h"
24 #include "lldb/Core/ConstString.h"
25 #include "lldb/Core/Flags.h"
26 #include "lldb/Core/PluginInterface.h"
27 
28 //----------------------------------------------------------------------
29 // Logging types
30 //----------------------------------------------------------------------
31 #define LLDB_LOG_FLAG_STDOUT    (1u << 0)
32 #define LLDB_LOG_FLAG_STDERR    (1u << 1)
33 #define LLDB_LOG_FLAG_FATAL     (1u << 2)
34 #define LLDB_LOG_FLAG_ERROR     (1u << 3)
35 #define LLDB_LOG_FLAG_WARNING   (1u << 4)
36 #define LLDB_LOG_FLAG_DEBUG     (1u << 5)
37 #define LLDB_LOG_FLAG_VERBOSE   (1u << 6)
38 
39 //----------------------------------------------------------------------
40 // Logging Options
41 //----------------------------------------------------------------------
42 #define LLDB_LOG_OPTION_THREADSAFE              (1u << 0)
43 #define LLDB_LOG_OPTION_VERBOSE                 (1u << 1)
44 #define LLDB_LOG_OPTION_DEBUG                   (1u << 2)
45 #define LLDB_LOG_OPTION_PREPEND_SEQUENCE        (1u << 3)
46 #define LLDB_LOG_OPTION_PREPEND_TIMESTAMP       (1u << 4)
47 #define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD (1u << 5)
48 #define LLDB_LOG_OPTION_PREPEND_THREAD_NAME     (1U << 6)
49 #define LLDB_LOG_OPTION_BACKTRACE               (1U << 7)
50 
51 //----------------------------------------------------------------------
52 // Logging Functions
53 //----------------------------------------------------------------------
54 namespace lldb_private {
55 
56 class Log
57 {
58 public:
59 
60     //------------------------------------------------------------------
61     // Callback definitions for abstracted plug-in log access.
62     //------------------------------------------------------------------
63     typedef void (*DisableCallback) (const char **categories, Stream *feedback_strm);
64     typedef Log * (*EnableCallback) (lldb::StreamSP &log_stream_sp,
65                                      uint32_t log_options,
66                                      const char **categories,
67                                      Stream *feedback_strm);
68     typedef void (*ListCategoriesCallback) (Stream *strm);
69 
70     struct Callbacks
71     {
72         DisableCallback disable;
73         EnableCallback enable;
74         ListCategoriesCallback list_categories;
75     };
76 
77     //------------------------------------------------------------------
78     // Static accessors for logging channels
79     //------------------------------------------------------------------
80     static void
81     RegisterLogChannel (const ConstString &channel,
82                         const Log::Callbacks &log_callbacks);
83 
84     static bool
85     UnregisterLogChannel (const ConstString &channel);
86 
87     static bool
88     GetLogChannelCallbacks (const ConstString &channel,
89                             Log::Callbacks &log_callbacks);
90 
91 
92     static void
93     EnableAllLogChannels (lldb::StreamSP &log_stream_sp,
94                           uint32_t log_options,
95                           const char **categories,
96                           Stream *feedback_strm);
97 
98     static void
99     DisableAllLogChannels (Stream *feedback_strm);
100 
101     static void
102     ListAllLogChannels (Stream *strm);
103 
104     static void
105     Initialize ();
106 
107     static void
108     Terminate ();
109 
110     //------------------------------------------------------------------
111     // Auto completion
112     //------------------------------------------------------------------
113     static void
114     AutoCompleteChannelName (const char *channel_name,
115                              StringList &matches);
116 
117     //------------------------------------------------------------------
118     // Member functions
119     //------------------------------------------------------------------
120     Log ();
121 
122     Log (const lldb::StreamSP &stream_sp);
123 
124     ~Log ();
125 
126     void
127     PutCString (const char *cstr);
128 
129     void
130     Printf (const char *format, ...)  __attribute__ ((format (printf, 2, 3)));
131 
132     void
133     VAPrintf (const char *format, va_list args);
134 
135     void
136     PrintfWithFlags( uint32_t flags, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));
137 
138     void
139     LogIf (uint32_t mask, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
140 
141     void
142     Debug (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
143 
144     void
145     DebugVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
146 
147     void
148     Error (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
149 
150     void
151     FatalError (int err, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
152 
153     void
154     Verbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
155 
156     void
157     Warning (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
158 
159     void
160     WarningVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
161 
162     Flags &
163     GetOptions();
164 
165     const Flags &
166     GetOptions() const;
167 
168     Flags &
169     GetMask();
170 
171     const Flags &
172     GetMask() const;
173 
174     bool
175     GetVerbose() const;
176 
177     bool
178     GetDebug() const;
179 
180     void
SetStream(const lldb::StreamSP & stream_sp)181     SetStream (const lldb::StreamSP &stream_sp)
182     {
183         m_stream_sp = stream_sp;
184     }
185 
186 protected:
187     //------------------------------------------------------------------
188     // Member variables
189     //------------------------------------------------------------------
190     lldb::StreamSP m_stream_sp;
191     Flags m_options;
192     Flags m_mask_bits;
193 
194     void
195     PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args);
196 
197 private:
198     DISALLOW_COPY_AND_ASSIGN (Log);
199 };
200 
201 
202 class LogChannel : public PluginInterface
203 {
204 public:
205     LogChannel ();
206 
207     virtual
208     ~LogChannel ();
209 
210     static lldb::LogChannelSP
211     FindPlugin (const char *plugin_name);
212 
213     // categories is a an array of chars that ends with a NULL element.
214     virtual void
215     Disable (const char **categories, Stream *feedback_strm) = 0;
216 
217     virtual bool
218     Enable (lldb::StreamSP &log_stream_sp,
219             uint32_t log_options,
220             Stream *feedback_strm,      // Feedback stream for argument errors etc
221             const char **categories) = 0;// The categories to enable within this logging stream, if empty, enable default set
222 
223     virtual void
224     ListCategories (Stream *strm) = 0;
225 
226 protected:
227     std::unique_ptr<Log> m_log_ap;
228 
229 private:
230     DISALLOW_COPY_AND_ASSIGN (LogChannel);
231 };
232 
233 
234 } // namespace lldb_private
235 
236 #endif  // liblldb_Log_H_
237