1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* lxc: linux Container library
*
* (C) Copyright IBM Corp. 2007, 2008
*
* Authors:
* Daniel Lezcano <dlezcano at fr.ibm.com>
* Cedric Le Goater <legoater@free.fr>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _log_h
#define _log_h
#include <stdarg.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#ifndef O_CLOEXEC
#define O_CLOEXEC 02000000
#endif
#ifndef F_DUPFD_CLOEXEC
#define F_DUPFD_CLOEXEC 1030
#endif
#define LXC_LOG_PREFIX_SIZE 32
#define LXC_LOG_BUFFER_SIZE 512
/* predefined priorities. */
enum {
LXC_LOG_PRIORITY_TRACE,
LXC_LOG_PRIORITY_DEBUG,
LXC_LOG_PRIORITY_INFO,
LXC_LOG_PRIORITY_NOTICE,
LXC_LOG_PRIORITY_WARN,
LXC_LOG_PRIORITY_ERROR,
LXC_LOG_PRIORITY_CRIT,
LXC_LOG_PRIORITY_ALERT,
LXC_LOG_PRIORITY_FATAL,
LXC_LOG_PRIORITY_NOTSET,
};
/* location information of the logging event */
struct lxc_log_locinfo {
const char *file;
const char *func;
int line;
};
#define LXC_LOG_LOCINFO_INIT \
{ .file = __FILE__, .func = __func__, .line = __LINE__ }
/* brief logging event object */
struct lxc_log_event {
const char* category;
int priority;
struct timeval timestamp;
struct lxc_log_locinfo *locinfo;
const char *fmt;
va_list *vap;
};
/* log appender object */
struct lxc_log_appender {
const char* name;
int (*append)(const struct lxc_log_appender *, struct lxc_log_event *);
/*
* appenders can be stacked
*/
struct lxc_log_appender *next;
};
/* log category object */
struct lxc_log_category {
const char *name;
int priority;
struct lxc_log_appender *appender;
const struct lxc_log_category *parent;
};
/*
* Returns true if the chained priority is equal to or higher than
* given priority.
*/
static inline int
lxc_log_priority_is_enabled(const struct lxc_log_category* category,
int priority)
{
while (category->priority == LXC_LOG_PRIORITY_NOTSET &&
category->parent)
category = category->parent;
return priority >= category->priority;
}
/*
* converts a priority to a literal string
*/
static inline const char* lxc_log_priority_to_string(int priority)
{
switch (priority) {
case LXC_LOG_PRIORITY_TRACE: return "TRACE";
case LXC_LOG_PRIORITY_DEBUG: return "DEBUG";
case LXC_LOG_PRIORITY_INFO: return "INFO";
case LXC_LOG_PRIORITY_NOTICE: return "NOTICE";
case LXC_LOG_PRIORITY_WARN: return "WARN";
case LXC_LOG_PRIORITY_ERROR: return "ERROR";
case LXC_LOG_PRIORITY_CRIT: return "CRIT";
case LXC_LOG_PRIORITY_ALERT: return "ALERT";
case LXC_LOG_PRIORITY_FATAL: return "FATAL";
default:
return "NOTSET";
}
}
/*
* converts a literal priority to an int
*/
static inline int lxc_log_priority_to_int(const char* name)
{
if (!strcasecmp("TRACE", name)) return LXC_LOG_PRIORITY_TRACE;
if (!strcasecmp("DEBUG", name)) return LXC_LOG_PRIORITY_DEBUG;
if (!strcasecmp("INFO", name)) return LXC_LOG_PRIORITY_INFO;
if (!strcasecmp("NOTICE", name)) return LXC_LOG_PRIORITY_NOTICE;
if (!strcasecmp("WARN", name)) return LXC_LOG_PRIORITY_WARN;
if (!strcasecmp("ERROR", name)) return LXC_LOG_PRIORITY_ERROR;
if (!strcasecmp("CRIT", name)) return LXC_LOG_PRIORITY_CRIT;
if (!strcasecmp("ALERT", name)) return LXC_LOG_PRIORITY_ALERT;
if (!strcasecmp("FATAL", name)) return LXC_LOG_PRIORITY_FATAL;
return LXC_LOG_PRIORITY_NOTSET;
}
static inline void
__lxc_log_append(const struct lxc_log_appender *appender,
struct lxc_log_event* event)
{
va_list va, *va_keep;
va_keep = event->vap;
while (appender) {
va_copy(va, *va_keep);
event->vap = &va;
appender->append(appender, event);
appender = appender->next;
va_end(va);
}
}
static inline void
__lxc_log(const struct lxc_log_category* category,
struct lxc_log_event* event)
{
while (category) {
__lxc_log_append(category->appender, event);
category = category->parent;
}
}
/*
* Helper macro to define log fonctions.
*/
#define lxc_log_priority_define(acategory, PRIORITY) \
\
static inline void LXC_##PRIORITY(struct lxc_log_locinfo *, \
const char *, ...) __attribute__ ((format (printf, 2, 3))); \
\
static inline void LXC_##PRIORITY(struct lxc_log_locinfo* locinfo, \
const char* format, ...) \
{ \
if (lxc_log_priority_is_enabled(acategory, \
LXC_LOG_PRIORITY_##PRIORITY)) { \
struct lxc_log_event evt = { \
.category = (acategory)->name, \
.priority = LXC_LOG_PRIORITY_##PRIORITY, \
.fmt = format, \
.locinfo = locinfo \
}; \
va_list va_ref; \
\
gettimeofday(&evt.timestamp, NULL); \
\
va_start(va_ref, format); \
evt.vap = &va_ref; \
__lxc_log(acategory, &evt); \
va_end(va_ref); \
} \
}
/*
* Helper macro to define and use static categories.
*/
#define lxc_log_category_define(name, parent) \
extern struct lxc_log_category lxc_log_category_##parent; \
struct lxc_log_category lxc_log_category_##name = { \
#name, \
LXC_LOG_PRIORITY_NOTSET, \
NULL, \
&lxc_log_category_##parent \
};
#define lxc_log_define(name, parent) \
lxc_log_category_define(name, parent) \
\
lxc_log_priority_define(&lxc_log_category_##name, TRACE) \
lxc_log_priority_define(&lxc_log_category_##name, DEBUG) \
lxc_log_priority_define(&lxc_log_category_##name, INFO) \
lxc_log_priority_define(&lxc_log_category_##name, NOTICE) \
lxc_log_priority_define(&lxc_log_category_##name, WARN) \
lxc_log_priority_define(&lxc_log_category_##name, ERROR) \
lxc_log_priority_define(&lxc_log_category_##name, CRIT) \
lxc_log_priority_define(&lxc_log_category_##name, ALERT) \
lxc_log_priority_define(&lxc_log_category_##name, FATAL)
#define lxc_log_category_priority(name) \
(lxc_log_priority_to_string(lxc_log_category_##name.priority))
/*
* top categories
*/
extern struct lxc_log_category lxc_log_category_lxc;
#define TRACE(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_TRACE(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define DEBUG(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_DEBUG(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define INFO(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_INFO(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define NOTICE(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_NOTICE(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define WARN(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_WARN(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define ERROR(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_ERROR(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define CRIT(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_CRIT(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define ALERT(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_ALERT(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define FATAL(format, ...) do { \
struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT; \
LXC_FATAL(&locinfo, format, ##__VA_ARGS__); \
} while (0)
#define SYSERROR(format, ...) do { \
ERROR("%s - " format, strerror(errno), ##__VA_ARGS__); \
} while (0)
extern int lxc_log_fd;
extern int lxc_log_init(const char *file, const char *priority,
const char *prefix, int quiet);
extern void lxc_log_setprefix(const char *a_prefix);
#endif
|