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 "system/time.h"
22 #include "system/filesys.h"
23 #include "system/network.h"
27 #include "lib/util/debug.h"
28 #include "lib/util/sys_rw.h"
29 #include "lib/util/time_basic.h"
31 #include "ctdb_private.h"
32 #include "ctdb_client.h"
34 #define CTDB_LOG_FILE_PREFIX "file"
41 log file logging function
43 static void ctdb_log_to_file(void *private_ptr
, int dbglevel
, const char *s
)
45 struct file_state
*state
= talloc_get_type(
46 private_ptr
, struct file_state
);
48 struct timeval_buf tvbuf
;
53 timeval_str_buf(&tv
, false, true, &tvbuf
);
55 ret
= asprintf(&s2
, "%s [%s%5u]: %s\n",
57 debug_extra
, (unsigned)getpid(), s
);
59 const char *errstr
= "asprintf failed\n";
60 sys_write(state
->fd
, errstr
, strlen(errstr
));
64 sys_write(state
->fd
, s2
, strlen(s2
));
69 static int file_state_destructor(struct file_state
*state
)
76 static int ctdb_log_setup_file(TALLOC_CTX
*mem_ctx
,
80 struct file_state
*state
;
84 l
= strlen(CTDB_LOG_FILE_PREFIX
);
85 if (logging
[l
] != ':') {
88 logfile
= &logging
[0] + l
+ 1;
90 state
= talloc_zero(mem_ctx
, struct file_state
);
95 if (logfile
== NULL
|| strcmp(logfile
, "-") == 0) {
99 /* also catch stderr of subcommands to stdout */
105 state
->fd
= open(logfile
, O_WRONLY
|O_APPEND
|O_CREAT
, 0666);
106 if (state
->fd
== -1) {
111 talloc_set_destructor(state
, file_state_destructor
);
112 debug_set_callback(state
, ctdb_log_to_file
);
117 void ctdb_log_init_file(void)
119 ctdb_log_register_backend(CTDB_LOG_FILE_PREFIX
, ctdb_log_setup_file
);