vsprintf returns char* on sunos4
[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 /* this is the rsync debugging function. Call it with FINFO or FERROR */
27 void rprintf(int fd, const char *format, ...)
28 {
29         va_list ap;  
30         char buf[1024];
31         int len;
32         FILE *f=NULL;
33         extern int am_daemon;
34
35         va_start(ap, format);
36
37 #if HAVE_VSNPRINTF
38         len = vsnprintf(buf, sizeof(buf)-1, format, ap);
39 #else
40         vsprintf(buf, format, ap);
41         len = strlen(buf);
42 #endif
43         va_end(ap);
44
45         if (len < 0) exit_cleanup(1);
46
47         if (len > sizeof(buf)-1) exit_cleanup(1);
48
49         buf[len] = 0;
50
51         if (am_daemon) {
52                 static int initialised;
53                 int priority = LOG_INFO;
54                 if (fd == FERROR) priority = LOG_WARNING;
55
56                 if (!initialised) {
57                         initialised = 1;
58 #ifdef LOG_DAEMON
59                         openlog("rsyncd", LOG_PID, lp_syslog_facility());
60 #else
61                         openlog("rsyncd", LOG_PID);
62 #endif
63                 }
64                 
65                 syslog(priority, "%s", buf);
66                 return;
67         }
68
69         if (fd == FERROR) {
70                 f = stderr;
71         } 
72
73         if (fd == FINFO) {
74                 extern int am_server;
75                 if (am_server) 
76                         f = stderr;
77                 else
78                         f = stdout;
79         } 
80
81         if (!f) exit_cleanup(1);
82
83         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
84 }
85
86 void rflush(int fd)
87 {
88         FILE *f = NULL;
89         extern int am_daemon;
90         
91         if (am_daemon) {
92                 return;
93         }
94
95         if (fd == FERROR) {
96                 f = stderr;
97         } 
98
99         if (fd == FINFO) {
100                 extern int am_server;
101                 if (am_server) 
102                         f = stderr;
103                 else
104                         f = stdout;
105         } 
106
107         if (!f) exit_cleanup(1);
108         fflush(f);
109 }
110