don't complain about not setting times on directories
[rsync/rsync.git] / log.c
CommitLineData
0b76cd63
AT
1/*
2 Copyright (C) Andrew Tridgell 1998
3
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.
8
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.
13
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.
17*/
18
19/*
20 logging and utility functions
21
22 tridge, May 1998
23 */
24#include "rsync.h"
25
4f6325c3
AT
26static FILE *logfile;
27
e0414f42
AT
28
29/****************************************************************************
30 return the date and time as a string
31****************************************************************************/
32static char *timestring(void )
33{
34 static char TimeBuf[200];
35 time_t t = time(NULL);
36 struct tm *tm = localtime(&t);
37
38#ifdef HAVE_STRFTIME
39 strftime(TimeBuf,sizeof(TimeBuf)-1,"%Y/%m/%d %T",tm);
40#else
41 strlcpy(TimeBuf, asctime(tm), sizeof(TimeBuf)-1);
42#endif
43
44 if (TimeBuf[strlen(TimeBuf)-1] == '\n') {
45 TimeBuf[strlen(TimeBuf)-1] == 0;
46 }
47
48 return(TimeBuf);
49}
50
4f6325c3
AT
51static void logit(int priority, char *buf)
52{
53 if (logfile) {
e0414f42 54 fprintf(logfile,"%s %s", timestring(), buf);
4f6325c3
AT
55 fflush(logfile);
56 } else {
57 syslog(priority, "%s", buf);
58 }
59}
e42c9458 60
1a016bfd
AT
61void log_open(void)
62{
63 static int initialised;
64 int options = LOG_PID;
bcf5b133 65 time_t t;
1a016bfd
AT
66
67 if (initialised) return;
68 initialised = 1;
69
4f6325c3
AT
70 if (lp_log_file()) {
71 logfile = fopen(lp_log_file(), "a");
72 return;
73 }
74
1a016bfd
AT
75#ifdef LOG_NDELAY
76 options |= LOG_NDELAY;
77#endif
78
79#ifdef LOG_DAEMON
80 openlog("rsyncd", options, lp_syslog_facility());
81#else
82 openlog("rsyncd", options);
83#endif
84
85#ifndef LOG_NDELAY
4f6325c3 86 logit(LOG_INFO,"rsyncd started\n");
1a016bfd 87#endif
bcf5b133
AT
88
89 /* this looks pointless, but it is needed in order for the
90 C library on some systems to fetch the timezone info
91 before the chroot */
92 t = time(NULL);
93 localtime(&t);
1a016bfd
AT
94}
95
96
0b76cd63
AT
97/* this is the rsync debugging function. Call it with FINFO or FERROR */
98void rprintf(int fd, const char *format, ...)
99{
100 va_list ap;
101 char buf[1024];
102 int len;
103 FILE *f=NULL;
104 extern int am_daemon;
8d9dc9f9
AT
105 /* recursion can happen with certain fatal conditions */
106 static int depth;
107
108 if (depth) return;
109
110 depth++;
0b76cd63
AT
111
112 va_start(ap, format);
e42c9458 113 len = vslprintf(buf, sizeof(buf)-1, format, ap);
0b76cd63
AT
114 va_end(ap);
115
116 if (len < 0) exit_cleanup(1);
117
45ccc5c0
AT
118 if (len > sizeof(buf)-1) exit_cleanup(1);
119
ff8b29b8
AT
120 buf[len] = 0;
121
122 if (am_daemon) {
123 int priority = LOG_INFO;
124 if (fd == FERROR) priority = LOG_WARNING;
45ccc5c0 125
1a016bfd 126 log_open();
8d9dc9f9 127 if (!io_multiplex_write(fd, buf, strlen(buf))) {
4f6325c3 128 logit(priority, buf);
8d9dc9f9
AT
129 }
130
131 depth--;
ff8b29b8 132 return;
0b76cd63
AT
133 }
134
ff8b29b8
AT
135 if (fd == FERROR) {
136 f = stderr;
137 }
138
139 if (fd == FINFO) {
140 extern int am_server;
141 if (am_server)
142 f = stderr;
143 else
144 f = stdout;
145 }
146
0b76cd63
AT
147 if (!f) exit_cleanup(1);
148
149 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
8d9dc9f9 150
bcf5b133 151 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
eb86d661 152
8d9dc9f9 153 depth--;
0b76cd63
AT
154}
155
156void rflush(int fd)
157{
158 FILE *f = NULL;
159 extern int am_daemon;
160
161 if (am_daemon) {
162 return;
163 }
164
165 if (fd == FERROR) {
166 f = stderr;
167 }
168
169 if (fd == FINFO) {
170 extern int am_server;
171 if (am_server)
172 f = stderr;
173 else
174 f = stdout;
175 }
176
177 if (!f) exit_cleanup(1);
178 fflush(f);
179}
180