if f_in == f_out then don't close one of them
[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
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
60         va_start(ap, format);
61         len = vslprintf(buf, sizeof(buf)-1, format, ap);
62         va_end(ap);
63
64         if (len < 0) exit_cleanup(1);
65
66         if (len > sizeof(buf)-1) exit_cleanup(1);
67
68         buf[len] = 0;
69
70         if (am_daemon) {
71                 int priority = LOG_INFO;
72                 if (fd == FERROR) priority = LOG_WARNING;
73
74                 log_open();
75                 syslog(priority, "%s", buf);
76                 return;
77         }
78
79         if (fd == FERROR) {
80                 f = stderr;
81         } 
82
83         if (fd == FINFO) {
84                 extern int am_server;
85                 if (am_server) 
86                         f = stderr;
87                 else
88                         f = stdout;
89         } 
90
91         if (!f) exit_cleanup(1);
92
93         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
94 }
95
96 void rflush(int fd)
97 {
98         FILE *f = NULL;
99         extern int am_daemon;
100         
101         if (am_daemon) {
102                 return;
103         }
104
105         if (fd == FERROR) {
106                 f = stderr;
107         } 
108
109         if (fd == FINFO) {
110                 extern int am_server;
111                 if (am_server) 
112                         f = stderr;
113                 else
114                         f = stdout;
115         } 
116
117         if (!f) exit_cleanup(1);
118         fflush(f);
119 }
120