A slight tweak to the logic that calls log_exit().
[rsync/rsync-patches.git] / log-file.diff
index ba97a8f..7bfc8bc 100644 (file)
@@ -1,9 +1,9 @@
-This patch allows a non-daemon server and a client rsync to log what they
+This patch allows a non-daemon server and/or a client rsync to log what they
 are doing, similar to how a daemon logs its actions.
 
 --- old/cleanup.c
 +++ new/cleanup.c
-@@ -21,6 +21,7 @@
+@@ -21,10 +21,12 @@
  
  #include "rsync.h"
  
@@ -11,12 +11,17 @@ are doing, similar to how a daemon logs its actions.
  extern int io_error;
  extern int keep_partial;
  extern int log_got_error;
-@@ -149,7 +150,7 @@ void _exit_cleanup(int code, const char 
+ extern char *partial_dir;
++extern char *logfile_name;
+ #ifdef HAVE_SIGACTION
+ static struct sigaction sigact;
+@@ -149,7 +151,7 @@ void _exit_cleanup(int code, const char 
                        code = RERR_PARTIAL;
        }
  
 -      if (code)
-+      if (code || am_server)
++      if (code || am_daemon || (am_server && logfile_name))
                log_exit(code, file, line);
  
        if (verbose > 2) {
@@ -46,13 +51,9 @@ are doing, similar to how a daemon logs its actions.
  int module_id = -1;
  struct chmod_mode_struct *daemon_chmod_modes;
  
-@@ -328,12 +330,16 @@ static int rsync_module(int f_in, int f_
-       if (lp_read_only(i))
+@@ -329,11 +331,12 @@ static int rsync_module(int f_in, int f_
                read_only = 1;
  
-+      /* optionally use a log file instead of syslog */
-+      logfile_name = lp_log_file();
-+
        if (lp_transfer_logging(i)) {
 -              if (log_format_has(lp_log_format(i), 'i'))
 -                      daemon_log_format_has_i = 1;
@@ -125,7 +126,7 @@ are doing, similar to how a daemon logs its actions.
  extern int am_root;
  extern int am_server;
  extern int am_daemon;
-@@ -663,7 +663,7 @@ static int try_dests_reg(struct file_str
+@@ -661,7 +661,7 @@ static int try_dests_reg(struct file_str
                } else if (itemizing)
                        itemize(file, ndx, 0, stp, 0, 0, NULL);
                if (verbose > 1 && maybe_ATTRS_REPORT) {
@@ -134,7 +135,7 @@ are doing, similar to how a daemon logs its actions.
                             ? FCLIENT : FINFO;
                        rprintf(code, "%s is uptodate\n", fname);
                }
-@@ -686,7 +686,7 @@ static int try_dests_reg(struct file_str
+@@ -684,7 +684,7 @@ static int try_dests_reg(struct file_str
                if (maybe_ATTRS_REPORT
                 && ((!itemizing && verbose && match_level == 2)
                  || (verbose > 1 && match_level == 3))) {
@@ -143,7 +144,7 @@ are doing, similar to how a daemon logs its actions.
                             ? FCLIENT : FINFO;
                        rprintf(code, "%s%s\n", fname,
                                match_level == 3 ? " is uptodate" : "");
-@@ -742,7 +742,7 @@ static int try_dests_non(struct file_str
+@@ -746,7 +746,7 @@ static int try_dests_non(struct file_str
                        itemize(file, ndx, 0, &st, changes, 0, lp);
                }
                if (verbose > 1 && maybe_ATTRS_REPORT) {
@@ -152,7 +153,7 @@ are doing, similar to how a daemon logs its actions.
                             ? FCLIENT : FINFO;
                        rprintf(code, "%s is uptodate\n", fname);
                }
-@@ -1304,9 +1304,9 @@ void generate_files(int f_out, struct fi
+@@ -1309,9 +1309,9 @@ void generate_files(int f_out, struct fi
        if (protocol_version >= 29) {
                itemizing = 1;
                maybe_ATTRS_REPORT = log_format_has_i ? 0 : ATTRS_REPORT;
@@ -166,7 +167,7 @@ are doing, similar to how a daemon logs its actions.
        } else if (!am_server) {
 --- old/log.c
 +++ new/log.c
-@@ -44,17 +44,18 @@ extern int protocol_version;
+@@ -44,18 +44,19 @@ extern int protocol_version;
  extern int preserve_times;
  extern int log_format_has_i;
  extern int log_format_has_o_or_i;
@@ -184,17 +185,34 @@ are doing, similar to how a daemon logs its actions.
  static int log_initialised;
  static int logfile_was_closed;
 -static char *logfname;
- static FILE *logfile;
+-static FILE *logfile;
++static FILE *logfile_fp;
  struct stats stats;
  
+ int log_got_error = 0;
+@@ -109,10 +110,10 @@ static void logit(int priority, char *bu
+ {
+       if (logfile_was_closed)
+               logfile_reopen();
+-      if (logfile) {
+-              fprintf(logfile,"%s [%d] %s",
++      if (logfile_fp) {
++              fprintf(logfile_fp, "%s [%d] %s",
+                       timestring(time(NULL)), (int)getpid(), buf);
+-              fflush(logfile);
++              fflush(logfile_fp);
+       } else {
+               syslog(priority, "%s", buf);
+       }
 @@ -145,14 +146,14 @@ static void syslog_init()
  static void logfile_open(void)
  {
        mode_t old_umask = umask(022 | orig_umask);
 -      logfile = fopen(logfname, "a");
-+      logfile = fopen(logfile_name, "a");
++      logfile_fp = fopen(logfile_name, "a");
        umask(old_umask);
-       if (!logfile) {
+-      if (!logfile) {
++      if (!logfile_fp) {
                int fopen_errno = errno;
                /* Rsync falls back to using syslog on failure. */
                syslog_init();
@@ -204,30 +222,48 @@ are doing, similar to how a daemon logs its actions.
                rprintf(FINFO, "Ignoring \"log file\" setting.\n");
        }
  }
-@@ -171,9 +172,7 @@ void log_init(void)
+@@ -171,9 +172,11 @@ void log_init(void)
        t = time(NULL);
        localtime(&t);
  
 -      /* optionally use a log file instead of syslog */
 -      logfname = lp_log_file();
 -      if (logfname && *logfname)
++      /* Optionally use a log file instead of syslog.  (Non-daemon
++       * rsyncs will have already set logfile_name, as needed.) */
++      if (am_daemon)
++              logfile_name = lp_log_file();
 +      if (logfile_name && *logfile_name)
                logfile_open();
        else
                syslog_init();
-@@ -243,9 +242,9 @@ void rwrite(enum logcode code, char *buf
+@@ -181,10 +184,10 @@ void log_init(void)
+ void logfile_close(void)
+ {
+-      if (logfile) {
++      if (logfile_fp) {
+               logfile_was_closed = 1;
+-              fclose(logfile);
+-              logfile = NULL;
++              fclose(logfile_fp);
++              logfile_fp = NULL;
+       }
+ }
+@@ -243,9 +246,9 @@ void rwrite(enum logcode code, char *buf
  
        if (code == FCLIENT)
                code = FINFO;
 -      else if (am_daemon) {
-+      else if (logfile_name) { /* always non-NULL in the daemon */
++      else if (am_daemon || logfile_name) {
                static int in_block;
 -              char msg[2048];
 +              char msg[2048], *s;
                int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
  
                if (in_block)
-@@ -254,10 +253,11 @@ void rwrite(enum logcode code, char *buf
+@@ -254,10 +257,11 @@ void rwrite(enum logcode code, char *buf
                if (!log_initialised)
                        log_init();
                strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
@@ -241,7 +277,7 @@ are doing, similar to how a daemon logs its actions.
                        return;
        } else if (code == FLOG)
                return;
-@@ -403,26 +403,14 @@ void rflush(enum logcode code)
+@@ -403,26 +407,14 @@ void rflush(enum logcode code)
  {
        FILE *f = NULL;
  
@@ -272,7 +308,7 @@ are doing, similar to how a daemon logs its actions.
        fflush(f);
  }
  
-@@ -695,12 +683,12 @@ void log_item(struct file_struct *file, 
+@@ -695,12 +687,12 @@ void log_item(struct file_struct *file, 
  {
        char *s_or_r = am_sender ? "send" : "recv";
  
@@ -289,7 +325,7 @@ are doing, similar to how a daemon logs its actions.
        }
  }
  
-@@ -712,7 +700,7 @@ void maybe_log_item(struct file_struct *
+@@ -712,7 +704,7 @@ void maybe_log_item(struct file_struct *
                || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
        int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
        if (am_server) {
@@ -298,7 +334,7 @@ are doing, similar to how a daemon logs its actions.
                        log_item(file, &stats, iflags, buf);
        } else if (see_item || local_change || *buf
            || (S_ISDIR(file->mode) && significant_flags))
-@@ -740,10 +728,10 @@ void log_delete(char *fname, int mode)
+@@ -740,10 +732,10 @@ void log_delete(char *fname, int mode)
                              ITEM_DELETED, NULL);
        }
  
@@ -323,7 +359,7 @@ are doing, similar to how a daemon logs its actions.
        }
 --- old/options.c
 +++ new/options.c
-@@ -148,6 +148,8 @@ char *basis_dir[MAX_BASIS_DIRS+1];
+@@ -146,6 +146,8 @@ char *basis_dir[MAX_BASIS_DIRS+1];
  char *config_file = NULL;
  char *shell_cmd = NULL;
  char *log_format = NULL;
@@ -332,7 +368,7 @@ are doing, similar to how a daemon logs its actions.
  char *password_file = NULL;
  char *rsync_path = RSYNC_PATH;
  char *backup_dir = NULL;
-@@ -164,7 +166,9 @@ int verbose = 0;
+@@ -162,7 +164,9 @@ int verbose = 0;
  int quiet = 0;
  int log_before_transfer = 0;
  int log_format_has_i = 0;
@@ -342,7 +378,7 @@ are doing, similar to how a daemon logs its actions.
  int always_checksum = 0;
  int list_only = 0;
  
-@@ -361,6 +365,7 @@ void usage(enum logcode F)
+@@ -359,6 +363,7 @@ void usage(enum logcode F)
    rprintf(F,"     --progress              show progress during transfer\n");
    rprintf(F," -P                          same as --partial --progress\n");
    rprintf(F," -i, --itemize-changes       output a change-summary for all updates\n");
@@ -350,7 +386,7 @@ are doing, similar to how a daemon logs its actions.
    rprintf(F,"     --log-format=FORMAT     output filenames using the specified format\n");
    rprintf(F,"     --password-file=FILE    read password from FILE\n");
    rprintf(F,"     --list-only             list the files instead of copying them\n");
-@@ -494,6 +499,7 @@ static struct poptOption long_options[] 
+@@ -492,6 +497,7 @@ static struct poptOption long_options[] 
    {"partial-dir",      0,  POPT_ARG_STRING, &partial_dir, 0, 0, 0 },
    {"delay-updates",    0,  POPT_ARG_NONE,   &delay_updates, 0, 0, 0 },
    {"prune-empty-dirs",'m', POPT_ARG_NONE,   &prune_empty_dirs, 0, 0, 0 },
@@ -358,7 +394,7 @@ are doing, similar to how a daemon logs its actions.
    {"log-format",       0,  POPT_ARG_STRING, &log_format, 0, 0, 0 },
    {"itemize-changes", 'i', POPT_ARG_NONE,   0, 'i', 0, 0 },
    {"bwlimit",          0,  POPT_ARG_INT,    &bwlimit, 0, 0, 0 },
-@@ -1314,6 +1320,21 @@ int parse_arguments(int *argc, const cha
+@@ -1311,6 +1317,21 @@ int parse_arguments(int *argc, const cha
        if (log_format_has_i || log_format_has(log_format, 'o'))
                log_format_has_o_or_i = 1;
  
@@ -434,7 +470,7 @@ are doing, similar to how a daemon logs its actions.
                        rprintf(code, "%s\n", fname);
 --- old/rsync.h
 +++ new/rsync.h
-@@ -156,8 +156,8 @@
+@@ -158,8 +158,8 @@
  
  
  /* Log-message categories.  Only FERROR and FINFO get sent over the socket.