[BACK]Return to log.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rsync

Annotation of src/usr.bin/rsync/log.c, Revision 1.9

1.9     ! deraadt     1: /*     $Id: log.c,v 1.8 2019/06/27 18:03:37 deraadt Exp $ */
1.1       benno       2: /*
                      3:  * Copyright (c) 2019 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <errno.h>
                     18: #include <stdarg.h>
                     19: #include <stdint.h>
                     20: #include <stdio.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
                     24: #include "extern.h"
                     25:
1.7       benno      26: extern int verbose;
                     27:
1.1       benno      28: /*
                     29:  * Log a message at level "level", starting at zero, which corresponds
                     30:  * to the current verbosity level opts->verbose (whose verbosity starts
                     31:  * at one).
                     32:  */
                     33: void
1.9     ! deraadt    34: rsync_log(int level, const char *fmt, ...)
1.1       benno      35: {
                     36:        char    *buf = NULL;
                     37:        va_list  ap;
                     38:
1.7       benno      39:        if (verbose < level + 1)
1.1       benno      40:                return;
                     41:
1.3       deraadt    42:        if (fmt != NULL) {
1.1       benno      43:                va_start(ap, fmt);
1.8       deraadt    44:                if (vasprintf(&buf, fmt, ap) == -1) {
1.1       benno      45:                        va_end(ap);
                     46:                        return;
                     47:                }
                     48:                va_end(ap);
                     49:        }
                     50:
1.3       deraadt    51:        if (level <= 0 && buf != NULL)
1.1       benno      52:                fprintf(stderr, "%s\n", buf);
                     53:        else if (level > 0)
1.9     ! deraadt    54:                fprintf(stderr, "%s: %s%s\n", getprogname(),
1.3       deraadt    55:                    (buf != NULL) ? ": " : "",
                     56:                    (buf != NULL) ? buf : "");
1.1       benno      57:        free(buf);
                     58: }
                     59:
                     60: /*
                     61:  * This reports an error---not a warning.
                     62:  * However, it is not like errx(3) in that it does not exit.
                     63:  */
                     64: void
1.9     ! deraadt    65: rsync_errx(const char *fmt, ...)
1.1       benno      66: {
                     67:        char    *buf = NULL;
                     68:        va_list  ap;
                     69:
1.3       deraadt    70:        if (fmt != NULL) {
1.1       benno      71:                va_start(ap, fmt);
1.8       deraadt    72:                if (vasprintf(&buf, fmt, ap) == -1) {
1.1       benno      73:                        va_end(ap);
                     74:                        return;
                     75:                }
                     76:                va_end(ap);
                     77:        }
                     78:
1.9     ! deraadt    79:        fprintf(stderr, "%s: error%s%s\n", getprogname(),
1.6       benno      80:           (buf != NULL) ? ": " : "",
                     81:           (buf != NULL) ? buf : "");
1.1       benno      82:        free(buf);
                     83: }
                     84:
                     85: /*
                     86:  * This reports an error---not a warning.
                     87:  * However, it is not like err(3) in that it does not exit.
                     88:  */
                     89: void
1.9     ! deraadt    90: rsync_err(const char *fmt, ...)
1.1       benno      91: {
                     92:        char    *buf = NULL;
                     93:        va_list  ap;
                     94:        int      er = errno;
                     95:
1.3       deraadt    96:        if (fmt != NULL) {
1.1       benno      97:                va_start(ap, fmt);
1.8       deraadt    98:                if (vasprintf(&buf, fmt, ap) == -1) {
1.1       benno      99:                        va_end(ap);
                    100:                        return;
                    101:                }
                    102:                va_end(ap);
                    103:        }
                    104:
1.9     ! deraadt   105:        fprintf(stderr, "%s: error%s%s: %s\n", getprogname(),
1.6       benno     106:           (buf != NULL) ? ": " : "",
                    107:           (buf != NULL) ? buf : "", strerror(er));
1.1       benno     108:        free(buf);
                    109: }
                    110:
                    111: /*
                    112:  * Prints a non-terminal error message, that is, when reporting on the
                    113:  * chain of functions from which the actual warning occurred.
                    114:  */
                    115: void
1.9     ! deraadt   116: rsync_errx1(const char *fmt, ...)
1.1       benno     117: {
                    118:        char    *buf = NULL;
                    119:        va_list  ap;
                    120:
1.7       benno     121:        if (verbose < 1)
1.1       benno     122:                return;
                    123:
1.3       deraadt   124:        if (fmt != NULL) {
1.1       benno     125:                va_start(ap, fmt);
1.8       deraadt   126:                if (vasprintf(&buf, fmt, ap) == -1) {
1.1       benno     127:                        va_end(ap);
                    128:                        return;
                    129:                }
                    130:                va_end(ap);
                    131:        }
                    132:
1.9     ! deraadt   133:        fprintf(stderr, "%s: error%s%s\n", getprogname(),
1.6       benno     134:           (buf != NULL) ? ": " : "",
                    135:           (buf != NULL) ? buf : "");
1.1       benno     136:        free(buf);
                    137: }
                    138:
                    139: /*
                    140:  * Prints a warning message.
                    141:  */
                    142: void
1.9     ! deraadt   143: rsync_warnx(const char *fmt, ...)
1.1       benno     144: {
                    145:        char    *buf = NULL;
                    146:        va_list  ap;
                    147:
1.3       deraadt   148:        if (fmt != NULL) {
1.1       benno     149:                va_start(ap, fmt);
1.8       deraadt   150:                if (vasprintf(&buf, fmt, ap) == -1) {
1.1       benno     151:                        va_end(ap);
                    152:                        return;
                    153:                }
                    154:                va_end(ap);
                    155:        }
                    156:
1.9     ! deraadt   157:        fprintf(stderr, "%s: warning%s%s\n", getprogname(),
1.6       benno     158:           (buf != NULL) ? ": " : "",
                    159:           (buf != NULL) ? buf : "");
1.1       benno     160:        free(buf);
                    161: }
                    162:
                    163: /*
                    164:  * Prints a warning with an errno.
                    165:  * It uses a level detector for when to inhibit printing.
                    166:  */
                    167: void
1.9     ! deraadt   168: rsync_warn(int level, const char *fmt, ...)
1.1       benno     169: {
                    170:        char    *buf = NULL;
                    171:        va_list  ap;
                    172:        int      er = errno;
                    173:
1.7       benno     174:        if (verbose < level)
1.1       benno     175:                return;
                    176:
1.3       deraadt   177:        if (fmt != NULL) {
1.1       benno     178:                va_start(ap, fmt);
1.8       deraadt   179:                if (vasprintf(&buf, fmt, ap) == -1) {
1.1       benno     180:                        va_end(ap);
                    181:                        return;
                    182:                }
                    183:                va_end(ap);
                    184:        }
                    185:
1.9     ! deraadt   186:        fprintf(stderr, "%s: warning%s%s: %s\n", getprogname(),
1.6       benno     187:           (buf != NULL) ? ": " : "",
                    188:           (buf != NULL) ? buf : "", strerror(er));
1.1       benno     189:        free(buf);
                    190: }