| 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 | static FILE *logf; |
| 37 | if (!logf) logf = fopen(RSYNCD_LOG, "a"); |
| 38 | f = logf; |
| 39 | if (!f) return; |
| 40 | } |
| 41 | |
| 42 | va_start(ap, format); |
| 43 | |
| 44 | #if HAVE_VSNPRINTF |
| 45 | len = vsnprintf(buf, sizeof(buf)-1, format, ap); |
| 46 | #else |
| 47 | len = vsprintf(buf, format, ap); |
| 48 | #endif |
| 49 | va_end(ap); |
| 50 | |
| 51 | if (len < 0) exit_cleanup(1); |
| 52 | |
| 53 | if (!am_daemon) { |
| 54 | if (fd == FERROR) { |
| 55 | f = stderr; |
| 56 | } |
| 57 | |
| 58 | if (fd == FINFO) { |
| 59 | extern int am_server; |
| 60 | if (am_server) |
| 61 | f = stderr; |
| 62 | else |
| 63 | f = stdout; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | if (!f) exit_cleanup(1); |
| 68 | |
| 69 | if (fwrite(buf, len, 1, f) != 1) exit_cleanup(1); |
| 70 | } |
| 71 | |
| 72 | void rflush(int fd) |
| 73 | { |
| 74 | FILE *f = NULL; |
| 75 | extern int am_daemon; |
| 76 | |
| 77 | if (am_daemon) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | if (fd == FERROR) { |
| 82 | f = stderr; |
| 83 | } |
| 84 | |
| 85 | if (fd == FINFO) { |
| 86 | extern int am_server; |
| 87 | if (am_server) |
| 88 | f = stderr; |
| 89 | else |
| 90 | f = stdout; |
| 91 | } |
| 92 | |
| 93 | if (!f) exit_cleanup(1); |
| 94 | fflush(f); |
| 95 | } |
| 96 | |