1 //===-- DNBLog.h ------------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  Created by Greg Clayton on 6/18/07.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBLOG_H
14 #define LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBLOG_H
15 
16 #include "DNBDefs.h"
17 #include <stdint.h>
18 #include <stdio.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 // Flags that get filled in automatically before calling the log callback
25 // function
26 #define DNBLOG_FLAG_FATAL (1u << 0)
27 #define DNBLOG_FLAG_ERROR (1u << 1)
28 #define DNBLOG_FLAG_WARNING (1u << 2)
29 #define DNBLOG_FLAG_DEBUG (1u << 3)
30 #define DNBLOG_FLAG_VERBOSE (1u << 4)
31 #define DNBLOG_FLAG_THREADED (1u << 5)
32 
33 #define DNBLOG_ENABLED
34 
35 #if defined(DNBLOG_ENABLED)
36 
37 void _DNBLog(uint32_t flags, const char *format, ...)
38     __attribute__((format(printf, 2, 3)));
39 void _DNBLogDebug(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
40 void _DNBLogDebugVerbose(const char *fmt, ...)
41     __attribute__((format(printf, 1, 2)));
42 void _DNBLogThreaded(const char *fmt, ...)
43     __attribute__((format(printf, 1, 2)));
44 void _DNBLogThreadedIf(uint32_t mask, const char *fmt, ...)
45     __attribute__((format(printf, 2, 3)));
46 void _DNBLogError(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
47 void _DNBLogFatalError(int err, const char *fmt, ...)
48     __attribute__((format(printf, 2, 3)));
49 void _DNBLogVerbose(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
50 void _DNBLogWarning(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
51 void _DNBLogWarningVerbose(const char *fmt, ...)
52     __attribute__((format(printf, 1, 2)));
53 bool DNBLogCheckLogBit(uint32_t bit);
54 uint32_t DNBLogSetLogMask(uint32_t mask);
55 uint32_t DNBLogGetLogMask();
56 void DNBLogSetLogCallback(DNBCallbackLog callback, void *baton);
57 DNBCallbackLog DNBLogGetLogCallback();
58 bool DNBLogEnabled();
59 bool DNBLogEnabledForAny(uint32_t mask);
60 int DNBLogGetDebug();
61 void DNBLogSetDebug(int g);
62 int DNBLogGetVerbose();
63 void DNBLogSetVerbose(int g);
64 
65 #define DNBLog(fmt, ...)                                                       \
66   do {                                                                         \
67     if (DNBLogEnabled()) {                                                     \
68       _DNBLog(0, fmt, ##__VA_ARGS__);                                          \
69     }                                                                          \
70   } while (0)
71 #define DNBLogDebug(fmt, ...)                                                  \
72   do {                                                                         \
73     if (DNBLogEnabled()) {                                                     \
74       _DNBLogDebug(fmt, ##__VA_ARGS__);                                        \
75     }                                                                          \
76   } while (0)
77 #define DNBLogDebugVerbose(fmt, ...)                                           \
78   do {                                                                         \
79     if (DNBLogEnabled()) {                                                     \
80       _DNBLogDebugVerbose(fmt, ##__VA_ARGS__);                                 \
81     }                                                                          \
82   } while (0)
83 #define DNBLogThreaded(fmt, ...)                                               \
84   do {                                                                         \
85     if (DNBLogEnabled()) {                                                     \
86       _DNBLogThreaded(fmt, ##__VA_ARGS__);                                     \
87     }                                                                          \
88   } while (0)
89 #define DNBLogThreadedIf(mask, fmt, ...)                                       \
90   do {                                                                         \
91     if (DNBLogEnabledForAny(mask)) {                                           \
92       _DNBLogThreaded(fmt, ##__VA_ARGS__);                                     \
93     }                                                                          \
94   } while (0)
95 #define DNBLogError(fmt, ...)                                                  \
96   do {                                                                         \
97     if (DNBLogEnabled()) {                                                     \
98       _DNBLogError(fmt, ##__VA_ARGS__);                                        \
99     }                                                                          \
100   } while (0)
101 #define DNBLogFatalError(err, fmt, ...)                                        \
102   do {                                                                         \
103     if (DNBLogEnabled()) {                                                     \
104       _DNBLogFatalError(err, fmt, ##__VA_ARGS__);                              \
105     }                                                                          \
106   } while (0)
107 #define DNBLogVerbose(fmt, ...)                                                \
108   do {                                                                         \
109     if (DNBLogEnabled()) {                                                     \
110       _DNBLogVerbose(fmt, ##__VA_ARGS__);                                      \
111     }                                                                          \
112   } while (0)
113 #define DNBLogWarning(fmt, ...)                                                \
114   do {                                                                         \
115     if (DNBLogEnabled()) {                                                     \
116       _DNBLogWarning(fmt, ##__VA_ARGS__);                                      \
117     }                                                                          \
118   } while (0)
119 #define DNBLogWarningVerbose(fmt, ...)                                         \
120   do {                                                                         \
121     if (DNBLogEnabled()) {                                                     \
122       _DNBLogWarningVerbose(fmt, ##__VA_ARGS__);                               \
123     }                                                                          \
124   } while (0)
125 
126 #else // #if defined(DNBLOG_ENABLED)
127 
128 #define DNBLogDebug(...) ((void)0)
129 #define DNBLogDebugVerbose(...) ((void)0)
130 #define DNBLogThreaded(...) ((void)0)
131 #define DNBLogThreadedIf(...) ((void)0)
132 #define DNBLogError(...) ((void)0)
133 #define DNBLogFatalError(...) ((void)0)
134 #define DNBLogVerbose(...) ((void)0)
135 #define DNBLogWarning(...) ((void)0)
136 #define DNBLogWarningVerbose(...) ((void)0)
137 #define DNBLogGetLogFile() ((FILE *)NULL)
138 #define DNBLogSetLogFile(f) ((void)0)
139 #define DNBLogCheckLogBit(bit) ((bool)false)
140 #define DNBLogSetLogMask(mask) ((uint32_t)0u)
141 #define DNBLogGetLogMask() ((uint32_t)0u)
142 #define DNBLogToASL() ((void)0)
143 #define DNBLogToFile() ((void)0)
144 #define DNBLogCloseLogFile() ((void)0)
145 
146 #endif // #else defined(DNBLOG_ENABLED)
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
152 #endif // LLDB_TOOLS_DEBUGSERVER_SOURCE_DNBLOG_H
153