2 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
20 logging and utility functions
29 /****************************************************************************
30 return the date and time as a string
31 ****************************************************************************/
32 static char *timestring(void )
34 static char TimeBuf[200];
35 time_t t = time(NULL);
36 struct tm *tm = localtime(&t);
39 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
41 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf)-1);
44 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
45 TimeBuf[strlen(TimeBuf)-1] = 0;
51 static void logit(int priority, char *buf)
54 fprintf(logfile,"%s %s", timestring(), buf);
57 syslog(priority, "%s", buf);
63 static int initialised;
64 int options = LOG_PID;
67 if (initialised) return;
71 logfile = fopen(lp_log_file(), "a");
76 options |= LOG_NDELAY;
80 openlog("rsyncd", options, lp_syslog_facility());
82 openlog("rsyncd", options);
86 logit(LOG_INFO,"rsyncd started\n");
89 /* this looks pointless, but it is needed in order for the
90 C library on some systems to fetch the timezone info
97 /* this is the rsync debugging function. Call it with FINFO or FERROR */
98 void rprintf(int fd, const char *format, ...)
104 extern int am_daemon;
105 /* recursion can happen with certain fatal conditions */
112 va_start(ap, format);
113 len = vslprintf(buf, sizeof(buf)-1, format, ap);
116 if (len < 0) exit_cleanup(1);
118 if (len > sizeof(buf)-1) exit_cleanup(1);
123 if (am_daemon) logit(LOG_INFO, buf);
129 int priority = LOG_INFO;
130 if (fd == FERROR) priority = LOG_WARNING;
133 if (!io_multiplex_write(fd, buf, strlen(buf))) {
134 logit(priority, buf);
146 extern int am_server;
153 if (!f) exit_cleanup(1);
155 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
157 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
165 extern int am_daemon;
176 extern int am_server;
183 if (!f) exit_cleanup(1);
188 /* log the outgoing transfer of a file */
189 void log_send(struct file_struct *file)
191 extern int module_id;
192 if (lp_transfer_logging(module_id)) {
193 rprintf(FLOG,"Sending %s [%s] %.0f %s\n",
194 client_name(0), client_addr(0),
195 (double)file->length, f_name(file));
199 /* log the incoming transfer of a file */
200 void log_recv(struct file_struct *file)
202 extern int module_id;
203 if (lp_transfer_logging(module_id)) {
204 rprintf(FLOG,"Receiving %s [%s] %.0f %s\n",
205 client_name(0), client_addr(0),
206 (double)file->length, f_name(file));
210 /* log the incoming transfer of a file for interactive use, this
211 will be called at the end where the client was run */
212 void log_transfer(struct file_struct *file, char *fname)
216 if (!verbose) return;
218 rprintf(FINFO,"%s\n", fname);