4 Copyright (C) Andrew Tridgell 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "lib/events/events.h"
22 #include "../include/ctdb_private.h"
23 #include "system/syslog.h"
24 #include "system/time.h"
25 #include "system/filesys.h"
27 struct ctdb_log_state {
34 /* we need this global to eep the DEBUG() syntax */
35 static struct ctdb_log_state *log_state;
38 syslog logging function
40 static void ctdb_syslog_log(const char *format, va_list ap)
42 int level = LOG_DEBUG;
43 switch (this_log_level) {
69 vsyslog(level, format, ap);
74 log file logging function
76 static void ctdb_logfile_log(const char *format, va_list ap)
85 ret = vasprintf(&s, format, ap);
87 const char *errstr = "vasprintf failed\n";
89 write(log_state->fd, errstr, strlen(errstr));
93 t = timeval_current();
94 tm = localtime(&t.tv_sec);
96 strftime(tbuf,sizeof(tbuf)-1,"%Y/%m/%d %H:%M:%S", tm);
98 ret = asprintf(&s2, "%s.%06u [%5u]: %s",
99 tbuf, (unsigned)t.tv_usec, (unsigned)getpid(), s);
102 const char *errstr = "asprintf failed\n";
103 write(log_state->fd, errstr, strlen(errstr));
107 write(log_state->fd, s2, strlen(s2));
112 static void ctdb_logfile_log_add(const char *format, va_list ap)
117 ret = vasprintf(&s, format, ap);
119 const char *errstr = "vasprintf failed\n";
121 write(log_state->fd, errstr, strlen(errstr));
126 write(log_state->fd, s, strlen(s));
132 choose the logfile location
134 int ctdb_set_logfile(struct ctdb_context *ctdb, const char *logfile, bool use_syslog)
137 ctdb->log = talloc_zero(ctdb, struct ctdb_log_state);
138 if (ctdb->log == NULL) {
139 printf("talloc_zero failed\n");
143 log_state = ctdb->log;
146 do_debug_v = ctdb_syslog_log;
147 do_debug_add_v = ctdb_syslog_log;
148 ctdb->log->use_syslog = true;
149 } else if (logfile == NULL || strcmp(logfile, "-") == 0) {
150 do_debug_v = ctdb_logfile_log;
151 do_debug_add_v = ctdb_logfile_log_add;
153 /* also catch stderr of subcommands to stdout */
156 printf("dup2 failed: %s\n", strerror(errno));
160 do_debug_v = ctdb_logfile_log;
161 do_debug_add_v = ctdb_logfile_log_add;
163 ctdb->log->fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666);
164 if (ctdb->log->fd == -1) {
165 printf("Failed to open logfile %s\n", logfile);
176 called when log data comes in from a child process
178 static void ctdb_log_handler(struct event_context *ev, struct fd_event *fde,
179 uint16_t flags, void *private)
181 struct ctdb_context *ctdb = talloc_get_type(private, struct ctdb_context);
185 if (!(flags & EVENT_FD_READ)) {
189 n = read(ctdb->log->pfd, &ctdb->log->buf[ctdb->log->buf_used],
190 sizeof(ctdb->log->buf) - ctdb->log->buf_used);
192 ctdb->log->buf_used += n;
195 this_log_level = script_log_level;
197 while (ctdb->log->buf_used > 0 &&
198 (p = memchr(ctdb->log->buf, '\n', ctdb->log->buf_used)) != NULL) {
199 int n1 = (p - ctdb->log->buf)+1;
201 /* swallow \r from child processes */
202 if (n2 > 0 && ctdb->log->buf[n2-1] == '\r') {
205 if (script_log_level <= LogLevel) {
206 do_debug("%*.*s\n", n2, n2, ctdb->log->buf);
207 /* log it in the eventsystem as well */
208 ctdb_log_event_script_output(ctdb, ctdb->log->buf, n2);
210 memmove(ctdb->log->buf, p+1, sizeof(ctdb->log->buf) - n1);
211 ctdb->log->buf_used -= n1;
214 /* the buffer could have completely filled - unfortunately we have
215 no choice but to dump it out straight away */
216 if (ctdb->log->buf_used == sizeof(ctdb->log->buf)) {
217 if (script_log_level <= LogLevel) {
219 (int)ctdb->log->buf_used, (int)ctdb->log->buf_used, ctdb->log->buf);
220 /* log it in the eventsystem as well */
221 ctdb_log_event_script_output(ctdb, ctdb->log->buf, ctdb->log->buf_used);
223 ctdb->log->buf_used = 0;
230 setup for logging of child process stdout
232 int ctdb_set_child_logging(struct ctdb_context *ctdb)
237 if (ctdb->log->fd == 1) {
238 /* not needed for stdout logging */
242 /* setup a pipe to catch IO from subprocesses */
244 DEBUG(DEBUG_ERR,(__location__ " Failed to setup for child logging pipe\n"));
248 event_add_fd(ctdb->ev, ctdb, p[0], EVENT_FD_READ,
249 ctdb_log_handler, ctdb);
250 set_close_on_exec(p[0]);
251 ctdb->log->pfd = p[0];
258 printf("dup2 failed: %s\n", strerror(errno));
263 /* also catch stderr of subcommands to the log */
266 printf("dup2 failed: %s\n", strerror(errno));