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