e047d96f695bb322a8e8776e283179b7d0c0034f
[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         if (am_daemon) {
36 #ifdef LOG_DAEMON
37                 openlog("rsyncd", LOG_PID, LOG_DAEMON);
38 #else /* for old systems that have no facility codes. */
39                 openlog("rsyncd", LOG_PID);
40 #endif
41         }
42
43         va_start(ap, format);
44
45 #if HAVE_VSNPRINTF
46         len = vsnprintf(buf, sizeof(buf)-1, format, ap);
47 #else
48         len = vsprintf(buf, format, ap);
49 #endif
50         va_end(ap);
51
52         if (len < 0) exit_cleanup(1);
53
54         buf[len] = 0;
55
56         if (am_daemon) {
57                 int priority = LOG_INFO;
58                 if (fd == FERROR) priority = LOG_WARNING;
59                 
60                 syslog(priority, "%s", buf);
61                 return;
62         }
63
64         if (fd == FERROR) {
65                 f = stderr;
66         } 
67
68         if (fd == FINFO) {
69                 extern int am_server;
70                 if (am_server) 
71                         f = stderr;
72                 else
73                         f = stdout;
74         } 
75
76         if (!f) exit_cleanup(1);
77
78         if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1);
79 }
80
81 void rflush(int fd)
82 {
83         FILE *f = NULL;
84         extern int am_daemon;
85         
86         if (am_daemon) {
87                 return;
88         }
89
90         if (fd == FERROR) {
91                 f = stderr;
92         } 
93
94         if (fd == FINFO) {
95                 extern int am_server;
96                 if (am_server) 
97                         f = stderr;
98                 else
99                         f = stdout;
100         } 
101
102         if (!f) exit_cleanup(1);
103         fflush(f);
104 }
105