122
125
log_prefix[sizeof(log_prefix) - 1] = 0;
128
static int build_dir(const char *name)
130
char *n = strdup(name); // because we'll be modifying it
135
ERROR("Out of memory while creating directory '%s'.", name);
140
for (p = n+1; p < e; p++) {
144
if (access(n, F_OK)) {
145
ret = lxc_unpriv(mkdir(n, 0755));
146
if (ret && errno != -EEXIST) {
147
SYSERROR("failed to create directory '%s'.", n);
125
158
/*---------------------------------------------------------------------------*/
126
159
static int log_open(const char *name)
183
static char *build_log_path(const char *name)
189
* '$logpath' + '/' + '$name' + '.log' + '\0'
191
* '$logpath' + '/' + '$name' + '/' + '$name' + '.log' + '\0'
192
* sizeof(LOGPATH) includes its \0
194
len = sizeof(LOGPATH) + strlen(name) + 6;
195
#if USE_CONFIGPATH_LOGS
196
len += strlen(name) + 1; /* add "/$container_name/" */
201
#if USE_CONFIGPATH_LOGS
202
ret = snprintf(p, len, "%s/%s/%s.log", LOGPATH, name, name);
204
ret = snprintf(p, len, "%s/%s.log", LOGPATH, name);
206
if (ret < 0 || ret >= len) {
213
int do_lxc_log_set_file(const char *fname, int from_default);
150
215
/*---------------------------------------------------------------------------*/
151
extern int lxc_log_init(const char *file, const char *priority,
152
const char *prefix, int quiet)
216
extern int lxc_log_init(const char *name, const char *file,
217
const char *priority, const char *prefix, int quiet)
154
219
int lxc_priority = LXC_LOG_PRIORITY_ERROR;
221
char *tmpfile = NULL;
222
int want_lxc_log_specified = 0;
224
if (lxc_log_fd != -1)
228
lxc_loglevel_specified = 1;
157
229
lxc_priority = lxc_log_priority_to_int(priority);
159
231
if (lxc_priority == LXC_LOG_PRIORITY_NOTSET) {
172
244
lxc_log_setprefix(prefix);
246
if (file && strcmp(file, "none") == 0) {
247
want_lxc_log_specified = 1;
179
ERROR("failed to initialize log service");
252
tmpfile = build_log_path(name);
254
ERROR("could not build log path");
258
want_lxc_log_specified = 1;
261
ret = do_lxc_log_set_file(tmpfile ? tmpfile : file, !want_lxc_log_specified);
263
if (want_lxc_log_specified)
264
lxc_log_specified = 1;
266
* If !want_lxc_log_specified, that is, if the user did not request
267
* this logpath, then ignore failures and continue logging to console
269
if (!want_lxc_log_specified && ret != 0) {
270
INFO("Ignoring failure to open default logfile.");
281
* This is called when we read a lxc.loglevel entry in a lxc.conf file. This
282
* happens after processing command line arguments, which override the .conf
283
* settings. So only set the level if previously unset.
285
extern int lxc_log_set_level(int level)
287
if (lxc_loglevel_specified)
289
if (level < 0 || level >= LXC_LOG_PRIORITY_NOTSET) {
290
ERROR("invalid log priority %d", level);
293
lxc_log_category_lxc.priority = level;
297
char *log_fname; // default to NULL, set in lxc_log_set_file.
299
* This can be called:
300
* 1. when a program calls lxc_log_init with no logfile parameter (in which
301
* case the default is used). In this case lxc.logfile can override this.
302
* 2. when a program calls lxc_log_init with a logfile parameter. In this
303
* case we don't want lxc.logfile to override this.
304
* 3. When a lxc.logfile entry is found in config file.
306
int do_lxc_log_set_file(const char *fname, int from_default)
308
if (lxc_log_specified) {
309
INFO("lxc.logfile overriden by command line");
312
if (lxc_log_fd != -1) {
313
// we are overriding the default.
318
#if USE_CONFIGPATH_LOGS
319
// we don't build_dir for the default if the default is
320
// i.e. /var/lib/lxc/$container/$container.log
323
if (build_dir(fname)) {
324
ERROR("failed to create dir for log file \"%s\" : %s", fname,
329
lxc_log_fd = log_open(fname);
330
if (lxc_log_fd == -1)
333
log_fname = strdup(fname);
337
extern int lxc_log_set_file(const char *fname)
339
return do_lxc_log_set_file(fname, 0);
342
extern int lxc_log_get_level(void)
344
if (!lxc_loglevel_specified)
345
return LXC_LOG_PRIORITY_NOTSET;
346
return lxc_log_category_lxc.priority;
349
extern const char *lxc_log_get_file(void)