this is a large commit which adds io multiplexing, thus giving error
[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
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
51 /* this is the rsync debugging function. Call it with FINFO or FERROR */
52 void 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;
59         /* recursion can happen with certain fatal conditions */
60         static int depth;
61
62         if (depth) return;
63
64         depth++;
65
66         va_start(ap, format);
67         len = vslprintf(buf, sizeof(buf)-1, format, ap);
68         va_end(ap);
69
70         if (len < 0) exit_cleanup(1);
71
72         if (len > sizeof(buf)-1) exit_cleanup(1);
73
74         buf[len] = 0;
75
76         if (am_daemon) {
77                 int priority = LOG_INFO;
78                 if (fd == FERROR) priority = LOG_WARNING;
79
80                 log_open();
81                 if (!io_multiplex_write(fd, buf, strlen(buf))) {
82                         syslog(priority, "%s", buf);
83                 }
84
85                 depth--;
86                 return;
87         }
88
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
101         if (!f) exit_cleanup(1);
102
103         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
104
105         depth--;
106 }
107
108 void 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