Log.h revision 6c530f2201be4788dedf3d5970399220fbd50b11
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
50//----------------------------------------------------------------------
51// Logging Functions
52//----------------------------------------------------------------------
53namespace lldb_private {
54
55class Log
56{
57public:
58
59    //------------------------------------------------------------------
60    // Callback definitions for abstracted plug-in log access.
61    //------------------------------------------------------------------
62    typedef void (*DisableCallback) (const char **categories, Stream *feedback_strm);
63    typedef lldb::LogSP (*EnableCallback) (lldb::StreamSP &log_stream_sp,
64                                           uint32_t log_options,
65                                           const char **categories,
66                                           Stream *feedback_strm);
67    typedef void (*ListCategoriesCallback) (Stream *strm);
68
69    struct Callbacks
70    {
71        DisableCallback disable;
72        EnableCallback enable;
73        ListCategoriesCallback list_categories;
74    };
75
76    //------------------------------------------------------------------
77    // Static accessors for logging channels
78    //------------------------------------------------------------------
79    static void
80    RegisterLogChannel (const char *channel,
81                        const Log::Callbacks &log_callbacks);
82
83    static bool
84    UnregisterLogChannel (const char *channel);
85
86    static bool
87    GetLogChannelCallbacks (const char *channel,
88                            Log::Callbacks &log_callbacks);
89
90
91    static void
92    EnableAllLogChannels (lldb::StreamSP &log_stream_sp,
93                          uint32_t log_options,
94                          const char **categories,
95                          Stream *feedback_strm);
96
97    static void
98    DisableAllLogChannels (Stream *feedback_strm);
99
100    static void
101    ListAllLogChannels (Stream *strm);
102
103    static void
104    Initialize ();
105
106    static void
107    Terminate ();
108
109    //------------------------------------------------------------------
110    // Auto completion
111    //------------------------------------------------------------------
112    static void
113    AutoCompleteChannelName (const char *channel_name,
114                             StringList &matches);
115
116    //------------------------------------------------------------------
117    // Member functions
118    //------------------------------------------------------------------
119    Log ();
120
121    Log (lldb::StreamSP &stream_sp);
122
123    ~Log ();
124
125    void
126    PutCString (const char *cstr);
127
128    void
129    Printf (const char *format, ...)  __attribute__ ((format (printf, 2, 3)));
130
131    void
132    VAPrintf (const char *format, va_list args);
133
134    void
135    PrintfWithFlags( uint32_t flags, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));
136
137    void
138    LogIf (uint32_t mask, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
139
140    void
141    Debug (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
142
143    void
144    DebugVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
145
146    void
147    Error (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
148
149    void
150    FatalError (int err, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
151
152    void
153    Verbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
154
155    void
156    Warning (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
157
158    void
159    WarningVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
160
161    Flags &
162    GetOptions();
163
164    const Flags &
165    GetOptions() const;
166
167    Flags &
168    GetMask();
169
170    const Flags &
171    GetMask() const;
172
173    bool
174    GetVerbose() const;
175
176    bool
177    GetDebug() const;
178
179protected:
180    //------------------------------------------------------------------
181    // Member variables
182    //------------------------------------------------------------------
183    lldb::StreamSP m_stream_sp;
184    Flags m_options;
185    Flags m_mask_bits;
186
187    void
188    PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args);
189
190private:
191    DISALLOW_COPY_AND_ASSIGN (Log);
192};
193
194
195class LogChannel : public PluginInterface
196{
197public:
198    LogChannel ();
199
200    virtual
201    ~LogChannel ();
202
203    static lldb::LogChannelSP
204    FindPlugin (const char *plugin_name);
205
206    // categories is a an array of chars that ends with a NULL element.
207    virtual void
208    Disable (const char **categories, Stream *feedback_strm) = 0;
209
210    virtual bool
211    Enable (lldb::StreamSP &log_stream_sp,
212            uint32_t log_options,
213            Stream *feedback_strm,      // Feedback stream for argument errors etc
214            const char **categories) = 0;// The categories to enable within this logging stream, if empty, enable default set
215
216    virtual void
217    ListCategories (Stream *strm) = 0;
218
219protected:
220    lldb::LogSP m_log_sp;
221
222private:
223    DISALLOW_COPY_AND_ASSIGN (LogChannel);
224};
225
226
227} // namespace lldb_private
228
229#endif  // liblldb_Log_H_
230