]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.h
4a426656d18b4a8ce88c2f51e112720c4f24d225
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
34 #define O_CLOEXEC 02000000
37 #ifndef F_DUPFD_CLOEXEC
38 #define F_DUPFD_CLOEXEC 1030
41 #define LXC_LOG_PREFIX_SIZE 32
42 #define LXC_LOG_BUFFER_SIZE 512
44 /* predefined priorities. */
46 LXC_LOG_PRIORITY_TRACE
,
47 LXC_LOG_PRIORITY_DEBUG
,
48 LXC_LOG_PRIORITY_INFO
,
49 LXC_LOG_PRIORITY_NOTICE
,
50 LXC_LOG_PRIORITY_WARN
,
51 LXC_LOG_PRIORITY_ERROR
,
52 LXC_LOG_PRIORITY_CRIT
,
53 LXC_LOG_PRIORITY_ALERT
,
54 LXC_LOG_PRIORITY_FATAL
,
55 LXC_LOG_PRIORITY_NOTSET
,
58 /* location information of the logging event */
59 struct lxc_log_locinfo
{
65 #define LXC_LOG_LOCINFO_INIT \
66 { .file = __FILE__, .func = __func__, .line = __LINE__ }
68 /* brief logging event object */
69 struct lxc_log_event
{
72 struct timeval timestamp
;
73 struct lxc_log_locinfo
*locinfo
;
78 /* log appender object */
79 struct lxc_log_appender
{
81 int (*append
)(const struct lxc_log_appender
*, struct lxc_log_event
*);
84 * appenders can be stacked
86 struct lxc_log_appender
*next
;
89 /* log category object */
90 struct lxc_log_category
{
93 struct lxc_log_appender
*appender
;
94 const struct lxc_log_category
*parent
;
98 * Returns true if the chained priority is equal to or higher than
102 lxc_log_priority_is_enabled(const struct lxc_log_category
* category
,
105 while (category
->priority
== LXC_LOG_PRIORITY_NOTSET
&&
107 category
= category
->parent
;
109 return priority
>= category
->priority
;
113 * converts a priority to a literal string
115 static inline const char* lxc_log_priority_to_string(int priority
)
118 case LXC_LOG_PRIORITY_TRACE
: return "TRACE";
119 case LXC_LOG_PRIORITY_DEBUG
: return "DEBUG";
120 case LXC_LOG_PRIORITY_INFO
: return "INFO";
121 case LXC_LOG_PRIORITY_NOTICE
: return "NOTICE";
122 case LXC_LOG_PRIORITY_WARN
: return "WARN";
123 case LXC_LOG_PRIORITY_ERROR
: return "ERROR";
124 case LXC_LOG_PRIORITY_CRIT
: return "CRIT";
125 case LXC_LOG_PRIORITY_ALERT
: return "ALERT";
126 case LXC_LOG_PRIORITY_FATAL
: return "FATAL";
132 * converts a literal priority to an int
134 static inline int lxc_log_priority_to_int(const char* name
)
136 if (!strcasecmp("TRACE", name
)) return LXC_LOG_PRIORITY_TRACE
;
137 if (!strcasecmp("DEBUG", name
)) return LXC_LOG_PRIORITY_DEBUG
;
138 if (!strcasecmp("INFO", name
)) return LXC_LOG_PRIORITY_INFO
;
139 if (!strcasecmp("NOTICE", name
)) return LXC_LOG_PRIORITY_NOTICE
;
140 if (!strcasecmp("WARN", name
)) return LXC_LOG_PRIORITY_WARN
;
141 if (!strcasecmp("ERROR", name
)) return LXC_LOG_PRIORITY_ERROR
;
142 if (!strcasecmp("CRIT", name
)) return LXC_LOG_PRIORITY_CRIT
;
143 if (!strcasecmp("ALERT", name
)) return LXC_LOG_PRIORITY_ALERT
;
144 if (!strcasecmp("FATAL", name
)) return LXC_LOG_PRIORITY_FATAL
;
146 return LXC_LOG_PRIORITY_NOTSET
;
150 __lxc_log_append(const struct lxc_log_appender
*appender
,
151 struct lxc_log_event
* event
)
153 va_list va
, *va_keep
;
154 va_keep
= event
->vap
;
157 va_copy(va
, *va_keep
);
159 appender
->append(appender
, event
);
160 appender
= appender
->next
;
166 __lxc_log(const struct lxc_log_category
* category
,
167 struct lxc_log_event
* event
)
170 __lxc_log_append(category
->appender
, event
);
171 category
= category
->parent
;
176 * Helper macro to define log functions.
178 #define lxc_log_priority_define(acategory, PRIORITY) \
180 static inline void LXC_##PRIORITY(struct lxc_log_locinfo *, \
181 const char *, ...) __attribute__ ((format (printf, 2, 3))); \
183 static inline void LXC_##PRIORITY(struct lxc_log_locinfo* locinfo, \
184 const char* format, ...) \
186 if (lxc_log_priority_is_enabled(acategory, \
187 LXC_LOG_PRIORITY_##PRIORITY)) { \
188 struct lxc_log_event evt = { \
189 .category = (acategory)->name, \
190 .priority = LXC_LOG_PRIORITY_##PRIORITY, \
196 gettimeofday(&evt.timestamp, NULL); \
198 va_start(va_ref, format); \
200 __lxc_log(acategory, &evt); \
206 * Helper macro to define and use static categories.
208 #define lxc_log_category_define(name, parent) \
209 extern struct lxc_log_category lxc_log_category_##parent; \
210 struct lxc_log_category lxc_log_category_##name = { \
212 LXC_LOG_PRIORITY_NOTSET, \
214 &lxc_log_category_##parent \
217 #define lxc_log_define(name, parent) \
218 lxc_log_category_define(name, parent) \
220 lxc_log_priority_define(&lxc_log_category_##name, TRACE) \
221 lxc_log_priority_define(&lxc_log_category_##name, DEBUG) \
222 lxc_log_priority_define(&lxc_log_category_##name, INFO) \
223 lxc_log_priority_define(&lxc_log_category_##name, NOTICE) \
224 lxc_log_priority_define(&lxc_log_category_##name, WARN) \
225 lxc_log_priority_define(&lxc_log_category_##name, ERROR) \
226 lxc_log_priority_define(&lxc_log_category_##name, CRIT) \
227 lxc_log_priority_define(&lxc_log_category_##name, ALERT) \
228 lxc_log_priority_define(&lxc_log_category_##name, FATAL)
230 #define lxc_log_category_priority(name) \
231 (lxc_log_priority_to_string(lxc_log_category_##name.priority))
236 #define TRACE(format, ...) do { \
237 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
238 LXC_TRACE(&locinfo, format, ##__VA_ARGS__); \
241 #define DEBUG(format, ...) do { \
242 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
243 LXC_DEBUG(&locinfo, format, ##__VA_ARGS__); \
246 #define INFO(format, ...) do { \
247 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
248 LXC_INFO(&locinfo, format, ##__VA_ARGS__); \
251 #define NOTICE(format, ...) do { \
252 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
253 LXC_NOTICE(&locinfo, format, ##__VA_ARGS__); \
256 #define WARN(format, ...) do { \
257 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
258 LXC_WARN(&locinfo, format, ##__VA_ARGS__); \
261 #define ERROR(format, ...) do { \
262 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
263 LXC_ERROR(&locinfo, format, ##__VA_ARGS__); \
266 #define CRIT(format, ...) do { \
267 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
268 LXC_CRIT(&locinfo, format, ##__VA_ARGS__); \
271 #define ALERT(format, ...) do { \
272 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
273 LXC_ALERT(&locinfo, format, ##__VA_ARGS__); \
276 #define FATAL(format, ...) do { \
277 struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
278 LXC_FATAL(&locinfo, format, ##__VA_ARGS__); \
283 #define SYSERROR(format, ...) do { \
284 ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
287 extern int lxc_log_fd
;
289 extern int lxc_log_init(const char *name
, const char *file
,
290 const char *priority
, const char *prefix
, int quiet
,
291 const char *lxcpath
);
293 extern int lxc_log_set_file(const char *fname
);
294 extern int lxc_log_set_level(int level
);
295 extern void lxc_log_set_prefix(const char *prefix
);
296 extern const char *lxc_log_get_file(void);
297 extern int lxc_log_get_level(void);
298 extern bool lxc_log_has_valid_level(void);
299 extern const char *lxc_log_get_prefix(void);