]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.c
Revert "use a default per-container logfile"
[mirror_lxc.git] / src / lxc / log.c
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 "log.h"
37 #include "caps.h"
38
39 #define LXC_LOG_PREFIX_SIZE 32
40 #define LXC_LOG_BUFFER_SIZE 512
41
42 int lxc_log_fd = -1;
43 static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
44 static int lxc_loglevel_specified = 0;
45
46 lxc_log_define(lxc_log, lxc);
47
48 /*---------------------------------------------------------------------------*/
49 static int log_append_stderr(const struct lxc_log_appender *appender,
50 struct lxc_log_event *event)
51 {
52 if (event->priority < LXC_LOG_PRIORITY_ERROR)
53 return 0;
54
55 fprintf(stderr, "%s: ", log_prefix);
56 vfprintf(stderr, event->fmt, *event->vap);
57 fprintf(stderr, "\n");
58 return 0;
59 }
60
61 /*---------------------------------------------------------------------------*/
62 static int log_append_logfile(const struct lxc_log_appender *appender,
63 struct lxc_log_event *event)
64 {
65 char buffer[LXC_LOG_BUFFER_SIZE];
66 int n;
67
68 if (lxc_log_fd == -1)
69 return 0;
70
71 n = snprintf(buffer, sizeof(buffer),
72 "%15s %10ld.%03ld %-8s %s - ",
73 log_prefix,
74 event->timestamp.tv_sec,
75 event->timestamp.tv_usec / 1000,
76 lxc_log_priority_to_string(event->priority),
77 event->category);
78
79 n += vsnprintf(buffer + n, sizeof(buffer) - n, event->fmt,
80 *event->vap);
81
82 if (n >= sizeof(buffer) - 1) {
83 WARN("truncated next event from %d to %zd bytes", n,
84 sizeof(buffer));
85 n = sizeof(buffer) - 1;
86 }
87
88 buffer[n] = '\n';
89
90 return write(lxc_log_fd, buffer, n + 1);
91 }
92
93 static struct lxc_log_appender log_appender_stderr = {
94 .name = "stderr",
95 .append = log_append_stderr,
96 .next = NULL,
97 };
98
99 static struct lxc_log_appender log_appender_logfile = {
100 .name = "logfile",
101 .append = log_append_logfile,
102 .next = NULL,
103 };
104
105 static struct lxc_log_category log_root = {
106 .name = "root",
107 .priority = LXC_LOG_PRIORITY_ERROR,
108 .appender = NULL,
109 .parent = NULL,
110 };
111
112 struct lxc_log_category lxc_log_category_lxc = {
113 .name = "lxc",
114 .priority = LXC_LOG_PRIORITY_ERROR,
115 .appender = &log_appender_stderr,
116 .parent = &log_root
117 };
118
119 /*---------------------------------------------------------------------------*/
120 extern void lxc_log_setprefix(const char *prefix)
121 {
122 strncpy(log_prefix, prefix, sizeof(log_prefix));
123 log_prefix[sizeof(log_prefix) - 1] = 0;
124 }
125
126 /*---------------------------------------------------------------------------*/
127 static int log_open(const char *name)
128 {
129 int fd;
130 int newfd;
131
132 fd = lxc_unpriv(open(name, O_CREAT | O_WRONLY |
133 O_APPEND | O_CLOEXEC, 0666));
134 if (fd == -1) {
135 ERROR("failed to open log file \"%s\" : %s", name,
136 strerror(errno));
137 return -1;
138 }
139
140 if (fd > 2)
141 return fd;
142
143 newfd = fcntl(fd, F_DUPFD_CLOEXEC, 3);
144 if (newfd == -1)
145 ERROR("failed to dup log fd %d : %s", fd, strerror(errno));
146
147 close(fd);
148 return newfd;
149 }
150
151 /*---------------------------------------------------------------------------*/
152 extern int lxc_log_init(const char *file, const char *priority,
153 const char *prefix, int quiet)
154 {
155 int lxc_priority = LXC_LOG_PRIORITY_ERROR;
156
157 if (lxc_log_fd != -1)
158 return 0;
159
160 if (priority) {
161 lxc_loglevel_specified = 1;
162 lxc_priority = lxc_log_priority_to_int(priority);
163
164 if (lxc_priority == LXC_LOG_PRIORITY_NOTSET) {
165 ERROR("invalid log priority %s", priority);
166 return -1;
167 }
168 }
169
170 lxc_log_category_lxc.priority = lxc_priority;
171 lxc_log_category_lxc.appender = &log_appender_logfile;
172
173 if (!quiet)
174 lxc_log_category_lxc.appender->next = &log_appender_stderr;
175
176 if (prefix)
177 lxc_log_setprefix(prefix);
178
179 if (file)
180 return lxc_log_set_file(file);
181
182 return 0;
183 }
184
185 /*
186 * This is called when we read a lxc.loglevel entry in a lxc.conf file. This
187 * happens after processing command line arguments, which override the .conf
188 * settings. So only set the level if previously unset.
189 */
190 extern int lxc_log_set_level(int level)
191 {
192 if (lxc_loglevel_specified)
193 return 0;
194 if (level < 0 || level >= LXC_LOG_PRIORITY_NOTSET) {
195 ERROR("invalid log priority %d", level);
196 return -1;
197 }
198 lxc_log_category_lxc.priority = level;
199 return 0;
200 }
201
202 char *log_fname; // default to NULL, set in lxc_log_set_file.
203
204 /*
205 * This is called when we read a lxc.logfile entry in a lxc.conf file. This
206 * happens after processing command line arguments, which override the .conf
207 * settings. So only set the logfile if previously unset.
208 */
209 extern int lxc_log_set_file(const char *fname)
210 {
211 if (lxc_log_fd != -1) {
212 // this should've been caught at config_logfile.
213 ERROR("Race in setting logfile?");
214 return -1;
215 }
216
217 lxc_log_fd = log_open(fname);
218 if (lxc_log_fd == -1) {
219 ERROR("failed to open log file %s\n", fname);
220 return -1;
221 }
222
223 log_fname = strdup(fname);
224 return 0;
225 }
226
227 extern int lxc_log_get_level(void)
228 {
229 if (!lxc_loglevel_specified)
230 return LXC_LOG_PRIORITY_NOTSET;
231 return lxc_log_category_lxc.priority;
232 }
233
234 extern const char *lxc_log_get_file(void)
235 {
236 return log_fname;
237 }