2 * lxc: linux Container library
4 * (C) Copyright IBM Corp. 2007, 2008
7 * Cedric Le Goater <legoater@free.fr>
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.
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.
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
27 #include <sys/types.h>
31 #define __USE_GNU /* for *_CLOEXEC */
38 #define LXC_LOG_PREFIX_SIZE 32
39 #define LXC_LOG_BUFFER_SIZE 512
42 static char log_prefix
[LXC_LOG_PREFIX_SIZE
] = "lxc";
44 lxc_log_define(lxc_log
, lxc
);
46 /*---------------------------------------------------------------------------*/
47 static int log_append_logfile(const struct lxc_log_appender
*appender
,
48 struct lxc_log_event
*event
)
50 char buffer
[LXC_LOG_BUFFER_SIZE
];
56 n
= snprintf(buffer
, sizeof(buffer
),
57 "%15s %10ld.%03ld %-8s %s - ",
59 event
->timestamp
.tv_sec
,
60 event
->timestamp
.tv_usec
/ 1000,
61 lxc_log_priority_to_string(event
->priority
),
64 n
+= vsnprintf(buffer
+ n
, sizeof(buffer
) - n
, event
->fmt
,
67 if (n
>= sizeof(buffer
) - 1) {
68 WARN("truncated next event from %d to %zd bytes", n
,
70 n
= sizeof(buffer
) - 1;
75 return write(lxc_log_fd
, buffer
, n
+ 1);
78 static struct lxc_log_appender log_appender_logfile
= {
80 .append
= log_append_logfile
,
84 static struct lxc_log_category log_root
= {
86 .priority
= LXC_LOG_PRIORITY_ERROR
,
91 struct lxc_log_category lxc_log_category_lxc
= {
93 .priority
= LXC_LOG_PRIORITY_ERROR
,
94 .appender
= &log_appender_logfile
,
98 /*---------------------------------------------------------------------------*/
99 extern void lxc_log_setprefix(const char *prefix
)
101 strncpy(log_prefix
, prefix
, sizeof(log_prefix
));
102 log_prefix
[sizeof(log_prefix
) - 1] = 0;
105 /*---------------------------------------------------------------------------*/
106 static int log_open(const char *name
)
111 fd
= open(name
, O_CREAT
| O_WRONLY
| O_APPEND
| O_CLOEXEC
, 0666);
113 ERROR("failed to open log file \"%s\" : %s", name
,
121 newfd
= fcntl(fd
, F_DUPFD_CLOEXEC
, 3);
123 ERROR("failed to dup log fd %d : %s", fd
, strerror(errno
));
129 /*---------------------------------------------------------------------------*/
130 extern int lxc_log_init(const char *file
, const char *priority
,
133 int lxc_priority
= LXC_LOG_PRIORITY_ERROR
;
136 lxc_priority
= lxc_log_priority_to_int(priority
);
138 if (lxc_priority
== LXC_LOG_PRIORITY_NOTSET
) {
139 ERROR("invalid log priority %s", priority
);
144 lxc_log_category_lxc
.priority
= lxc_priority
;
147 lxc_log_setprefix(prefix
);
154 ERROR("failed to initialize log service");