A slight tweak to the logic that calls log_exit().
[rsync/rsync-patches.git] / log-file.diff
CommitLineData
ac81cbf8 1This patch allows a non-daemon server and/or a client rsync to log what they
9d32714e
WD
2are doing, similar to how a daemon logs its actions.
3
4--- old/cleanup.c
5+++ new/cleanup.c
ac81cbf8 6@@ -21,10 +21,12 @@
9d32714e
WD
7
8 #include "rsync.h"
9
10+extern int am_server;
11 extern int io_error;
12 extern int keep_partial;
13 extern int log_got_error;
ac81cbf8
WD
14 extern char *partial_dir;
15+extern char *logfile_name;
16
17 #ifdef HAVE_SIGACTION
18 static struct sigaction sigact;
19@@ -149,7 +151,7 @@ void _exit_cleanup(int code, const char
9d32714e
WD
20 code = RERR_PARTIAL;
21 }
22
23- if (code)
ac81cbf8 24+ if (code || am_daemon || (am_server && logfile_name))
9d32714e
WD
25 log_exit(code, file, line);
26
27 if (verbose > 2) {
28--- old/clientserver.c
29+++ new/clientserver.c
6596b2bd 30@@ -44,10 +44,14 @@ extern int protocol_version;
9d32714e
WD
31 extern int io_timeout;
32 extern int no_detach;
33 extern int default_af_hint;
34+extern int logfile_format_has_i;
35+extern int logfile_format_has_o_or_i;
36 extern mode_t orig_umask;
37 extern char *bind_address;
38 extern char *sockopts;
39 extern char *config_file;
40+extern char *logfile_format;
41+extern char *logfile_name;
42 extern char *files_from;
43 extern char *tmpdir;
44 extern struct chmod_mode_struct *chmod_modes;
6596b2bd 45@@ -55,8 +59,6 @@ extern struct filter_list_struct server_
9d32714e
WD
46
47 char *auth_user;
48 int read_only = 0;
49-int daemon_log_format_has_i = 0;
50-int daemon_log_format_has_o_or_i = 0;
51 int module_id = -1;
52 struct chmod_mode_struct *daemon_chmod_modes;
53
6596b2bd 54@@ -329,11 +331,12 @@ static int rsync_module(int f_in, int f_
9d32714e
WD
55 read_only = 1;
56
9d32714e
WD
57 if (lp_transfer_logging(i)) {
58- if (log_format_has(lp_log_format(i), 'i'))
59- daemon_log_format_has_i = 1;
60- if (daemon_log_format_has_i
61- || log_format_has(lp_log_format(i), 'o'))
62- daemon_log_format_has_o_or_i = 1;
63+ logfile_format = lp_log_format(i);
64+ if (log_format_has(logfile_format, 'i'))
65+ logfile_format_has_i = 1;
66+ if (logfile_format_has_i
67+ || log_format_has(logfile_format, 'o'))
68+ logfile_format_has_o_or_i = 1;
69 }
70
71 am_root = (MY_UID() == 0);
72--- old/flist.c
73+++ new/flist.c
74@@ -95,15 +95,15 @@ static int show_filelist_p(void)
75
76 static void start_filelist_progress(char *kind)
77 {
78- rprintf(FINFO, "%s ... ", kind);
79+ rprintf(FCLIENT, "%s ... ", kind);
80 if (verbose > 1 || do_progress)
81- rprintf(FINFO, "\n");
82+ rprintf(FCLIENT, "\n");
83 rflush(FINFO);
84 }
85
86 static void emit_filelist_progress(int count)
87 {
88- rprintf(FINFO, " %d files...\r", count);
89+ rprintf(FCLIENT, " %d files...\r", count);
90 }
91
92 static void maybe_emit_filelist_progress(int count)
93@@ -295,7 +295,7 @@ void flist_expand(struct file_list *flis
94 flist->malloced);
95
96 if (verbose >= 2 && flist->malloced != FLIST_START) {
97- rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n",
98+ rprintf(FCLIENT, "[%s] expand file_list to %.0f bytes, did%s move\n",
99 who_am_i(),
100 (double)sizeof flist->files[0] * flist->malloced,
101 (new_ptr == flist->files) ? " not" : "");
102@@ -1077,6 +1077,7 @@ struct file_list *send_file_list(int f,
103 int64 start_write;
104 int use_ff_fd = 0;
105
106+ rprintf(FLOG, "building file list\n");
107 if (show_filelist_p())
108 start_filelist_progress("building file list");
109
110@@ -1343,6 +1344,7 @@ struct file_list *recv_file_list(int f)
111 unsigned short flags;
112 int64 start_read;
113
114+ rprintf(FLOG, "receiving file list\n");
115 if (show_filelist_p())
116 start_filelist_progress("receiving file list");
117
118--- old/generator.c
119+++ new/generator.c
120@@ -27,7 +27,7 @@ extern int verbose;
121 extern int dry_run;
122 extern int do_xfers;
123 extern int log_format_has_i;
124-extern int daemon_log_format_has_i;
125+extern int logfile_format_has_i;
126 extern int am_root;
127 extern int am_server;
128 extern int am_daemon;
ac81cbf8 129@@ -661,7 +661,7 @@ static int try_dests_reg(struct file_str
9d32714e
WD
130 } else if (itemizing)
131 itemize(file, ndx, 0, stp, 0, 0, NULL);
132 if (verbose > 1 && maybe_ATTRS_REPORT) {
133- code = daemon_log_format_has_i || dry_run
134+ code = logfile_format_has_i || dry_run
135 ? FCLIENT : FINFO;
136 rprintf(code, "%s is uptodate\n", fname);
137 }
ac81cbf8 138@@ -684,7 +684,7 @@ static int try_dests_reg(struct file_str
9d32714e
WD
139 if (maybe_ATTRS_REPORT
140 && ((!itemizing && verbose && match_level == 2)
141 || (verbose > 1 && match_level == 3))) {
142- code = daemon_log_format_has_i || dry_run
143+ code = logfile_format_has_i || dry_run
144 ? FCLIENT : FINFO;
145 rprintf(code, "%s%s\n", fname,
146 match_level == 3 ? " is uptodate" : "");
ac81cbf8 147@@ -746,7 +746,7 @@ static int try_dests_non(struct file_str
9d32714e
WD
148 itemize(file, ndx, 0, &st, changes, 0, lp);
149 }
150 if (verbose > 1 && maybe_ATTRS_REPORT) {
151- code = daemon_log_format_has_i || dry_run
152+ code = logfile_format_has_i || dry_run
153 ? FCLIENT : FINFO;
154 rprintf(code, "%s is uptodate\n", fname);
155 }
ac81cbf8 156@@ -1309,9 +1309,9 @@ void generate_files(int f_out, struct fi
9d32714e
WD
157 if (protocol_version >= 29) {
158 itemizing = 1;
159 maybe_ATTRS_REPORT = log_format_has_i ? 0 : ATTRS_REPORT;
160- code = daemon_log_format_has_i ? 0 : FLOG;
161+ code = logfile_format_has_i ? 0 : FLOG;
162 } else if (am_daemon) {
163- itemizing = daemon_log_format_has_i && do_xfers;
164+ itemizing = logfile_format_has_i && do_xfers;
165 maybe_ATTRS_REPORT = ATTRS_REPORT;
166 code = itemizing || !do_xfers ? FCLIENT : FINFO;
167 } else if (!am_server) {
168--- old/log.c
169+++ new/log.c
6596b2bd 170@@ -44,18 +44,19 @@ extern int protocol_version;
9d32714e
WD
171 extern int preserve_times;
172 extern int log_format_has_i;
173 extern int log_format_has_o_or_i;
174-extern int daemon_log_format_has_o_or_i;
175+extern int logfile_format_has_o_or_i;
176 extern mode_t orig_umask;
177 extern char *auth_user;
178 extern char *log_format;
179+extern char *logfile_format;
180+extern char *logfile_name;
181 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
182 extern iconv_t ic_chck;
183 #endif
184
6596b2bd 185 static int log_initialised;
9d32714e
WD
186 static int logfile_was_closed;
187-static char *logfname;
31ce435c
WD
188-static FILE *logfile;
189+static FILE *logfile_fp;
9d32714e
WD
190 struct stats stats;
191
31ce435c
WD
192 int log_got_error = 0;
193@@ -109,10 +110,10 @@ static void logit(int priority, char *bu
194 {
195 if (logfile_was_closed)
196 logfile_reopen();
197- if (logfile) {
198- fprintf(logfile,"%s [%d] %s",
199+ if (logfile_fp) {
200+ fprintf(logfile_fp, "%s [%d] %s",
201 timestring(time(NULL)), (int)getpid(), buf);
202- fflush(logfile);
203+ fflush(logfile_fp);
204 } else {
205 syslog(priority, "%s", buf);
206 }
9d32714e
WD
207@@ -145,14 +146,14 @@ static void syslog_init()
208 static void logfile_open(void)
209 {
210 mode_t old_umask = umask(022 | orig_umask);
211- logfile = fopen(logfname, "a");
31ce435c 212+ logfile_fp = fopen(logfile_name, "a");
9d32714e 213 umask(old_umask);
31ce435c
WD
214- if (!logfile) {
215+ if (!logfile_fp) {
9d32714e
WD
216 int fopen_errno = errno;
217 /* Rsync falls back to using syslog on failure. */
218 syslog_init();
219 rsyserr(FERROR, fopen_errno,
220- "failed to open log-file %s", logfname);
221+ "failed to open log-file %s", logfile_name);
222 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
223 }
224 }
6596b2bd
WD
225@@ -171,9 +172,11 @@ void log_init(void)
226 t = time(NULL);
9d32714e
WD
227 localtime(&t);
228
6596b2bd 229- /* optionally use a log file instead of syslog */
9d32714e
WD
230- logfname = lp_log_file();
231- if (logfname && *logfname)
6596b2bd
WD
232+ /* Optionally use a log file instead of syslog. (Non-daemon
233+ * rsyncs will have already set logfile_name, as needed.) */
234+ if (am_daemon)
235+ logfile_name = lp_log_file();
9d32714e
WD
236+ if (logfile_name && *logfile_name)
237 logfile_open();
238 else
239 syslog_init();
6596b2bd 240@@ -181,10 +184,10 @@ void log_init(void)
31ce435c
WD
241
242 void logfile_close(void)
243 {
244- if (logfile) {
245+ if (logfile_fp) {
246 logfile_was_closed = 1;
247- fclose(logfile);
248- logfile = NULL;
249+ fclose(logfile_fp);
250+ logfile_fp = NULL;
251 }
252 }
253
6596b2bd 254@@ -243,9 +246,9 @@ void rwrite(enum logcode code, char *buf
9d32714e
WD
255
256 if (code == FCLIENT)
257 code = FINFO;
258- else if (am_daemon) {
6596b2bd 259+ else if (am_daemon || logfile_name) {
9d32714e
WD
260 static int in_block;
261- char msg[2048];
262+ char msg[2048], *s;
263 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
264
265 if (in_block)
6596b2bd 266@@ -254,10 +257,11 @@ void rwrite(enum logcode code, char *buf
9d32714e
WD
267 if (!log_initialised)
268 log_init();
269 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
270- logit(priority, msg);
271+ for (s = msg; *s == '\n' && s[1]; s++) {}
272+ logit(priority, s);
273 in_block = 0;
274
275- if (code == FLOG || !am_server)
276+ if (code == FLOG || (am_daemon && !am_server))
277 return;
278 } else if (code == FLOG)
279 return;
6596b2bd 280@@ -403,26 +407,14 @@ void rflush(enum logcode code)
9d32714e
WD
281 {
282 FILE *f = NULL;
283
284- if (am_daemon) {
285+ if (am_daemon || code == FLOG)
286 return;
287- }
288
289- if (code == FLOG) {
290- return;
291- }
292-
293- if (code == FERROR) {
294+ if (code == FERROR || am_server)
295 f = stderr;
296- }
297-
298- if (code == FINFO) {
299- if (am_server)
300- f = stderr;
301- else
302- f = stdout;
303- }
304+ else
305+ f = stdout;
306
307- if (!f) exit_cleanup(RERR_MESSAGEIO);
308 fflush(f);
309 }
310
6596b2bd 311@@ -695,12 +687,12 @@ void log_item(struct file_struct *file,
9d32714e
WD
312 {
313 char *s_or_r = am_sender ? "send" : "recv";
314
315- if (lp_transfer_logging(module_id)) {
316- log_formatted(FLOG, lp_log_format(module_id), s_or_r,
317- file, initial_stats, iflags, hlink);
318- } else if (log_format && !am_server) {
319+ if (log_format && !am_server) {
320 log_formatted(FNAME, log_format, s_or_r,
321 file, initial_stats, iflags, hlink);
322+ } else if (logfile_format) {
323+ log_formatted(FLOG, logfile_format, s_or_r,
324+ file, initial_stats, iflags, hlink);
325 }
326 }
327
6596b2bd 328@@ -712,7 +704,7 @@ void maybe_log_item(struct file_struct *
9d32714e
WD
329 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
330 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
331 if (am_server) {
332- if (am_daemon && !dry_run && see_item)
333+ if (logfile_name && !dry_run && see_item)
334 log_item(file, &stats, iflags, buf);
335 } else if (see_item || local_change || *buf
336 || (S_ISDIR(file->mode) && significant_flags))
6596b2bd 337@@ -740,10 +732,10 @@ void log_delete(char *fname, int mode)
9d32714e
WD
338 ITEM_DELETED, NULL);
339 }
340
341- if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
342+ if (!logfile_name || dry_run || !logfile_format)
343 return;
344
345- fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
346+ fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
347 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
348 }
349
350--- old/main.c
351+++ new/main.c
352@@ -168,7 +168,6 @@ static void handle_stats(int f)
353 return;
354
355 if (am_daemon) {
356- log_exit(0, __FILE__, __LINE__);
357 if (f == -1 || !am_sender)
358 return;
359 }
360--- old/options.c
361+++ new/options.c
a56690e2 362@@ -146,6 +146,8 @@ char *basis_dir[MAX_BASIS_DIRS+1];
9d32714e
WD
363 char *config_file = NULL;
364 char *shell_cmd = NULL;
365 char *log_format = NULL;
366+char *logfile_name = NULL;
367+char *logfile_format = NULL;
368 char *password_file = NULL;
369 char *rsync_path = RSYNC_PATH;
370 char *backup_dir = NULL;
a56690e2 371@@ -162,7 +164,9 @@ int verbose = 0;
9d32714e
WD
372 int quiet = 0;
373 int log_before_transfer = 0;
374 int log_format_has_i = 0;
375+int logfile_format_has_i = 0;
376 int log_format_has_o_or_i = 0;
377+int logfile_format_has_o_or_i = 0;
378 int always_checksum = 0;
379 int list_only = 0;
380
a56690e2 381@@ -359,6 +363,7 @@ void usage(enum logcode F)
9d32714e
WD
382 rprintf(F," --progress show progress during transfer\n");
383 rprintf(F," -P same as --partial --progress\n");
384 rprintf(F," -i, --itemize-changes output a change-summary for all updates\n");
385+ rprintf(F," --log-file=FILE output what we're doing to a log file\n");
386 rprintf(F," --log-format=FORMAT output filenames using the specified format\n");
387 rprintf(F," --password-file=FILE read password from FILE\n");
388 rprintf(F," --list-only list the files instead of copying them\n");
a56690e2 389@@ -492,6 +497,7 @@ static struct poptOption long_options[]
9d32714e
WD
390 {"partial-dir", 0, POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
391 {"delay-updates", 0, POPT_ARG_NONE, &delay_updates, 0, 0, 0 },
392 {"prune-empty-dirs",'m', POPT_ARG_NONE, &prune_empty_dirs, 0, 0, 0 },
393+ {"log-file", 0, POPT_ARG_STRING, &logfile_name, 0, 0, 0 },
394 {"log-format", 0, POPT_ARG_STRING, &log_format, 0, 0, 0 },
395 {"itemize-changes", 'i', POPT_ARG_NONE, 0, 'i', 0, 0 },
396 {"bwlimit", 0, POPT_ARG_INT, &bwlimit, 0, 0, 0 },
a56690e2 397@@ -1311,6 +1317,21 @@ int parse_arguments(int *argc, const cha
9d32714e
WD
398 if (log_format_has_i || log_format_has(log_format, 'o'))
399 log_format_has_o_or_i = 1;
400
401+ if (am_daemon)
402+ logfile_name = NULL;
403+ else if (logfile_name) {
404+ if (am_server) {
405+ logfile_format = "%i %n%L";
406+ logfile_format_has_i = logfile_format_has_o_or_i = 1;
407+ } else if (log_format) {
408+ logfile_format = log_format;
409+ logfile_format_has_i = log_format_has_i;
410+ logfile_format_has_o_or_i = log_format_has_o_or_i;
411+ }
412+ log_before_transfer = !am_server;
413+ log_init();
414+ }
415+
416 if (daemon_bwlimit && (!bwlimit || bwlimit > daemon_bwlimit))
417 bwlimit = daemon_bwlimit;
418 if (bwlimit) {
419--- old/progress.c
420+++ new/progress.c
421@@ -103,7 +103,7 @@ static void rprint_progress(OFF_T ofs, O
422 stats.num_files);
423 } else
424 strcpy(eol, "\r");
425- rprintf(FINFO, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s",
426+ rprintf(FCLIENT, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s",
427 human_num(ofs), pct, rate, units,
428 remain_h, remain_m, remain_s, eol);
429 }
430--- old/receiver.c
431+++ new/receiver.c
432@@ -27,7 +27,7 @@ extern int am_server;
433 extern int do_progress;
434 extern int log_before_transfer;
435 extern int log_format_has_i;
436-extern int daemon_log_format_has_i;
437+extern int logfile_format_has_i;
438 extern int csum_length;
439 extern int read_batch;
440 extern int write_batch;
441@@ -341,8 +341,7 @@ int recv_files(int f_in, struct file_lis
442 struct file_struct *file;
443 struct stats initial_stats;
444 int save_make_backups = make_backups;
445- int itemizing = am_daemon ? daemon_log_format_has_i
446- : !am_server && log_format_has_i;
447+ int itemizing = am_server ? logfile_format_has_i : log_format_has_i;
448 int max_phase = protocol_version >= 29 ? 2 : 1;
449 int i, recv_ok;
450
451--- old/rsync.c
452+++ new/rsync.c
453@@ -32,7 +32,7 @@
454
455 extern int verbose;
456 extern int dry_run;
457-extern int daemon_log_format_has_i;
458+extern int logfile_format_has_i;
459 extern int preserve_perms;
460 extern int preserve_executability;
461 extern int preserve_times;
462@@ -218,7 +218,7 @@ int set_file_attrs(char *fname, struct f
463 #endif
464
465 if (verbose > 1 && flags & ATTRS_REPORT) {
466- enum logcode code = daemon_log_format_has_i || dry_run
467+ enum logcode code = logfile_format_has_i || dry_run
468 ? FCLIENT : FINFO;
469 if (updated)
470 rprintf(code, "%s\n", fname);
471--- old/rsync.h
472+++ new/rsync.h
a56690e2 473@@ -158,8 +158,8 @@
9d32714e
WD
474
475
476 /* Log-message categories. Only FERROR and FINFO get sent over the socket.
477- * FLOG and FCLIENT are only used on the daemon side for custom logging,
478- * while FNAME is only used on the client side. */
479+ * FLOG only goes to the log file, not to the client; FCLIENT is the opposite.
480+ * FNAME is a client-side message when outputting a filename on its own. */
481 enum logcode { FERROR=1, FINFO=2, FLOG=3, FCLIENT=4, FNAME=5, FSOCKERR=6 };
482
483 /* Messages types that are sent over the message channel. The logcode
484--- old/rsync.yo
485+++ new/rsync.yo
486@@ -387,6 +387,7 @@ to the detailed description below for a
487 --progress show progress during transfer
488 -P same as --partial --progress
489 -i, --itemize-changes output a change-summary for all updates
490+ --log-file=FILE output what we're doing to a log file
491 --log-format=FORMAT output filenames using the specified format
492 --password-file=FILE read password from FILE
493 --list-only list the files instead of copying them
494@@ -1423,6 +1424,23 @@ the string "*deleting" for each item tha
495 you are talking to a recent enough rsync that it logs deletions instead of
496 outputting them as a verbose message).
497
498+dit(bf(--log-file=FILE)) This option causes rsync to log what it is doing
499+to a file. This is similar to the logging that a daemon does, but can be
500+requested for the client side and/or the server side of a non-daemon
501+transfer. If specified as a client option, transfer logging will in effect
502+if the bf(--log-format) option was either specified or implied (e.g.
503+bf(--verbose) implies a basic log format). If explicitly sent to a server
504+via the bf(--rsync-path) option, transfer logging will always occur using
505+the default bf(--itemize-changes) format.
506+
507+Here's a example command that requests the remote side to log what is
508+happening:
509+
510+verb( rsync -av --rsync-path="path --log-file=/tmp/rlog" src/ dest/)
511+
512+This is very useful if you need to debug why a connection is closing
513+unexpectedly.
514+
515 dit(bf(--log-format=FORMAT)) This allows you to specify exactly what the
516 rsync client outputs to the user on a per-file basis. The format is a text
517 string containing embedded single-character escape sequences prefixed with
518--- old/sender.c
519+++ new/sender.c
520@@ -25,7 +25,7 @@ extern int am_server;
521 extern int am_daemon;
522 extern int log_before_transfer;
523 extern int log_format_has_i;
524-extern int daemon_log_format_has_i;
525+extern int logfile_format_has_i;
526 extern int csum_length;
527 extern int append_mode;
528 extern int io_error;
529@@ -215,8 +215,7 @@ void send_files(struct file_list *flist,
530 int phase = 0, max_phase = protocol_version >= 29 ? 2 : 1;
531 struct stats initial_stats;
532 int save_make_backups = make_backups;
533- int itemizing = am_daemon ? daemon_log_format_has_i
534- : !am_server && log_format_has_i;
535+ int itemizing = am_server ? logfile_format_has_i : log_format_has_i;
536 int f_xfer = write_batch < 0 ? batch_fd : f_out;
537 int i, j;
538