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