]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.h
2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 * Cedric Le Goater <legoater@free.fr>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
42 #define O_CLOEXEC 02000000
45 #ifndef F_DUPFD_CLOEXEC
46 #define F_DUPFD_CLOEXEC 1030
49 #define LXC_LOG_PREFIX_SIZE 32
50 #define LXC_LOG_BUFFER_SIZE 4096
52 /* This attribute is required to silence clang warnings */
54 #define ATTR_UNUSED __attribute__ ((unused))
59 /* predefined lxc log priorities. */
73 /* location information of the logging event */
74 struct lxc_log_locinfo
{
80 #define LXC_LOG_LOCINFO_INIT \
81 { .file = __FILE__, .func = __func__, .line = __LINE__ }
83 /* brief logging event object */
84 struct lxc_log_event
{
87 struct timespec timestamp
;
88 struct lxc_log_locinfo
*locinfo
;
93 /* log appender object */
94 struct lxc_log_appender
{
96 int (*append
)(const struct lxc_log_appender
*, struct lxc_log_event
*);
99 * appenders can be stacked
101 struct lxc_log_appender
*next
;
104 /* log category object */
105 struct lxc_log_category
{
108 struct lxc_log_appender
*appender
;
109 const struct lxc_log_category
*parent
;
113 extern int lxc_log_use_global_fd
;
117 * Returns true if the chained priority is equal to or higher than
121 lxc_log_priority_is_enabled(const struct lxc_log_category
* category
,
124 while (category
->priority
== LXC_LOG_LEVEL_NOTSET
&&
126 category
= category
->parent
;
128 int cmp_prio
= category
->priority
;
130 if (!lxc_log_use_global_fd
&& current_config
&&
131 current_config
->loglevel
!= LXC_LOG_LEVEL_NOTSET
)
132 cmp_prio
= current_config
->loglevel
;
135 return priority
>= cmp_prio
;
139 * converts a priority to a literal string
141 static inline const char* lxc_log_priority_to_string(int priority
)
144 case LXC_LOG_LEVEL_TRACE
: return "TRACE";
145 case LXC_LOG_LEVEL_DEBUG
: return "DEBUG";
146 case LXC_LOG_LEVEL_INFO
: return "INFO";
147 case LXC_LOG_LEVEL_NOTICE
: return "NOTICE";
148 case LXC_LOG_LEVEL_WARN
: return "WARN";
149 case LXC_LOG_LEVEL_ERROR
: return "ERROR";
150 case LXC_LOG_LEVEL_CRIT
: return "CRIT";
151 case LXC_LOG_LEVEL_ALERT
: return "ALERT";
152 case LXC_LOG_LEVEL_FATAL
: return "FATAL";
158 static inline const char* lxc_syslog_priority_to_string(int priority
)
161 case LOG_DAEMON
: return "daemon";
162 case LOG_LOCAL0
: return "local0";
163 case LOG_LOCAL1
: return "local1";
164 case LOG_LOCAL2
: return "local2";
165 case LOG_LOCAL3
: return "local3";
166 case LOG_LOCAL4
: return "local4";
167 case LOG_LOCAL5
: return "local5";
168 case LOG_LOCAL6
: return "local6";
169 case LOG_LOCAL7
: return "local7";
176 * converts a literal priority to an int
178 static inline int lxc_log_priority_to_int(const char* name
)
180 if (!strcasecmp("TRACE", name
)) return LXC_LOG_LEVEL_TRACE
;
181 if (!strcasecmp("DEBUG", name
)) return LXC_LOG_LEVEL_DEBUG
;
182 if (!strcasecmp("INFO", name
)) return LXC_LOG_LEVEL_INFO
;
183 if (!strcasecmp("NOTICE", name
)) return LXC_LOG_LEVEL_NOTICE
;
184 if (!strcasecmp("WARN", name
)) return LXC_LOG_LEVEL_WARN
;
185 if (!strcasecmp("ERROR", name
)) return LXC_LOG_LEVEL_ERROR
;
186 if (!strcasecmp("CRIT", name
)) return LXC_LOG_LEVEL_CRIT
;
187 if (!strcasecmp("ALERT", name
)) return LXC_LOG_LEVEL_ALERT
;
188 if (!strcasecmp("FATAL", name
)) return LXC_LOG_LEVEL_FATAL
;
190 return LXC_LOG_LEVEL_NOTSET
;
193 static inline int lxc_syslog_priority_to_int(const char* name
)
195 if (!strcasecmp("daemon", name
)) return LOG_DAEMON
;
196 if (!strcasecmp("local0", name
)) return LOG_LOCAL0
;
197 if (!strcasecmp("local1", name
)) return LOG_LOCAL1
;
198 if (!strcasecmp("local2", name
)) return LOG_LOCAL2
;
199 if (!strcasecmp("local3", name
)) return LOG_LOCAL3
;
200 if (!strcasecmp("local4", name
)) return LOG_LOCAL4
;
201 if (!strcasecmp("local5", name
)) return LOG_LOCAL5
;
202 if (!strcasecmp("local6", name
)) return LOG_LOCAL6
;
203 if (!strcasecmp("local7", name
)) return LOG_LOCAL7
;
209 __lxc_log_append(const struct lxc_log_appender
*appender
,
210 struct lxc_log_event
* event
)
212 va_list va
, *va_keep
;
213 va_keep
= event
->vap
;
216 va_copy(va
, *va_keep
);
218 appender
->append(appender
, event
);
219 appender
= appender
->next
;
225 __lxc_log(const struct lxc_log_category
* category
,
226 struct lxc_log_event
* event
)
229 __lxc_log_append(category
->appender
, event
);
230 category
= category
->parent
;
235 * Helper macro to define log functions.
237 #define lxc_log_priority_define(acategory, LEVEL) \
239 ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo *, \
240 const char *, ...) __attribute__ ((format (printf, 2, 3))); \
242 ATTR_UNUSED static inline void LXC_##LEVEL(struct lxc_log_locinfo* locinfo, \
243 const char* format, ...) \
245 if (lxc_log_priority_is_enabled(acategory, \
246 LXC_LOG_LEVEL_##LEVEL)) { \
247 struct lxc_log_event evt = { \
248 .category = (acategory)->name, \
249 .priority = LXC_LOG_LEVEL_##LEVEL, \
255 /* clock_gettime() is explicitly marked as MT-Safe \
256 * without restrictions. So let's use it for our \
257 * logging stamps. */ \
258 clock_gettime(CLOCK_REALTIME, &evt.timestamp); \
260 va_start(va_ref, format); \
262 __lxc_log(acategory, &evt); \
268 * Helper macro to define and use static categories.
270 #define lxc_log_category_define(name, parent) \
271 extern struct lxc_log_category lxc_log_category_##parent; \
272 struct lxc_log_category lxc_log_category_##name = { \
274 LXC_LOG_LEVEL_NOTSET, \
276 &lxc_log_category_##parent \
279 #define lxc_log_define(name, parent) \
280 lxc_log_category_define(name, parent) \
282 lxc_log_priority_define(&lxc_log_category_##name, TRACE) \
283 lxc_log_priority_define(&lxc_log_category_##name, DEBUG) \
284 lxc_log_priority_define(&lxc_log_category_##name, INFO) \
285 lxc_log_priority_define(&lxc_log_category_##name, NOTICE) \
286 lxc_log_priority_define(&lxc_log_category_##name, WARN) \
287 lxc_log_priority_define(&lxc_log_category_##name, ERROR) \
288 lxc_log_priority_define(&lxc_log_category_##name, CRIT) \
289 lxc_log_priority_define(&lxc_log_category_##name, ALERT) \
290 lxc_log_priority_define(&lxc_log_category_##name, FATAL)
292 #define lxc_log_category_priority(name) \
293 (lxc_log_priority_to_string(lxc_log_category_##name.priority))
298 #define TRACE(format, ...) do { \
299 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
300 LXC_TRACE(&locinfo, format, ##__VA_ARGS__); \
303 #define DEBUG(format, ...) do { \
304 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
305 LXC_DEBUG(&locinfo, format, ##__VA_ARGS__); \
308 #define INFO(format, ...) do { \
309 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
310 LXC_INFO(&locinfo, format, ##__VA_ARGS__); \
313 #define NOTICE(format, ...) do { \
314 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
315 LXC_NOTICE(&locinfo, format, ##__VA_ARGS__); \
318 #define WARN(format, ...) do { \
319 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
320 LXC_WARN(&locinfo, format, ##__VA_ARGS__); \
323 #define ERROR(format, ...) do { \
324 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
325 LXC_ERROR(&locinfo, format, ##__VA_ARGS__); \
328 #define CRIT(format, ...) do { \
329 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
330 LXC_CRIT(&locinfo, format, ##__VA_ARGS__); \
333 #define ALERT(format, ...) do { \
334 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
335 LXC_ALERT(&locinfo, format, ##__VA_ARGS__); \
338 #define FATAL(format, ...) do { \
339 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
340 LXC_FATAL(&locinfo, format, ##__VA_ARGS__); \
345 #define SYSERROR(format, ...) do { \
346 ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
349 extern int lxc_log_fd
;
351 extern int lxc_log_set_file(int *fd
, const char *fname
);
352 extern int lxc_log_syslog(int facility
);
353 extern void lxc_log_enable_syslog(void);
354 extern int lxc_log_set_level(int *dest
, int level
);
355 extern void lxc_log_set_prefix(const char *prefix
);
356 extern const char *lxc_log_get_file(void);
357 extern int lxc_log_get_level(void);
358 extern bool lxc_log_has_valid_level(void);
359 extern const char *lxc_log_get_prefix(void);
360 extern void lxc_log_options_no_override();