+ if (log_initialised)
+ return;
+ log_initialised = 1;
+
+ /* This looks pointless, but it is needed in order for the
+ * C library on some systems to fetch the timezone info
+ * before the chroot. */
+ timestring(time(NULL));
+
+ /* Optionally use a log file instead of syslog. (Non-daemon
+ * rsyncs will have already set logfile_name, as needed.) */
+ if (am_daemon && !logfile_name)
+ logfile_name = lp_log_file();
+ if (logfile_name && *logfile_name)
+ logfile_open();
+ else
+ syslog_init();
+}
+
+void logfile_close(void)
+{
+ if (logfile_fp) {
+ logfile_was_closed = 1;
+ fclose(logfile_fp);
+ logfile_fp = NULL;
+ }
+}
+
+void logfile_reopen(void)
+{
+ if (logfile_was_closed) {
+ logfile_was_closed = 0;
+ logfile_open();
+ }
+}
+
+static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
+{
+ const char *s, *end = buf + len;
+ for (s = buf; s < end; s++) {
+ if ((s < end - 4
+ && *s == '\\' && s[1] == '#'
+ && isdigit(*(uchar*)(s+2))
+ && isdigit(*(uchar*)(s+3))
+ && isdigit(*(uchar*)(s+4)))
+ || (*s != '\t'
+ && ((use_isprint && !isprint(*(uchar*)s))
+ || *(uchar*)s < ' '))) {
+ if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
+ exit_cleanup(RERR_MESSAGEIO);
+ fprintf(f, "\\#%03o", *(uchar*)s);
+ buf = s + 1;
+ }