removed old "make dist" target
[rsync/rsync.git] / log.c
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
26
27 void log_open(void)
28 {
29         static int initialised;
30         int options = LOG_PID;
31         time_t t;
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
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);
55 }
56                 
57
58 /* this is the rsync debugging function. Call it with FINFO or FERROR */
59 void 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;
66         /* recursion can happen with certain fatal conditions */
67         static int depth;
68
69         if (depth) return;
70
71         depth++;
72
73         va_start(ap, format);
74         len = vslprintf(buf, sizeof(buf)-1, format, ap);
75         va_end(ap);
76
77         if (len < 0) exit_cleanup(1);
78
79         if (len > sizeof(buf)-1) exit_cleanup(1);
80
81         buf[len] = 0;
82
83         if (am_daemon) {
84                 int priority = LOG_INFO;
85                 if (fd == FERROR) priority = LOG_WARNING;
86
87                 log_open();
88                 if (!io_multiplex_write(fd, buf, strlen(buf))) {
89                         syslog(priority, "%s", buf);
90                 }
91
92                 depth--;
93                 return;
94         }
95
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
108         if (!f) exit_cleanup(1);
109
110         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
111
112         if (buf[len-1] == '\r' || buf[len-1] == '\n') fflush(f);
113
114         depth--;
115 }
116
117 void 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