~ubuntu-branches/ubuntu/oneiric/lxc/oneiric

« back to all changes in this revision

Viewing changes to src/lxc/log.c

  • 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
 * Cedric Le Goater <legoater@free.fr>
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU Lesser General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2.1 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * Lesser General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public
 
20
 * License along with this library; if not, write to the Free Software
 
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
22
 */
 
23
#include <stdio.h>
 
24
#include <errno.h>
 
25
#include <limits.h>
 
26
#include <unistd.h>
 
27
#include <sys/types.h>
 
28
#include <sys/stat.h>
 
29
#include <string.h>
 
30
 
 
31
#define __USE_GNU /* for *_CLOEXEC */
 
32
 
 
33
#include <fcntl.h>
 
34
#include <stdlib.h>
 
35
 
 
36
#include <lxc/log.h>
 
37
 
 
38
#define LXC_LOG_PREFIX_SIZE     32
 
39
#define LXC_LOG_BUFFER_SIZE     512
 
40
 
 
41
int lxc_log_fd = 2;
 
42
static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
 
43
 
 
44
lxc_log_define(lxc_log, lxc);
 
45
 
 
46
/*---------------------------------------------------------------------------*/
 
47
static int log_append_logfile(const struct lxc_log_appender *appender,
 
48
        const struct lxc_log_event *event)
 
49
{
 
50
        char buffer[LXC_LOG_BUFFER_SIZE];
 
51
        int n;
 
52
 
 
53
        if (lxc_log_fd == -1)
 
54
                return 0;
 
55
 
 
56
        n = snprintf(buffer, sizeof(buffer),
 
57
                     "%15s %10ld.%03ld %-8s %s - ",
 
58
                     log_prefix,
 
59
                     event->timestamp.tv_sec,
 
60
                     event->timestamp.tv_usec / 1000,
 
61
                     lxc_log_priority_to_string(event->priority),
 
62
                     event->category);
 
63
 
 
64
        n += vsnprintf(buffer + n, sizeof(buffer) - n, event->fmt,
 
65
                       event->va);
 
66
 
 
67
        if (n >= sizeof(buffer) - 1) {
 
68
                WARN("truncated next event from %d to %d bytes", n,
 
69
                     sizeof(buffer));
 
70
                n = sizeof(buffer) - 1;
 
71
        }
 
72
 
 
73
        buffer[n] = '\n';
 
74
 
 
75
        return write(lxc_log_fd, buffer, n + 1);
 
76
}
 
77
 
 
78
static struct lxc_log_appender log_appender_logfile = {
 
79
        .name           = "logfile",
 
80
        .append         = log_append_logfile,
 
81
        .next           = NULL,
 
82
};
 
83
 
 
84
static struct lxc_log_category log_root = {
 
85
        .name           = "root",
 
86
        .priority       = LXC_LOG_PRIORITY_ERROR,
 
87
        .appender       = NULL,
 
88
        .parent         = NULL,
 
89
};
 
90
 
 
91
struct lxc_log_category lxc_log_category_lxc = {
 
92
        .name           = "lxc",
 
93
        .priority       = LXC_LOG_PRIORITY_ERROR,
 
94
        .appender       = &log_appender_logfile,
 
95
        .parent         = &log_root
 
96
};
 
97
 
 
98
/*---------------------------------------------------------------------------*/
 
99
extern void lxc_log_setprefix(const char *prefix)
 
100
{
 
101
        strncpy(log_prefix, prefix, sizeof(log_prefix));
 
102
        log_prefix[sizeof(log_prefix) - 1] = 0;
 
103
}
 
104
 
 
105
/*---------------------------------------------------------------------------*/
 
106
static int log_open(const char *name)
 
107
{
 
108
        int fd;
 
109
        int newfd;
 
110
 
 
111
        fd = open(name, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0666);
 
112
        if (fd == -1) {
 
113
                ERROR("failed to open log file \"%s\" : %s", name,
 
114
                      strerror(errno));
 
115
                return -1;
 
116
        }
 
117
 
 
118
        if (fd > 2)
 
119
                return fd;
 
120
 
 
121
        newfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
 
122
        if (newfd == -1)
 
123
                ERROR("failed to dup log fd %d : %s", fd, strerror(errno));
 
124
 
 
125
        close(fd);
 
126
        return newfd;
 
127
}
 
128
 
 
129
/*---------------------------------------------------------------------------*/
 
130
extern int lxc_log_init(const char *file, int priority, const char *prefix)
 
131
{
 
132
        lxc_log_category_lxc.priority = priority;
 
133
 
 
134
        if (prefix)
 
135
                lxc_log_setprefix(prefix);
 
136
 
 
137
        if (file) {
 
138
                int fd;
 
139
 
 
140
                fd = log_open(file);
 
141
                if (fd == -1) {
 
142
                        ERROR("failed to initialize log service");
 
143
                        return -1;
 
144
                }
 
145
 
 
146
                lxc_log_fd = fd;
 
147
        }
 
148
 
 
149
        return 0;
 
150
}