use strlcat() strlcpy() and slprintf() whenever possible to avoid any
[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 /* this is the rsync debugging function. Call it with FINFO or FERROR */
28 void rprintf(int fd, const char *format, ...)
29 {
30         va_list ap;  
31         char buf[1024];
32         int len;
33         FILE *f=NULL;
34         extern int am_daemon;
35
36         va_start(ap, format);
37         len = vslprintf(buf, sizeof(buf)-1, format, ap);
38         va_end(ap);
39
40         if (len < 0) exit_cleanup(1);
41
42         if (len > sizeof(buf)-1) exit_cleanup(1);
43
44         buf[len] = 0;
45
46         if (am_daemon) {
47                 static int initialised;
48                 int priority = LOG_INFO;
49                 if (fd == FERROR) priority = LOG_WARNING;
50
51                 if (!initialised) {
52                         initialised = 1;
53 #ifdef LOG_DAEMON
54                         openlog("rsyncd", LOG_PID, lp_syslog_facility());
55 #else
56                         openlog("rsyncd", LOG_PID);
57 #endif
58                 }
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