~serge-hallyn/ubuntu/natty/lxc/lxc-clone

« back to all changes in this revision

Viewing changes to src/lxc/log.h

  • Committer: Bazaar Package Importer
  • Author(s): Guido Trotter
  • Date: 2009-04-29 17:49:13 UTC
  • Revision ID: james.westby@ubuntu.com-20090429174913-jvahs1ykizqtodje
Tags: upstream-0.6.2
ImportĀ upstreamĀ versionĀ 0.6.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * lxc: linux Container library
 
3
 *
 
4
 * (C) Copyright IBM Corp. 2007, 2008
 
5
 *
 
6
 * Authors:
 
7
 * Daniel Lezcano <dlezcano at fr.ibm.com>
 
8
 * Cedric Le Goater <legoater@free.fr>
 
9
 *
 
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.
 
14
 *
 
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.
 
19
 *
 
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
23
 */
 
24
#ifndef _log_h
 
25
#define _log_h
 
26
 
 
27
#include <stdarg.h>
 
28
#include <stdio.h>
 
29
#include <sys/time.h>
 
30
#include <string.h>
 
31
 
 
32
#ifndef O_CLOEXEC
 
33
#define O_CLOEXEC 02000000
 
34
#endif
 
35
 
 
36
#ifndef F_DUPFD_CLOEXEC
 
37
#define F_DUPFD_CLOEXEC 1030
 
38
#endif
 
39
 
 
40
#define LXC_LOG_PREFIX_SIZE     32
 
41
#define LXC_LOG_BUFFER_SIZE     512
 
42
 
 
43
/* predefined priorities. */
 
44
enum {
 
45
        LXC_LOG_PRIORITY_TRACE,
 
46
        LXC_LOG_PRIORITY_DEBUG,
 
47
        LXC_LOG_PRIORITY_INFO,
 
48
        LXC_LOG_PRIORITY_NOTICE,
 
49
        LXC_LOG_PRIORITY_WARN,
 
50
        LXC_LOG_PRIORITY_ERROR,
 
51
        LXC_LOG_PRIORITY_CRIT,
 
52
        LXC_LOG_PRIORITY_ALERT,
 
53
        LXC_LOG_PRIORITY_FATAL,
 
54
        LXC_LOG_PRIORITY_NOTSET,
 
55
};
 
56
 
 
57
/* location information of the logging event */
 
58
struct lxc_log_locinfo {
 
59
        const char      *file;
 
60
        const char      *func;
 
61
        int             line;
 
62
};
 
63
 
 
64
#define LXC_LOG_LOCINFO_INIT                                            \
 
65
        { .file = __FILE__, .func = __func__, .line = __LINE__  }
 
66
 
 
67
/* brief logging event object */
 
68
struct lxc_log_event {
 
69
        const char*             category;
 
70
        int                     priority;
 
71
        struct timeval          timestamp;
 
72
        struct lxc_log_locinfo  *locinfo;
 
73
        const char              *fmt;
 
74
        va_list                 va;
 
75
};
 
76
 
 
77
/* log appender object */
 
78
struct lxc_log_appender {
 
79
        const char*     name;
 
80
        int (*append)(const struct lxc_log_appender *,
 
81
                      const struct lxc_log_event *);
 
82
 
 
83
        /*
 
84
         * appenders can be stacked
 
85
         */
 
86
        struct lxc_log_appender *next;
 
87
};
 
88
 
 
89
/* log category object */
 
90
struct lxc_log_category {
 
91
        const char                      *name;
 
92
        int                             priority;
 
93
        struct lxc_log_appender         *appender;
 
94
        const struct lxc_log_category   *parent;
 
95
};
 
96
 
 
97
/*
 
98
 * Returns true if the chained priority is equal to or higher than
 
99
 * given priority.
 
100
 */
 
101
static inline int
 
102
lxc_log_priority_is_enabled(const struct lxc_log_category* category,
 
103
                           int priority)
 
104
{
 
105
        while (category->priority == LXC_LOG_PRIORITY_NOTSET &&
 
106
               category->parent)
 
107
                category = category->parent;
 
108
 
 
109
        return priority >= category->priority;
 
110
}
 
111
 
 
112
/*
 
113
 * converts a priority to a literal string
 
114
 */
 
115
static inline const char* lxc_log_priority_to_string(int priority)
 
116
{
 
117
        switch (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";
 
127
        default:
 
128
                return "NOTSET";
 
129
        }
 
130
}
 
131
/*
 
132
 * converts a literal priority to an int
 
133
 */
 
134
static inline int lxc_log_priority_to_int(const char* name)
 
135
{
 
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;
 
145
 
 
146
        return LXC_LOG_PRIORITY_NOTSET;
 
147
}
 
148
 
 
149
static inline void
 
150
__lxc_log_append(const struct lxc_log_appender *appender,
 
151
                const struct lxc_log_event* event)
 
152
{
 
153
        while (appender) {
 
154
                appender->append(appender, event);
 
155
                appender = appender->next;
 
156
        }
 
157
}
 
158
 
 
159
static inline void
 
160
__lxc_log(const struct lxc_log_category* category,
 
161
         const struct lxc_log_event* event)
 
162
{
 
163
        while (category) {
 
164
                __lxc_log_append(category->appender, event);
 
165
                category = category->parent;
 
166
        }
 
167
}
 
168
 
 
169
/*
 
170
 * Helper macro to define log fonctions.
 
171
 */
 
172
#define lxc_log_priority_define(acategory, PRIORITY)                    \
 
173
                                                                        \
 
174
static inline void LXC_##PRIORITY(struct lxc_log_locinfo *,             \
 
175
        const char *, ...) __attribute__ ((format (printf, 2, 3)));     \
 
176
                                                                        \
 
177
static inline void LXC_##PRIORITY(struct lxc_log_locinfo* locinfo,      \
 
178
                                  const char* format, ...)              \
 
179
{                                                                       \
 
180
        if (lxc_log_priority_is_enabled(acategory,                      \
 
181
                                        LXC_LOG_PRIORITY_##PRIORITY)) { \
 
182
                struct lxc_log_event evt = {                            \
 
183
                        .category       = (acategory)->name,            \
 
184
                        .priority       = LXC_LOG_PRIORITY_##PRIORITY,  \
 
185
                        .fmt            = format,                       \
 
186
                        .locinfo        = locinfo                       \
 
187
                };                                                      \
 
188
                                                                        \
 
189
                gettimeofday(&evt.timestamp, NULL);                     \
 
190
                                                                        \
 
191
                va_start(evt.va, format);                               \
 
192
                __lxc_log(acategory, &evt);                             \
 
193
                va_end(evt.va);                                         \
 
194
        }                                                               \
 
195
}
 
196
 
 
197
/*
 
198
 * Helper macro to define and use static categories.
 
199
 */
 
200
#define lxc_log_category_define(name, parent)                           \
 
201
        extern struct lxc_log_category lxc_log_category_##parent;       \
 
202
        struct lxc_log_category lxc_log_category_##name = {             \
 
203
                #name,                                                  \
 
204
                LXC_LOG_PRIORITY_NOTSET,                                \
 
205
                NULL,                                                   \
 
206
                &lxc_log_category_##parent                              \
 
207
        };
 
208
 
 
209
#define lxc_log_define(name, parent)                                    \
 
210
        lxc_log_category_define(name, parent)                           \
 
211
                                                                        \
 
212
        lxc_log_priority_define(&lxc_log_category_##name, TRACE)        \
 
213
        lxc_log_priority_define(&lxc_log_category_##name, DEBUG)        \
 
214
        lxc_log_priority_define(&lxc_log_category_##name, INFO)         \
 
215
        lxc_log_priority_define(&lxc_log_category_##name, NOTICE)       \
 
216
        lxc_log_priority_define(&lxc_log_category_##name, WARN)         \
 
217
        lxc_log_priority_define(&lxc_log_category_##name, ERROR)        \
 
218
        lxc_log_priority_define(&lxc_log_category_##name, CRIT)         \
 
219
        lxc_log_priority_define(&lxc_log_category_##name, ALERT)        \
 
220
        lxc_log_priority_define(&lxc_log_category_##name, FATAL)
 
221
 
 
222
#define lxc_log_category_priority(name)                                 \
 
223
        (lxc_log_priority_to_string(lxc_log_category_##name.priority))
 
224
 
 
225
/*
 
226
 * top categories
 
227
 */
 
228
extern struct lxc_log_category lxc_log_category_lxc;
 
229
 
 
230
#define TRACE(format, ...) do {                                         \
 
231
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
232
        LXC_TRACE(&locinfo, format, ##__VA_ARGS__);                     \
 
233
} while (0)
 
234
 
 
235
#define DEBUG(format, ...) do {                                         \
 
236
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
237
        LXC_DEBUG(&locinfo, format, ##__VA_ARGS__);                     \
 
238
} while (0)
 
239
 
 
240
#define INFO(format, ...) do {                                          \
 
241
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
242
        LXC_INFO(&locinfo, format, ##__VA_ARGS__);                      \
 
243
} while (0)
 
244
 
 
245
#define NOTICE(format, ...) do {                                        \
 
246
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
247
        LXC_NOTICE(&locinfo, format, ##__VA_ARGS__);                    \
 
248
} while (0)
 
249
 
 
250
#define WARN(format, ...) do {                                          \
 
251
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
252
        LXC_WARN(&locinfo, format, ##__VA_ARGS__);                      \
 
253
} while (0)
 
254
 
 
255
#define ERROR(format, ...) do {                                         \
 
256
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
257
        LXC_ERROR(&locinfo, format, ##__VA_ARGS__);                     \
 
258
} while (0)
 
259
 
 
260
#define CRIT(format, ...) do {                                          \
 
261
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
262
        LXC_CRIT(&locinfo, format, ##__VA_ARGS__);                      \
 
263
} while (0)
 
264
 
 
265
#define ALERT(format, ...) do {                                         \
 
266
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
267
        LXC_ALERT(&locinfo, format, ##__VA_ARGS__);                     \
 
268
} while (0)
 
269
 
 
270
#define FATAL(format, ...) do {                                         \
 
271
        struct lxc_log_locinfo locinfo = LXC_LOG_LOCINFO_INIT;          \
 
272
        LXC_FATAL(&locinfo, format, ##__VA_ARGS__);                     \
 
273
} while (0)
 
274
 
 
275
 
 
276
 
 
277
#define SYSERROR(format, ...) do {                                      \
 
278
        ERROR("%s - " format "\n", strerror(errno), ##__VA_ARGS__);     \
 
279
} while (0)
 
280
 
 
281
#endif