- Typical tranfers now save 12-20 bytes per file because several vars
[rsync/rsync.git] / log.c
1 /*
2  * Logging and utility functions.
3  *
4  * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5  * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org>
6  * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
21  */
22
23 #include "rsync.h"
24 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
25 #include <iconv.h>
26 #endif
27
28 extern int verbose;
29 extern int dry_run;
30 extern int am_daemon;
31 extern int am_server;
32 extern int am_sender;
33 extern int local_server;
34 extern int quiet;
35 extern int module_id;
36 extern int msg_fd_out;
37 extern int allow_8bit_chars;
38 extern int protocol_version;
39 extern int preserve_times;
40 extern int preserve_uid;
41 extern int preserve_gid;
42 extern int flist_extra_ndx;
43 extern int file_struct_len;
44 extern int stdout_format_has_i;
45 extern int stdout_format_has_o_or_i;
46 extern int logfile_format_has_i;
47 extern int logfile_format_has_o_or_i;
48 extern mode_t orig_umask;
49 extern char *auth_user;
50 extern char *stdout_format;
51 extern char *logfile_format;
52 extern char *logfile_name;
53 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
54 extern iconv_t ic_chck;
55 #endif
56 extern char curr_dir[];
57 extern unsigned int module_dirlen;
58
59 static int log_initialised;
60 static int logfile_was_closed;
61 static FILE *logfile_fp;
62 struct stats stats;
63
64 int log_got_error = 0;
65
66 struct {
67         int code;
68         char const *name;
69 } const rerr_names[] = {
70         { RERR_SYNTAX     , "syntax or usage error" },
71         { RERR_PROTOCOL   , "protocol incompatibility" },
72         { RERR_FILESELECT , "errors selecting input/output files, dirs" },
73         { RERR_UNSUPPORTED, "requested action not supported" },
74         { RERR_STARTCLIENT, "error starting client-server protocol" },
75         { RERR_SOCKETIO   , "error in socket IO" },
76         { RERR_FILEIO     , "error in file IO" },
77         { RERR_STREAMIO   , "error in rsync protocol data stream" },
78         { RERR_MESSAGEIO  , "errors with program diagnostics" },
79         { RERR_IPC        , "error in IPC code" },
80         { RERR_CRASHED    , "sibling process crashed" },
81         { RERR_TERMINATED , "sibling process terminated abnormally" },
82         { RERR_SIGNAL1    , "received SIGUSR1" },
83         { RERR_SIGNAL     , "received SIGINT, SIGTERM, or SIGHUP" },
84         { RERR_WAITCHILD  , "waitpid() failed" },
85         { RERR_MALLOC     , "error allocating core memory buffers" },
86         { RERR_PARTIAL    , "some files could not be transferred" },
87         { RERR_VANISHED   , "some files vanished before they could be transferred" },
88         { RERR_TIMEOUT    , "timeout in data send/receive" },
89         { RERR_CMD_FAILED , "remote shell failed" },
90         { RERR_CMD_KILLED , "remote shell killed" },
91         { RERR_CMD_RUN    , "remote command could not be run" },
92         { RERR_CMD_NOTFOUND,"remote command not found" },
93         { RERR_DEL_LIMIT  , "the --max-delete limit stopped deletions" },
94         { 0, NULL }
95 };
96
97 /*
98  * Map from rsync error code to name, or return NULL.
99  */
100 static char const *rerr_name(int code)
101 {
102         int i;
103         for (i = 0; rerr_names[i].name; i++) {
104                 if (rerr_names[i].code == code)
105                         return rerr_names[i].name;
106         }
107         return NULL;
108 }
109
110 static void logit(int priority, const char *buf)
111 {
112         if (logfile_was_closed)
113                 logfile_reopen();
114         if (logfile_fp) {
115                 fprintf(logfile_fp, "%s [%d] %s",
116                         timestring(time(NULL)), (int)getpid(), buf);
117                 fflush(logfile_fp);
118         } else {
119                 syslog(priority, "%s", buf);
120         }
121 }
122
123 static void syslog_init()
124 {
125         static int been_here = 0;
126         int options = LOG_PID;
127
128         if (been_here)
129                 return;
130         been_here = 1;
131
132 #ifdef LOG_NDELAY
133         options |= LOG_NDELAY;
134 #endif
135
136 #ifdef LOG_DAEMON
137         openlog("rsyncd", options, lp_syslog_facility(module_id));
138 #else
139         openlog("rsyncd", options);
140 #endif
141
142 #ifndef LOG_NDELAY
143         logit(LOG_INFO, "rsyncd started\n");
144 #endif
145 }
146
147 static void logfile_open(void)
148 {
149         mode_t old_umask = umask(022 | orig_umask);
150         logfile_fp = fopen(logfile_name, "a");
151         umask(old_umask);
152         if (!logfile_fp) {
153                 int fopen_errno = errno;
154                 /* Rsync falls back to using syslog on failure. */
155                 syslog_init();
156                 rsyserr(FERROR, fopen_errno,
157                         "failed to open log-file %s", logfile_name);
158                 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
159         }
160 }
161
162 void log_init(int restart)
163 {
164         if (log_initialised) {
165                 if (!restart)
166                         return;
167                 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
168                         if (logfile_fp) {
169                                 fclose(logfile_fp);
170                                 logfile_fp = NULL;
171                         } else
172                                 closelog();
173                         logfile_name = NULL;
174                 } else if (*logfile_name)
175                         return; /* unchanged, non-empty "log file" names */
176                 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
177                         closelog();
178                 else
179                         return; /* unchanged syslog settings */
180         } else
181                 log_initialised = 1;
182
183         /* This looks pointless, but it is needed in order for the
184          * C library on some systems to fetch the timezone info
185          * before the chroot. */
186         timestring(time(NULL));
187
188         /* Optionally use a log file instead of syslog.  (Non-daemon
189          * rsyncs will have already set logfile_name, as needed.) */
190         if (am_daemon && !logfile_name)
191                 logfile_name = lp_log_file(module_id);
192         if (logfile_name && *logfile_name)
193                 logfile_open();
194         else
195                 syslog_init();
196 }
197
198 void logfile_close(void)
199 {
200         if (logfile_fp) {
201                 logfile_was_closed = 1;
202                 fclose(logfile_fp);
203                 logfile_fp = NULL;
204         }
205 }
206
207 void logfile_reopen(void)
208 {
209         if (logfile_was_closed) {
210                 logfile_was_closed = 0;
211                 logfile_open();
212         }
213 }
214
215 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
216 {
217         const char *s, *end = buf + len;
218         for (s = buf; s < end; s++) {
219                 if ((s < end - 4
220                   && *s == '\\' && s[1] == '#'
221                   && isDigit(s + 2)
222                   && isDigit(s + 3)
223                   && isDigit(s + 4))
224                  || (*s != '\t'
225                   && ((use_isprint && !isPrint(s))
226                    || *(uchar*)s < ' '))) {
227                         if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
228                                 exit_cleanup(RERR_MESSAGEIO);
229                         fprintf(f, "\\#%03o", *(uchar*)s);
230                         buf = s + 1;
231                 }
232         }
233         if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
234                 exit_cleanup(RERR_MESSAGEIO);
235 }
236
237 /* this is the underlying (unformatted) rsync debugging function. Call
238  * it with FINFO, FERROR or FLOG.  Note: recursion can happen with
239  * certain fatal conditions. */
240 void rwrite(enum logcode code, const char *buf, int len)
241 {
242         int trailing_CR_or_NL;
243         FILE *f = NULL;
244
245         if (len < 0)
246                 exit_cleanup(RERR_MESSAGEIO);
247
248         if (am_server && msg_fd_out >= 0) {
249                 /* Pass the message to our sibling. */
250                 send_msg((enum msgcode)code, buf, len);
251                 return;
252         }
253
254         if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
255                 code = FERROR;
256
257         if (code == FCLIENT)
258                 code = FINFO;
259         else if (am_daemon || logfile_name) {
260                 static int in_block;
261                 char msg[2048];
262                 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
263
264                 if (in_block)
265                         return;
266                 in_block = 1;
267                 if (!log_initialised)
268                         log_init(0);
269                 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
270                 logit(priority, msg);
271                 in_block = 0;
272
273                 if (code == FLOG || (am_daemon && !am_server))
274                         return;
275         } else if (code == FLOG)
276                 return;
277
278         if (quiet && code != FERROR)
279                 return;
280
281         if (am_server) {
282                 /* Pass the message to the non-server side. */
283                 if (send_msg((enum msgcode)code, buf, len))
284                         return;
285                 if (am_daemon) {
286                         /* TODO: can we send the error to the user somehow? */
287                         return;
288                 }
289         }
290
291         switch (code) {
292         case FERROR:
293                 log_got_error = 1;
294                 f = stderr;
295                 break;
296         case FINFO:
297                 f = am_server ? stderr : stdout;
298                 break;
299         default:
300                 exit_cleanup(RERR_MESSAGEIO);
301         }
302
303         trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
304                           ? buf[--len] : 0;
305
306 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
307 #ifndef ICONV_CONST
308 #define ICONV_CONST
309 #endif
310         if (ic_chck != (iconv_t)-1) {
311                 char convbuf[1024];
312                 ICONV_CONST char *in_buf = (ICONV_CONST char *)buf;
313                 char *out_buf = convbuf;
314                 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
315
316                 iconv(ic_chck, NULL, 0, NULL, 0);
317                 while (iconv(ic_chck, &in_buf,&in_cnt,
318                              &out_buf,&out_cnt) == (size_t)-1) {
319                         if (out_buf != convbuf) {
320                                 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
321                                 out_buf = convbuf;
322                                 out_cnt = sizeof convbuf - 1;
323                         }
324                         if (errno == E2BIG)
325                                 continue;
326                         fprintf(f, "\\#%03o", *(uchar*)in_buf++);
327                         in_cnt--;
328                 }
329                 if (out_buf != convbuf)
330                         filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
331         } else
332 #endif
333                 filtered_fwrite(f, buf, len, !allow_8bit_chars);
334
335         if (trailing_CR_or_NL) {
336                 fputc(trailing_CR_or_NL, f);
337                 fflush(f);
338         }
339 }
340
341 /* This is the rsync debugging function. Call it with FINFO, FERROR or
342  * FLOG. */
343 void rprintf(enum logcode code, const char *format, ...)
344 {
345         va_list ap;
346         char buf[BIGPATHBUFLEN];
347         size_t len;
348
349         va_start(ap, format);
350         len = vsnprintf(buf, sizeof buf, format, ap);
351         va_end(ap);
352
353         /* Deal with buffer overruns.  Instead of panicking, just
354          * truncate the resulting string.  (Note that configure ensures
355          * that we have a vsnprintf() that doesn't ever return -1.) */
356         if (len > sizeof buf - 1) {
357                 static const char ellipsis[] = "[...]";
358
359                 /* Reset length, and zero-terminate the end of our buffer */
360                 len = sizeof buf - 1;
361                 buf[len] = '\0';
362
363                 /* Copy the ellipsis to the end of the string, but give
364                  * us one extra character:
365                  *
366                  *                  v--- null byte at buf[sizeof buf - 1]
367                  *        abcdefghij0
368                  *     -> abcd[...]00  <-- now two null bytes at end
369                  *
370                  * If the input format string has a trailing newline,
371                  * we copy it into that extra null; if it doesn't, well,
372                  * all we lose is one byte.  */
373                 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
374                 if (format[strlen(format)-1] == '\n') {
375                         buf[len-1] = '\n';
376                 }
377         }
378
379         rwrite(code, buf, len);
380 }
381
382 /* This is like rprintf, but it also tries to print some
383  * representation of the error code.  Normally errcode = errno.
384  *
385  * Unlike rprintf, this always adds a newline and there should not be
386  * one in the format string.
387  *
388  * Note that since strerror might involve dynamically loading a
389  * message catalog we need to call it once before chroot-ing. */
390 void rsyserr(enum logcode code, int errcode, const char *format, ...)
391 {
392         va_list ap;
393         char buf[BIGPATHBUFLEN];
394         size_t len;
395
396         strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
397         len = (sizeof RSYNC_NAME ": ") - 1;
398
399         va_start(ap, format);
400         len += vsnprintf(buf + len, sizeof buf - len, format, ap);
401         va_end(ap);
402
403         if (len < sizeof buf) {
404                 len += snprintf(buf + len, sizeof buf - len,
405                                 ": %s (%d)\n", strerror(errcode), errcode);
406         }
407         if (len >= sizeof buf)
408                 exit_cleanup(RERR_MESSAGEIO);
409
410         rwrite(code, buf, len);
411 }
412
413 void rflush(enum logcode code)
414 {
415         FILE *f = NULL;
416
417         if (am_daemon || code == FLOG)
418                 return;
419
420         if (code == FERROR || am_server)
421                 f = stderr;
422         else
423                 f = stdout;
424
425         fflush(f);
426 }
427
428 /* A generic logging routine for send/recv, with parameter substitiution. */
429 static void log_formatted(enum logcode code, const char *format, const char *op,
430                           struct file_struct *file, struct stats *initial_stats,
431                           int iflags, const char *hlink)
432 {
433         char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
434         char *p, *s, *c;
435         const char *n;
436         size_t len, total;
437         int64 b;
438
439         *fmt = '%';
440
441         /* We expand % codes one by one in place in buf.  We don't
442          * copy in the terminating null of the inserted strings, but
443          * rather keep going until we reach the null of the format. */
444         total = strlcpy(buf, format, sizeof buf);
445         if (total > MAXPATHLEN) {
446                 rprintf(FERROR, "log-format string is WAY too long!\n");
447                 exit_cleanup(RERR_MESSAGEIO);
448         }
449         buf[total++] = '\n';
450         buf[total] = '\0';
451
452         for (p = buf; (p = strchr(p, '%')) != NULL; ) {
453                 s = p++;
454                 c = fmt + 1;
455                 if (*p == '-')
456                         *c++ = *p++;
457                 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
458                         *c++ = *p++;
459                 if (!*p)
460                         break;
461                 *c = '\0';
462                 n = NULL;
463
464                 switch (*p) {
465                 case 'h':
466                         if (am_daemon)
467                                 n = client_name(0);
468                         break;
469                 case 'a':
470                         if (am_daemon)
471                                 n = client_addr(0);
472                         break;
473                 case 'l':
474                         strlcat(fmt, ".0f", sizeof fmt);
475                         snprintf(buf2, sizeof buf2, fmt,
476                                  (double)file->length);
477                         n = buf2;
478                         break;
479                 case 'U':
480                         strlcat(fmt, "ld", sizeof fmt);
481                         snprintf(buf2, sizeof buf2, fmt,
482                                  preserve_uid ? (long)F_UID(file) : 0);
483                         n = buf2;
484                         break;
485                 case 'G':
486                         if (!preserve_gid || F_GID(file) == GID_NONE)
487                                 n = "DEFAULT";
488                         else {
489                                 strlcat(fmt, "ld", sizeof fmt);
490                                 snprintf(buf2, sizeof buf2, fmt,
491                                          (long)F_GID(file));
492                                 n = buf2;
493                         }
494                         break;
495                 case 'p':
496                         strlcat(fmt, "ld", sizeof fmt);
497                         snprintf(buf2, sizeof buf2, fmt,
498                                  (long)getpid());
499                         n = buf2;
500                         break;
501                 case 'M':
502                         n = c = timestring(file->modtime);
503                         while ((c = strchr(p, ' ')) != NULL)
504                                 *c = '-';
505                         break;
506                 case 'B':
507                         c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
508                         permstring(c, file->mode);
509                         n = c + 1; /* skip the type char */
510                         break;
511                 case 'o':
512                         n = op;
513                         break;
514                 case 'f':
515                         c = f_name(file, NULL);
516                         if (am_sender && file->dir.root) {
517                                 pathjoin(buf2, sizeof buf2,
518                                          file->dir.root, c);
519                                 clean_fname(buf2, 0);
520                                 if (fmt[1]) {
521                                         strlcpy(c, buf2, MAXPATHLEN);
522                                         n = c;
523                                 } else
524                                         n = buf2;
525                         } else if (*c != '/') {
526                                 pathjoin(buf2, sizeof buf2,
527                                          curr_dir + module_dirlen, c);
528                                 clean_fname(buf2, 0);
529                                 if (fmt[1]) {
530                                         strlcpy(c, buf2, MAXPATHLEN);
531                                         n = c;
532                                 } else
533                                         n = buf2;
534                         } else {
535                                 clean_fname(c, 0);
536                                 n = c;
537                         }
538                         if (*n == '/')
539                                 n++;
540                         break;
541                 case 'n':
542                         c = f_name(file, NULL);
543                         if (S_ISDIR(file->mode))
544                                 strlcat(c, "/", MAXPATHLEN);
545                         n = c;
546                         break;
547                 case 'L':
548                         if (hlink && *hlink) {
549                                 n = hlink;
550                                 strlcpy(buf2, " => ", sizeof buf2);
551                         } else if (S_ISLNK(file->mode)) {
552                                 n = F_SYMLINK(file);
553                                 strlcpy(buf2, " -> ", sizeof buf2);
554                         } else {
555                                 n = "";
556                                 if (!fmt[1])
557                                         break;
558                                 strlcpy(buf2, "    ", sizeof buf2);
559                         }
560                         strlcat(fmt, "s", sizeof fmt);
561                         snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
562                         n = buf2;
563                         break;
564                 case 'm':
565                         n = lp_name(module_id);
566                         break;
567                 case 't':
568                         n = timestring(time(NULL));
569                         break;
570                 case 'P':
571                         n = lp_path(module_id);
572                         break;
573                 case 'u':
574                         n = auth_user;
575                         break;
576                 case 'b':
577                         if (am_sender) {
578                                 b = stats.total_written -
579                                         initial_stats->total_written;
580                         } else {
581                                 b = stats.total_read -
582                                         initial_stats->total_read;
583                         }
584                         strlcat(fmt, ".0f", sizeof fmt);
585                         snprintf(buf2, sizeof buf2, fmt, (double)b);
586                         n = buf2;
587                         break;
588                 case 'c':
589                         if (!am_sender) {
590                                 b = stats.total_written -
591                                         initial_stats->total_written;
592                         } else {
593                                 b = stats.total_read -
594                                         initial_stats->total_read;
595                         }
596                         strlcat(fmt, ".0f", sizeof fmt);
597                         snprintf(buf2, sizeof buf2, fmt, (double)b);
598                         n = buf2;
599                         break;
600                 case 'i':
601                         if (iflags & ITEM_DELETED) {
602                                 n = "*deleting";
603                                 break;
604                         }
605                         n  = c = buf2 + MAXPATHLEN - 32;
606                         c[0] = iflags & ITEM_LOCAL_CHANGE
607                               ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
608                              : !(iflags & ITEM_TRANSFER) ? '.'
609                              : !local_server && *op == 's' ? '<' : '>';
610                         c[1] = S_ISDIR(file->mode) ? 'd'
611                              : IS_SPECIAL(file->mode) ? 'S'
612                              : IS_DEVICE(file->mode) ? 'D'
613                              : S_ISLNK(file->mode) ? 'L' : 'f';
614                         c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
615                         c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
616                         c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
617                              : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
618                         c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
619                         c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
620                         c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
621                         c[8] = '.';
622                         c[9] = '\0';
623
624                         if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
625                                 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
626                                 int i;
627                                 for (i = 2; c[i]; i++)
628                                         c[i] = ch;
629                         } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
630                                 int i;
631                                 for (i = 2; c[i]; i++) {
632                                         if (c[i] != '.')
633                                                 break;
634                                 }
635                                 if (!c[i]) {
636                                         for (i = 2; c[i]; i++)
637                                                 c[i] = ' ';
638                                 }
639                         }
640                         break;
641                 }
642
643                 /* "n" is the string to be inserted in place of this % code. */
644                 if (!n)
645                         continue;
646                 if (n != buf2 && fmt[1]) {
647                         strlcat(fmt, "s", sizeof fmt);
648                         snprintf(buf2, sizeof buf2, fmt, n);
649                         n = buf2;
650                 }
651                 len = strlen(n);
652
653                 /* Subtract the length of the escape from the string's size. */
654                 total -= p - s + 1;
655
656                 if (len + total >= (size_t)sizeof buf) {
657                         rprintf(FERROR,
658                                 "buffer overflow expanding %%%c -- exiting\n",
659                                 p[0]);
660                         exit_cleanup(RERR_MESSAGEIO);
661                 }
662
663                 /* Shuffle the rest of the string along to make space for n */
664                 if (len != (size_t)(p - s + 1))
665                         memmove(s + len, p + 1, total - (s - buf) + 1);
666                 total += len;
667
668                 /* Insert the contents of string "n", but NOT its null. */
669                 if (len)
670                         memcpy(s, n, len);
671
672                 /* Skip over inserted string; continue looking */
673                 p = s + len;
674         }
675
676         rwrite(code, buf, total);
677 }
678
679 /* Return 1 if the format escape is in the log-format string (e.g. look for
680  * the 'b' in the "%9b" format escape). */
681 int log_format_has(const char *format, char esc)
682 {
683         const char *p;
684
685         if (!format)
686                 return 0;
687
688         for (p = format; (p = strchr(p, '%')) != NULL; ) {
689                 if (*++p == '-')
690                         p++;
691                 while (isDigit(p))
692                         p++;
693                 if (!*p)
694                         break;
695                 if (*p == esc)
696                         return 1;
697         }
698         return 0;
699 }
700
701 /* Log the transfer of a file.  If the code is FCLIENT, the output just goes
702  * to stdout.  If it is FLOG, it just goes to the log file.  Otherwise we
703  * output to both. */
704 void log_item(enum logcode code, struct file_struct *file,
705               struct stats *initial_stats, int iflags, const char *hlink)
706 {
707         const char *s_or_r = am_sender ? "send" : "recv";
708
709         if (code != FLOG && stdout_format && !am_server) {
710                 log_formatted(FCLIENT, stdout_format, s_or_r,
711                               file, initial_stats, iflags, hlink);
712         }
713         if (code != FCLIENT && logfile_format && *logfile_format) {
714                 log_formatted(FLOG, logfile_format, s_or_r,
715                               file, initial_stats, iflags, hlink);
716         }
717 }
718
719 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
720                     const char *buf)
721 {
722         int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
723         int see_item = itemizing && (significant_flags || *buf
724                 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
725         int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
726         if (am_server) {
727                 if (logfile_name && !dry_run && see_item
728                  && (significant_flags || logfile_format_has_i))
729                         log_item(FLOG, file, &stats, iflags, buf);
730         } else if (see_item || local_change || *buf
731             || (S_ISDIR(file->mode) && significant_flags)) {
732                 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
733                 log_item(code, file, &stats, iflags, buf);
734         }
735 }
736
737 void log_delete(const char *fname, int mode)
738 {
739         static struct file_struct file;
740         int len = strlen(fname);
741         const char *fmt;
742
743         file.mode = mode;
744         file.basename = fname;
745
746         if (!verbose && !stdout_format)
747                 ;
748         else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
749                 if (S_ISDIR(mode))
750                         len++; /* directories include trailing null */
751                 send_msg(MSG_DELETED, fname, len);
752         } else {
753                 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
754                 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
755                               ITEM_DELETED, NULL);
756         }
757
758         if (!logfile_name || dry_run || !logfile_format)
759                 return;
760
761         fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
762         log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
763 }
764
765 /*
766  * Called when the transfer is interrupted for some reason.
767  *
768  * Code is one of the RERR_* codes from errcode.h, or terminating
769  * successfully.
770  */
771 void log_exit(int code, const char *file, int line)
772 {
773         if (code == 0) {
774                 rprintf(FLOG,"sent %.0f bytes  received %.0f bytes  total size %.0f\n",
775                         (double)stats.total_written,
776                         (double)stats.total_read,
777                         (double)stats.total_size);
778         } else {
779                 const char *name;
780
781                 name = rerr_name(code);
782                 if (!name)
783                         name = "unexplained error";
784
785                 /* VANISHED is not an error, only a warning */
786                 if (code == RERR_VANISHED) {
787                         rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
788                                 name, code, file, line, who_am_i(), RSYNC_VERSION);
789                 } else {
790                         rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
791                                 name, code, file, line, who_am_i(), RSYNC_VERSION);
792                 }
793         }
794 }