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