1221828Sgrehan//===-- Log.h ---------------------------------------------------*- C++ -*-===//
2221828Sgrehan//
3221828Sgrehan//                     The LLVM Compiler Infrastructure
4221828Sgrehan//
5221828Sgrehan// This file is distributed under the University of Illinois Open Source
6221828Sgrehan// License. See LICENSE.TXT for details.
7221828Sgrehan//
8221828Sgrehan//===----------------------------------------------------------------------===//
9221828Sgrehan
10221828Sgrehan#ifndef liblldb_Log_h_
11221828Sgrehan#define liblldb_Log_h_
12221828Sgrehan
13221828Sgrehan// C Includes
14221828Sgrehan#include <stdarg.h>
15221828Sgrehan#include <stdint.h>
16221828Sgrehan#include <signal.h>
17221828Sgrehan#include <stdio.h>
18221828Sgrehan
19221828Sgrehan// C++ Includes
20221828Sgrehan// Other libraries and framework includes
21221828Sgrehan// Project includes
22221828Sgrehan#include "lldb/lldb-private.h"
23221828Sgrehan#include "lldb/Core/ConstString.h"
24221828Sgrehan#include "lldb/Core/Flags.h"
25221828Sgrehan#include "lldb/Core/PluginInterface.h"
26221828Sgrehan
27221828Sgrehan//----------------------------------------------------------------------
28221828Sgrehan// Logging types
29221828Sgrehan//----------------------------------------------------------------------
30221828Sgrehan#define LLDB_LOG_FLAG_STDOUT    (1u << 0)
31221828Sgrehan#define LLDB_LOG_FLAG_STDERR    (1u << 1)
32221828Sgrehan#define LLDB_LOG_FLAG_FATAL     (1u << 2)
33221828Sgrehan#define LLDB_LOG_FLAG_ERROR     (1u << 3)
34221828Sgrehan#define LLDB_LOG_FLAG_WARNING   (1u << 4)
35221828Sgrehan#define LLDB_LOG_FLAG_DEBUG     (1u << 5)
36221828Sgrehan#define LLDB_LOG_FLAG_VERBOSE   (1u << 6)
37250427Sneel
38221828Sgrehan//----------------------------------------------------------------------
39248389Sneel// Logging Options
40248389Sneel//----------------------------------------------------------------------
41248389Sneel#define LLDB_LOG_OPTION_THREADSAFE              (1u << 0)
42248389Sneel#define LLDB_LOG_OPTION_VERBOSE                 (1u << 1)
43248389Sneel#define LLDB_LOG_OPTION_DEBUG                   (1u << 2)
44248389Sneel#define LLDB_LOG_OPTION_PREPEND_SEQUENCE        (1u << 3)
45270074Sgrehan#define LLDB_LOG_OPTION_PREPEND_TIMESTAMP       (1u << 4)
46270074Sgrehan#define LLDB_LOG_OPTION_PREPEND_PROC_AND_THREAD (1u << 5)
47270074Sgrehan#define LLDB_LOG_OPTION_PREPEND_THREAD_NAME     (1U << 6)
48270074Sgrehan#define LLDB_LOG_OPTION_BACKTRACE               (1U << 7)
49221828Sgrehan
50221828Sgrehan//----------------------------------------------------------------------
51250427Sneel// Logging Functions
52248389Sneel//----------------------------------------------------------------------
53270074Sgrehannamespace lldb_private {
54248389Sneel
55221828Sgrehanclass Log
56221828Sgrehan{
57270071Sgrehanpublic:
58221828Sgrehan
59270074Sgrehan    //------------------------------------------------------------------
60221828Sgrehan    // Callback definitions for abstracted plug-in log access.
61270074Sgrehan    //------------------------------------------------------------------
62221828Sgrehan    typedef void (*DisableCallback) (const char **categories, Stream *feedback_strm);
63270071Sgrehan    typedef Log * (*EnableCallback) (lldb::StreamSP &log_stream_sp,
64221828Sgrehan                                     uint32_t log_options,
65270074Sgrehan                                     const char **categories,
66270074Sgrehan                                     Stream *feedback_strm);
67270074Sgrehan    typedef void (*ListCategoriesCallback) (Stream *strm);
68248389Sneel
69248389Sneel    struct Callbacks
70248389Sneel    {
71248389Sneel        DisableCallback disable;
72250427Sneel        EnableCallback enable;
73248389Sneel        ListCategoriesCallback list_categories;
74250427Sneel    };
75248389Sneel
76250427Sneel    //------------------------------------------------------------------
77248389Sneel    // Static accessors for logging channels
78270074Sgrehan    //------------------------------------------------------------------
79270074Sgrehan    static void
80270074Sgrehan    RegisterLogChannel (const ConstString &channel,
81250427Sneel                        const Log::Callbacks &log_callbacks);
82250427Sneel
83250427Sneel    static bool
84221828Sgrehan    UnregisterLogChannel (const ConstString &channel);
85270071Sgrehan
86221828Sgrehan    static bool
87221828Sgrehan    GetLogChannelCallbacks (const ConstString &channel,
88221828Sgrehan                            Log::Callbacks &log_callbacks);
89221828Sgrehan
90221828Sgrehan
91221828Sgrehan    static void
92250427Sneel    EnableAllLogChannels (lldb::StreamSP &log_stream_sp,
93221828Sgrehan                          uint32_t log_options,
94221828Sgrehan                          const char **categories,
95250427Sneel                          Stream *feedback_strm);
96250427Sneel
97250427Sneel    static void
98250427Sneel    DisableAllLogChannels (Stream *feedback_strm);
99250427Sneel
100250427Sneel    static void
101250427Sneel    ListAllLogChannels (Stream *strm);
102250427Sneel
103250427Sneel    static void
104250427Sneel    Initialize ();
105250427Sneel
106250427Sneel    static void
107250427Sneel    Terminate ();
108250427Sneel
109270074Sgrehan    //------------------------------------------------------------------
110270074Sgrehan    // Auto completion
111270074Sgrehan    //------------------------------------------------------------------
112270074Sgrehan    static void
113270074Sgrehan    AutoCompleteChannelName (const char *channel_name,
114270074Sgrehan                             StringList &matches);
115270074Sgrehan
116270074Sgrehan    //------------------------------------------------------------------
117270074Sgrehan    // Member functions
118270074Sgrehan    //------------------------------------------------------------------
119270074Sgrehan    Log ();
120270074Sgrehan
121270074Sgrehan    Log (const lldb::StreamSP &stream_sp);
122270074Sgrehan
123221828Sgrehan    ~Log ();
124221828Sgrehan
125250427Sneel    void
126250427Sneel    PutCString (const char *cstr);
127250427Sneel
128221828Sgrehan    void
129221828Sgrehan    Printf (const char *format, ...)  __attribute__ ((format (printf, 2, 3)));
130221828Sgrehan
131270074Sgrehan    void
132270074Sgrehan    VAPrintf (const char *format, va_list args);
133270074Sgrehan
134270074Sgrehan    void
135270074Sgrehan    PrintfWithFlags( uint32_t flags, const char *format, ...)  __attribute__ ((format (printf, 3, 4)));
136270074Sgrehan
137270074Sgrehan    void
138270074Sgrehan    LogIf (uint32_t mask, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
139270074Sgrehan
140248389Sneel    void
141248389Sneel    Debug (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
142248389Sneel
143248389Sneel    void
144248935Sneel    DebugVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
145248935Sneel
146248935Sneel    void
147248935Sneel    Error (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
148248935Sneel
149248935Sneel    void
150248935Sneel    FatalError (int err, const char *fmt, ...)  __attribute__ ((format (printf, 3, 4)));
151248935Sneel
152248935Sneel    void
153266339Sjhb    Verbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
154266339Sjhb
155248935Sneel    void
156248935Sneel    Warning (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
157248935Sneel
158266339Sjhb    void
159266593Sjhb    WarningVerbose (const char *fmt, ...)  __attribute__ ((format (printf, 2, 3)));
160221828Sgrehan
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