Log.h revision 263363
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 <stdarg.h>
15#include <stdint.h>
16#include <signal.h>
17#include <stdio.h>
18
19// C++ Includes
20// Other libraries and framework includes
21// Project includes
22#include "lldb/lldb-private.h"
23#include "lldb/Core/ConstString.h"
24#include "lldb/Core/Flags.h"
25#include "lldb/Core/PluginInterface.h"
26
27//----------------------------------------------------------------------
28// Logging types
29//----------------------------------------------------------------------
30#define LLDB_LOG_FLAG_STDOUT    (1u << 0)
31#define LLDB_LOG_FLAG_STDERR    (1u << 1)
32#define LLDB_LOG_FLAG_FATAL     (1u << 2)
33#define LLDB_LOG_FLAG_ERROR     (1u << 3)
34#define LLDB_LOG_FLAG_WARNING   (1u << 4)
35#define LLDB_LOG_FLAG_DEBUG     (1u << 5)
36#define LLDB_LOG_FLAG_VERBOSE   (1u << 6)
37
38//----------------------------------------------------------------------
39// Logging Options
40//----------------------------------------------------------------------
41#define LLDB_LOG_OPTION_THREADSAFE              (1u << 0)
42#define LLDB_LOG_OPTION_VERBOSE                 (1u << 1)
43#define LLDB_LOG_OPTION_DEBUG                   (1u << 2)
44#define LLDB_LOG_OPTION_PREPEND_SEQUENCE        (1u << 3)
45#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP       (1u << 4)
46#define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD (1u << 5)
47#define LLDB_LOG_OPTION_PREPEND_THREAD_NAME     (1U << 6)
48#define LLDB_LOG_OPTION_BACKTRACE               (1U << 7)
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 Log * (*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 ConstString &channel,
81                        const Log::Callbacks &log_callbacks);
82
83    static bool
84    UnregisterLogChannel (const ConstString &channel);
85
86    static bool
87    GetLogChannelCallbacks (const ConstString &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 (const 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
179    void
180    SetStream (const lldb::StreamSP &stream_sp)
181    {
182        m_stream_sp = stream_sp;
183    }
184
185protected:
186    //------------------------------------------------------------------
187    // Member variables
188    //------------------------------------------------------------------
189    lldb::StreamSP m_stream_sp;
190    Flags m_options;
191    Flags m_mask_bits;
192
193    void
194    PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args);
195
196private:
197    DISALLOW_COPY_AND_ASSIGN (Log);
198};
199
200
201class LogChannel : public PluginInterface
202{
203public:
204    LogChannel ();
205
206    virtual
207    ~LogChannel ();
208
209    static lldb::LogChannelSP
210    FindPlugin (const char *plugin_name);
211
212    // categories is a an array of chars that ends with a NULL element.
213    virtual void
214    Disable (const char **categories, Stream *feedback_strm) = 0;
215
216    virtual bool
217    Enable (lldb::StreamSP &log_stream_sp,
218            uint32_t log_options,
219            Stream *feedback_strm,      // Feedback stream for argument errors etc
220            const char **categories) = 0;// The categories to enable within this logging stream, if empty, enable default set
221
222    virtual void
223    ListCategories (Stream *strm) = 0;
224
225protected:
226    std::unique_ptr<Log> m_log_ap;
227
228private:
229    DISALLOW_COPY_AND_ASSIGN (LogChannel);
230};
231
232
233} // namespace lldb_private
234
235#endif  // liblldb_Log_H_
236