ldb-samba: Implement transitive extended matching
[obnox/samba/samba-obnox.git] / ctdb / server / ctdb_logging_file.c
1 /*
2    ctdb logging code
3
4    Copyright (C) Andrew Tridgell  2008
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "../include/ctdb_client.h"
22 #include "../include/ctdb_private.h"
23 #include "system/time.h"
24 #include "system/filesys.h"
25 #include "lib/util/time_basic.h"
26
27 #define CTDB_LOG_FILE_PREFIX "file"
28
29 struct file_state {
30         int fd;
31 };
32
33 /*
34   log file logging function
35  */
36 static void ctdb_log_to_file(void *private_ptr, int dbglevel, const char *s)
37 {
38         struct file_state *state = talloc_get_type(
39                 private_ptr, struct file_state);
40         struct timeval tv;
41         struct timeval_buf tvbuf;
42         char *s2 = NULL;
43         int ret;
44
45         GetTimeOfDay(&tv);
46         timeval_str_buf(&tv, false, true, &tvbuf);
47
48         ret = asprintf(&s2, "%s [%s%5u]: %s\n",
49                        tvbuf.buf,
50                        debug_extra, (unsigned)getpid(), s);
51         if (ret == -1) {
52                 const char *errstr = "asprintf failed\n";
53                 sys_write(state->fd, errstr, strlen(errstr));
54                 return;
55         }
56         if (s2) {
57                 sys_write(state->fd, s2, strlen(s2));
58                 free(s2);
59         }
60 }
61
62 static int file_state_destructor(struct file_state *state)
63 {
64        close(state->fd);
65        state->fd = -1;
66        return 0;
67 }
68
69 static int ctdb_log_setup_file(TALLOC_CTX *mem_ctx,
70                                const char *logging,
71                                const char *app_name)
72 {
73         struct file_state *state;
74         const char *logfile;
75         size_t l;
76
77         l = strlen(CTDB_LOG_FILE_PREFIX);
78         if (logging[l] != ':') {
79                 return EINVAL;
80         }
81         logfile = &logging[0] + l + 1;
82
83         state = talloc_zero(mem_ctx, struct file_state);
84         if (state == NULL) {
85                 return ENOMEM;
86         }
87
88         if (logfile == NULL || strcmp(logfile, "-") == 0) {
89                 int ret;
90
91                 state->fd = 1;
92                 /* also catch stderr of subcommands to stdout */
93                 ret = dup2(1, 2);
94                 if (ret == -1) {
95                         return errno;
96                 }
97         } else {
98                 state->fd = open(logfile, O_WRONLY|O_APPEND|O_CREAT, 0666);
99                 if (state->fd == -1) {
100                         return errno;
101                 }
102         }
103
104         talloc_set_destructor(state, file_state_destructor);
105         debug_set_callback(state, ctdb_log_to_file);
106
107         return 0;
108 }
109
110 void ctdb_log_init_file(void)
111 {
112         ctdb_log_register_backend(CTDB_LOG_FILE_PREFIX, ctdb_log_setup_file);
113 }