]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/log.c
properly handle va_list in log_append functions
[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 <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 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->vap);
66
67 if (n >= sizeof(buffer) - 1) {
68 WARN("truncated next event from %d to %zd 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, const char *priority,
131 const char *prefix)
132 {
133 int lxc_priority = LXC_LOG_PRIORITY_ERROR;
134
135 if (priority) {
136 lxc_priority = lxc_log_priority_to_int(priority);
137
138 if (lxc_priority == LXC_LOG_PRIORITY_NOTSET) {
139 ERROR("invalid log priority %s", priority);
140 return -1;
141 }
142 }
143
144 lxc_log_category_lxc.priority = lxc_priority;
145
146 if (prefix)
147 lxc_log_setprefix(prefix);
148
149 if (file) {
150 int fd;
151
152 fd = log_open(file);
153 if (fd == -1) {
154 ERROR("failed to initialize log service");
155 return -1;
156 }
157
158 lxc_log_fd = fd;
159 }
160
161 return 0;
162 }