Log.h revision f737d372a9672c9feaedf4b2cd7b16e31357d38e
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//----------------------------------------------------------------------
54namespace lldb_private {
55
56class Log
57{
58public:
59
60    //------------------------------------------------------------------
61    // Callback definitions for abstracted plug-in log access.
62    //------------------------------------------------------------------
63    typedef void (*DisableCallback) (const char **categories, Stream *feedback_strm);
64    typedef lldb::LogSP (*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 char *channel,
82                        const Log::Callbacks &log_callbacks);
83
84    static bool
85    UnregisterLogChannel (const char *channel);
86
87    static bool
88    GetLogChannelCallbacks (const char *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 (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
180protected:
181    //------------------------------------------------------------------
182    // Member variables
183    //------------------------------------------------------------------
184    lldb::StreamSP m_stream_sp;
185    Flags m_options;
186    Flags m_mask_bits;
187
188    void
189    PrintfWithFlagsVarArg (uint32_t flags, const char *format, va_list args);
190
191private:
192    DISALLOW_COPY_AND_ASSIGN (Log);
193};
194
195
196class LogChannel : public PluginInterface
197{
198public:
199    LogChannel ();
200
201    virtual
202    ~LogChannel ();
203
204    static lldb::LogChannelSP
205    FindPlugin (const char *plugin_name);
206
207    // categories is a an array of chars that ends with a NULL element.
208    virtual void
209    Disable (const char **categories, Stream *feedback_strm) = 0;
210
211    virtual bool
212    Enable (lldb::StreamSP &log_stream_sp,
213            uint32_t log_options,
214            Stream *feedback_strm,      // Feedback stream for argument errors etc
215            const char **categories) = 0;// The categories to enable within this logging stream, if empty, enable default set
216
217    virtual void
218    ListCategories (Stream *strm) = 0;
219
220protected:
221    lldb::LogSP m_log_sp;
222
223private:
224    DISALLOW_COPY_AND_ASSIGN (LogChannel);
225};
226
227
228} // namespace lldb_private
229
230#endif  // liblldb_Log_H_
231