- only keep a partial file if some literal data has been transferred,
[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;
31
32 if (initialised) return;
33 initialised = 1;
34
35#ifdef LOG_NDELAY
36 options |= LOG_NDELAY;
37#endif
38
39#ifdef LOG_DAEMON
40 openlog("rsyncd", options, lp_syslog_facility());
41#else
42 openlog("rsyncd", options);
43#endif
44
45#ifndef LOG_NDELAY
46 syslog(LOG_INFO,"rsyncd started\n");
47#endif
48}
49
50
0b76cd63
AT
51/* this is the rsync debugging function. Call it with FINFO or FERROR */
52void rprintf(int fd, const char *format, ...)
53{
54 va_list ap;
55 char buf[1024];
56 int len;
57 FILE *f=NULL;
58 extern int am_daemon;
8d9dc9f9
AT
59 /* recursion can happen with certain fatal conditions */
60 static int depth;
61
62 if (depth) return;
63
64 depth++;
0b76cd63
AT
65
66 va_start(ap, format);
e42c9458 67 len = vslprintf(buf, sizeof(buf)-1, format, ap);
0b76cd63
AT
68 va_end(ap);
69
70 if (len < 0) exit_cleanup(1);
71
45ccc5c0
AT
72 if (len > sizeof(buf)-1) exit_cleanup(1);
73
ff8b29b8
AT
74 buf[len] = 0;
75
76 if (am_daemon) {
77 int priority = LOG_INFO;
78 if (fd == FERROR) priority = LOG_WARNING;
45ccc5c0 79
1a016bfd 80 log_open();
8d9dc9f9
AT
81 if (!io_multiplex_write(fd, buf, strlen(buf))) {
82 syslog(priority, "%s", buf);
83 }
84
85 depth--;
ff8b29b8 86 return;
0b76cd63
AT
87 }
88
ff8b29b8
AT
89 if (fd == FERROR) {
90 f = stderr;
91 }
92
93 if (fd == FINFO) {
94 extern int am_server;
95 if (am_server)
96 f = stderr;
97 else
98 f = stdout;
99 }
100
0b76cd63
AT
101 if (!f) exit_cleanup(1);
102
103 if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
8d9dc9f9
AT
104
105 depth--;
0b76cd63
AT
106}
107
108void rflush(int fd)
109{
110 FILE *f = NULL;
111 extern int am_daemon;
112
113 if (am_daemon) {
114 return;
115 }
116
117 if (fd == FERROR) {
118 f = stderr;
119 }
120
121 if (fd == FINFO) {
122 extern int am_server;
123 if (am_server)
124 f = stderr;
125 else
126 f = stdout;
127 }
128
129 if (!f) exit_cleanup(1);
130 fflush(f);
131}
132