added "log format" option to allow admins to choose the format for
[rsync/rsync.git] / log.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1998
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17*/
18
19/*
20 logging and utility functions
21
22 tridge, May 1998
23 */
24#include "rsync.h"
25
26static FILE *logfile;
27
28
29/****************************************************************************
30 return the date and time as a string
31****************************************************************************/
32static char *timestring(void )
33{
34 static char TimeBuf[200];
35 time_t t = time(NULL);
36 struct tm *tm = localtime(&t);
37
38#ifdef HAVE_STRFTIME
39 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
40#else
41 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf)-1);
42#endif
43
44 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
45 TimeBuf[strlen(TimeBuf)-1] = 0;
46 }
47
48 return(TimeBuf);
49}
50
51static void logit(int priority, char *buf)
52{
53 if (logfile) {
54 fprintf(logfile,"%s [%d] %s",
55 timestring(), (int)getpid(), buf);
56 fflush(logfile);
57 } else {
58 syslog(priority, "%s", buf);
59 }
60}
61
62void log_open(void)
63{
64 static int initialised;
65 int options = LOG_PID;
66 time_t t;
67 char *logf;
68
69 if (initialised) return;
70 initialised = 1;
71
72 logf = lp_log_file();
73 if (logf && *logf) {
74 logfile = fopen(logf, "a");
75 return;
76 }
77
78#ifdef LOG_NDELAY
79 options |= LOG_NDELAY;
80#endif
81
82#ifdef LOG_DAEMON
83 openlog("rsyncd", options, lp_syslog_facility());
84#else
85 openlog("rsyncd", options);
86#endif
87
88#ifndef LOG_NDELAY
89 logit(LOG_INFO,"rsyncd started\n");
90#endif
91
92 /* this looks pointless, but it is needed in order for the
93 C library on some systems to fetch the timezone info
94 before the chroot */
95 t = time(NULL);
96 localtime(&t);
97}
98
99
100/* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
101 void rprintf(int fd, const char *format, ...)
102{
103 va_list ap;
104 char buf[1024];
105 int len;
106 FILE *f=NULL;
107 extern int am_daemon;
108 /* recursion can happen with certain fatal conditions */
109 static int depth;
110
111 if (depth) return;
112
113 depth++;
114
115 va_start(ap, format);
116 len = vslprintf(buf, sizeof(buf)-1, format, ap);
117 va_end(ap);
118
119 if (len < 0) exit_cleanup(1);
120
121 if (len > sizeof(buf)-1) exit_cleanup(1);
122
123 buf[len] = 0;
124
125 if (fd == FLOG) {
126 if (am_daemon) logit(LOG_INFO, buf);
127 depth--;
128 return;
129 }
130
131 if (am_daemon) {
132 int priority = LOG_INFO;
133 if (fd == FERROR) priority = LOG_WARNING;
134
135 log_open();
136 if (!io_multiplex_write(fd, buf, strlen(buf))) {
137 logit(priority, buf);
138 }
139
140 depth--;
141 return;
142 }
143
144 if (fd == FERROR) {
145 f = stderr;
146 }
147
148 if (fd == FINFO) {
149 extern int am_server;
150 if (am_server)
151 f = stderr;
152 else
153 f = stdout;
154 }
155
156 if (!f) exit_cleanup(1);
157
158 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
159
160 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
161
162 depth--;
163}
164
165void rflush(int fd)
166{
167 FILE *f = NULL;
168 extern int am_daemon;
169
170 if (am_daemon) {
171 return;
172 }
173
174 if (fd == FLOG) {
175 return;
176 }
177
178 if (fd == FERROR) {
179 f = stderr;
180 }
181
182 if (fd == FINFO) {
183 extern int am_server;
184 if (am_server)
185 f = stderr;
186 else
187 f = stdout;
188 }
189
190 if (!f) exit_cleanup(1);
191 fflush(f);
192}
193
194
195
196/* a generic logging routine for send/recv, with parameter
197 substitiution */
198static void log_formatted(char *op, struct file_struct *file)
199{
200 extern int module_id;
201 char buf[1024];
202 char *p, *s, *n;
203 char buf2[100];
204 int l;
205
206 strlcpy(buf, lp_log_format(module_id), sizeof(buf)-1);
207
208 for (s=&buf[0];
209 s && (p=strchr(s,'%')); ) {
210 n = NULL;
211 s = p + 1;
212
213 switch (p[1]) {
214 case 'h': n = client_name(0); break;
215 case 'a': n = client_addr(0); break;
216 case 'l':
217 slprintf(buf2,sizeof(buf2)-1,"%.0f",
218 (double)file->length);
219 n = buf2;
220 break;
221 case 'p':
222 slprintf(buf2,sizeof(buf2)-1,"%d",
223 (int)getpid());
224 n = buf2;
225 break;
226 case 'o': n = op; break;
227 case 'f': n = f_name(file); break;
228 }
229
230 if (!n) continue;
231
232 l = strlen(n);
233
234 if ((l-1) + ((int)(s - &buf[0])) > sizeof(buf)) {
235 rprintf(FERROR,"buffer overflow expanding %%%c - exiting\n",
236 p[0]);
237 exit_cleanup(1);
238 }
239
240 if (l != 2) {
241 memmove(s+(l-1), s+1, strlen(s+1));
242 }
243 memcpy(p, n, l);
244
245 s = p+l;
246 }
247
248 rprintf(FLOG,"%s\n", buf);
249}
250
251/* log the outgoing transfer of a file */
252void log_send(struct file_struct *file)
253{
254 extern int module_id;
255 if (lp_transfer_logging(module_id)) {
256 log_formatted("send", file);
257 }
258}
259
260/* log the incoming transfer of a file */
261void log_recv(struct file_struct *file)
262{
263 extern int module_id;
264 if (lp_transfer_logging(module_id)) {
265 log_formatted("recv", file);
266 }
267}
268
269/* called when the transfer is interrupted for some reason */
270void log_exit(int code)
271{
272 if (code == 0) {
273 extern struct stats stats;
274 rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
275 (double)stats.total_written,
276 (double)stats.total_read,
277 (double)stats.total_size);
278 } else {
279 rprintf(FLOG,"transfer interrupted\n");
280 }
281}
282
283/* log the incoming transfer of a file for interactive use, this
284 will be called at the end where the client was run */
285void log_transfer(struct file_struct *file, char *fname)
286{
287 extern int verbose;
288
289 if (!verbose) return;
290
291 rprintf(FINFO,"%s\n", fname);
292}
293