Dobby 3.0
Dobby “Docker based Thingy” is a tool for managing and running OCI containers using crun
Loading...
Searching...
No Matches
Logging.h
1/*
2* If not stated otherwise in this file or this component's LICENSE file the
3* following copyright and licenses apply:
4*
5* Copyright 2019 Sky UK
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*/
19//
20// Logging.h
21// AppInfrastructure
22//
23//
24#ifndef LOGGING_H
25#define LOGGING_H
26
27#include <unistd.h>
28
29#ifndef TEMP_FAILURE_RETRY
30#define TEMP_FAILURE_RETRY(exp) \
31 ({ \
32 decltype(exp) _rc; \
33 do { \
34 _rc = (exp); \
35 } while (_rc == -1 && errno == EINTR); \
36 _rc; \
37 })
38#endif
39
40// There's no nice, reliable way to test which version of strerror_r
41// we have at compile time, so if building for a platform that uses the
42// XSI-compliant version (e.g. OS X), define HAVE_GNU_STRERROR_R as 0.
43#ifndef HAVE_GNU_STRERROR_R
44 #if defined(__APPLE__)
45 #define HAVE_GNU_STRERROR_R 0
46 #else
47 #define HAVE_GNU_STRERROR_R 1
48 #endif
49#endif
50
51
52
57#if !defined(AI_BUILD_TYPE) || !defined(AI_RELEASE) || !defined(AI_DEBUG)
58# warning "No build type defined, expected AI_BUILD_TYPE to be defined to either AI_RELEASE or AI_DEBUG"
59#endif
60#if (AI_BUILD_TYPE != AI_RELEASE) && (AI_BUILD_TYPE != AI_DEBUG)
61# warning "AI_BUILD_TYPE is not equal to AI_RELEASE or AI_DEBUG"
62#endif
63
64
65#ifdef __cplusplus
66extern "C" {
67#endif
68
69
70
71extern int __ai_debug_log_level;
72extern void __ai_debug_log_printf(int level, const char *file, const char *func,
73 int line, const char *fmt, ...)
74 __attribute__ ((format (printf, 5, 6)));
75extern void __ai_debug_log_sys_printf(int err, int level, const char *file,
76 const char *func, int line,
77 const char *fmt, ...)
78 __attribute__ ((format (printf, 6, 7)));
79
80
81#define AI_DEBUG_LEVEL_PROD_MILESTONE -1
82#define AI_DEBUG_LEVEL_FATAL 0
83#define AI_DEBUG_LEVEL_ERROR 1
84#define AI_DEBUG_LEVEL_WARNING 2
85#define AI_DEBUG_LEVEL_MILESTONE 3
86#define AI_DEBUG_LEVEL_INFO 4 // Only in debug builds
87#define AI_DEBUG_LEVEL_DEBUG 5 // Only in debug builds
88
89
90
97#define __AI_LOG_PRINTF(level, fmt, ...) \
98 do { \
99 if (__builtin_expect(((level) <= __ai_debug_log_level),0)) \
100 __ai_debug_log_printf((level), __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__); \
101 } while(0)
102
103#define __AI_LOG_SYS_PRINTF(err, level, fmt, ...) \
104 do { \
105 if (__builtin_expect(((level) <= __ai_debug_log_level),0)) \
106 __ai_debug_log_sys_printf((err), (level), __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__); \
107 } while(0)
108
109
116#define AI_LOG_PROD_MILESTONE(fmt,...) \
117 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_PROD_MILESTONE, fmt, ##__VA_ARGS__)
118
119#define AI_LOG_MILESTONE(fmt,...) \
120 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_MILESTONE, fmt, ##__VA_ARGS__)
121
122#define AI_LOG_WARN(fmt,...) \
123 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
124#define AI_LOG_SYS_WARN(err,fmt,...) \
125 __AI_LOG_SYS_PRINTF(err, AI_DEBUG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
126
127#define AI_LOG_ERROR(fmt,...) \
128 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
129#define AI_LOG_SYS_ERROR(err, fmt,...) \
130 __AI_LOG_SYS_PRINTF(err, AI_DEBUG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
131#define AI_LOG_ERROR_EXIT(fmt,...) \
132 do { \
133 AI_LOG_ERROR(fmt, ##__VA_ARGS__); \
134 AI_LOG_FN_EXIT(); \
135 } while(0)
136#define AI_LOG_SYS_ERROR_EXIT(err,fmt,...) \
137 do { \
138 AI_LOG_SYS_ERROR(err, fmt, ##__VA_ARGS__); \
139 AI_LOG_FN_EXIT(); \
140 } while(0)
141
142#define AI_LOG_FATAL(fmt,...) \
143 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_FATAL, fmt, ##__VA_ARGS__)
144#define AI_LOG_SYS_FATAL(err,fmt,...) \
145 __AI_LOG_SYS_PRINTF(err, AI_DEBUG_LEVEL_FATAL, fmt, ##__VA_ARGS__)
146#define AI_LOG_FATAL_EXIT(fmt,...) \
147 do { \
148 AI_LOG_FATAL(fmt, ##__VA_ARGS__); \
149 AI_LOG_FN_EXIT(); \
150 } while(0)
151#define AI_LOG_SYS_FATAL_EXIT(err,fmt,...) \
152 do { \
153 AI_LOG_SYS_FATAL(err, fmt, ##__VA_ARGS__); \
154 AI_LOG_FN_EXIT(); \
155 } while(0)
156
157
161#if (AI_BUILD_TYPE == AI_RELEASE)
162 #define AI_LOG_FN_ENTRY()
163 #define AI_LOG_FN_EXIT()
164 #define AI_LOG_DEBUG(fmt,...)
165 #define AI_LOG_INFO(fmt,...)
166 #define AI_DEBUG_ASSERT(condition)
167
171#else /* (AI_BUILD_TYPE == AI_RELEASE) */
172 #define AI_LOG_FN_ENTRY() \
173 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_DEBUG, "entry")
174 #define AI_LOG_FN_EXIT() \
175 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_DEBUG, "exit")
176 #define AI_LOG_DEBUG(fmt,...) \
177 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
178 #define AI_LOG_INFO(fmt,...) \
179 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_INFO, fmt, ##__VA_ARGS__)
180 #include <assert.h>
181 #define AI_DEBUG_ASSERT(condition) \
182 do { \
183 if (! (condition) ) \
184 __AI_LOG_PRINTF(AI_DEBUG_LEVEL_FATAL, "ASSERT - " #condition ); \
185 assert( condition ); \
186 } while(0)
187#endif /* (AI_BUILD_TYPE != AI_RELEASE) */
188
189
190
191#ifdef __cplusplus
192}
193#endif
194
195#ifdef __cplusplus
196
197#include <functional>
198
199
200namespace AICommon
201{
202
203 typedef std::function<void (int level, const char *file, const char *func, int line, const char *message)> diag_printer_t;
204
205 void initLogging(diag_printer_t diagPrinter = nullptr);
206 void termLogging();
207
208} // namespace AICommon
209
210#endif // defined(__cplusplus)
211
212
213
214#endif /* LOGGING_H */
215