wanr people who use path names to rsync ::
[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
e42c9458 26
1a016bfd
AT
27void log_open(void)
28{
29 static int initialised;
30 int options = LOG_PID;
bcf5b133 31 time_t t;
1a016bfd
AT
32
33 if (initialised) return;
34 initialised = 1;
35
36#ifdef LOG_NDELAY
37 options |= LOG_NDELAY;
38#endif
39
40#ifdef LOG_DAEMON
41 openlog("rsyncd", options, lp_syslog_facility());
42#else
43 openlog("rsyncd", options);
44#endif
45
46#ifndef LOG_NDELAY
47 syslog(LOG_INFO,"rsyncd started\n");
48#endif
bcf5b133
AT
49
50 /* this looks pointless, but it is needed in order for the
51 C library on some systems to fetch the timezone info
52 before the chroot */
53 t = time(NULL);
54 localtime(&t);
1a016bfd
AT
55}
56
57
0b76cd63
AT
58/* this is the rsync debugging function. Call it with FINFO or FERROR */
59void rprintf(int fd, const char *format, ...)
60{
61 va_list ap;
62 char buf[1024];
63 int len;
64 FILE *f=NULL;
65 extern int am_daemon;
8d9dc9f9
AT
66 /* recursion can happen with certain fatal conditions */
67 static int depth;
68
69 if (depth) return;
70
71 depth++;
0b76cd63
AT
72
73 va_start(ap, format);
e42c9458 74 len = vslprintf(buf, sizeof(buf)-1, format, ap);
0b76cd63
AT
75 va_end(ap);
76
77 if (len < 0) exit_cleanup(1);
78
45ccc5c0
AT
79 if (len > sizeof(buf)-1) exit_cleanup(1);
80
ff8b29b8
AT
81 buf[len] = 0;
82
83 if (am_daemon) {
84 int priority = LOG_INFO;
85 if (fd == FERROR) priority = LOG_WARNING;
45ccc5c0 86
1a016bfd 87 log_open();
8d9dc9f9
AT
88 if (!io_multiplex_write(fd, buf, strlen(buf))) {
89 syslog(priority, "%s", buf);
90 }
91
92 depth--;
ff8b29b8 93 return;
0b76cd63
AT
94 }
95
ff8b29b8
AT
96 if (fd == FERROR) {
97 f = stderr;
98 }
99
100 if (fd == FINFO) {
101 extern int am_server;
102 if (am_server)
103 f = stderr;
104 else
105 f = stdout;
106 }
107
0b76cd63
AT
108 if (!f) exit_cleanup(1);
109
110 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
8d9dc9f9 111
bcf5b133 112 if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
eb86d661 113
8d9dc9f9 114 depth--;
0b76cd63
AT
115}
116
117void rflush(int fd)
118{
119 FILE *f = NULL;
120 extern int am_daemon;
121
122 if (am_daemon) {
123 return;
124 }
125
126 if (fd == FERROR) {
127 f = stderr;
128 }
129
130 if (fd == FINFO) {
131 extern int am_server;
132 if (am_server)
133 f = stderr;
134 else
135 f = stdout;
136 }
137
138 if (!f) exit_cleanup(1);
139 fflush(f);
140}
141