Annotation of embedaddon/ntp/libntp/msyslog.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * msyslog - either send a message to the terminal or print it on
                      3:  *          the standard output.
                      4:  *
                      5:  * Converted to use varargs, much better ... jks
                      6:  */
                      7: 
                      8: #ifdef HAVE_CONFIG_H
                      9: # include <config.h>
                     10: #endif
                     11: 
                     12: #include <sys/types.h>
                     13: #ifdef HAVE_UNISTD_H
                     14: # include <unistd.h>
                     15: #endif
                     16: #include <stdio.h>
                     17: 
                     18: #include "ntp.h"
                     19: #include "ntp_string.h"
                     20: #include "ntp_syslog.h"
                     21: 
                     22: #ifdef SYS_WINNT
                     23: # include <stdarg.h>
                     24: # include "..\ports\winnt\libntp\messages.h"
                     25: #endif
                     26: 
                     27: 
                     28: int    syslogit = 1;
                     29: int    msyslog_term = FALSE;   /* duplicate to stdout/err */
                     30: FILE * syslog_file;
                     31: 
                     32: u_int32 ntp_syslogmask =  ~(u_int32)0; /* libntp default is all lit */
                     33: 
                     34: extern char *  progname;
                     35: 
                     36: /* Declare the local functions */
                     37: void   addto_syslog    (int, const char *);
                     38: void   format_errmsg   (char *, size_t, const char *, int);
                     39: 
                     40: 
                     41: /*
                     42:  * This routine adds the contents of a buffer to the syslog or an
                     43:  * application-specific logfile.
                     44:  */
                     45: void
                     46: addto_syslog(
                     47:        int             level,
                     48:        const char *    msg
                     49:        )
                     50: {
                     51:        static char *   prevcall_progname;
                     52:        static char *   prog;
                     53:        const char      nl[] = "\n";
                     54:        const char      empty[] = "";
                     55:        FILE *          term_file;
                     56:        int             log_to_term;
                     57:        int             log_to_file;
                     58:        const char *    nl_or_empty;
                     59:        const char *    human_time;
                     60: 
                     61:        /* setup program basename static var prog if needed */
                     62:        if (progname != prevcall_progname) {
                     63:                prevcall_progname = progname;
                     64:                prog = strrchr(progname, DIR_SEP);
                     65:                if (prog != NULL)
                     66:                        prog++;
                     67:                else
                     68:                        prog = progname;
                     69:        }
                     70: 
                     71:        log_to_term = msyslog_term;
                     72:        log_to_file = FALSE;
                     73: #if !defined(VMS) && !defined(SYS_VXWORKS)
                     74:        if (syslogit)
                     75:                syslog(level, "%s", msg);
                     76:        else
                     77: #endif
                     78:                if (syslog_file != NULL)
                     79:                        log_to_file = TRUE;
                     80:                else
                     81:                        log_to_term = TRUE;
                     82: #if DEBUG
                     83:        if (debug > 0)
                     84:                log_to_term = TRUE;
                     85: #endif
                     86:        if (!(log_to_file || log_to_term))
                     87:                return;
                     88: 
                     89:        /* syslog() adds the timestamp, name, and pid */
                     90:        human_time = humanlogtime();
                     91: 
                     92:        /* syslog() adds trailing \n if not present */
                     93:        if ('\n' != msg[strlen(msg) - 1])
                     94:                nl_or_empty = nl;
                     95:        else
                     96:                nl_or_empty = empty;
                     97: 
                     98:        if (log_to_term) {
                     99:                term_file = (level <= LOG_ERR)
                    100:                                ? stderr
                    101:                                : stdout;
                    102:                fprintf(term_file, "%s %s[%d]: %s%s", human_time, prog,
                    103:                        (int)getpid(), msg, nl_or_empty);
                    104:                fflush(term_file);
                    105:        }
                    106: 
                    107:        if (log_to_file) {
                    108:                fprintf(syslog_file, "%s %s[%d]: %s%s", human_time,
                    109:                        prog, (int)getpid(), msg, nl_or_empty);
                    110:                fflush(syslog_file);
                    111:        }
                    112: }
                    113: 
                    114: 
                    115: void
                    116: format_errmsg(
                    117:        char *          nfmt,
                    118:        size_t          lennfmt,
                    119:        const char *    fmt,
                    120:        int             errval
                    121:        )
                    122: {
                    123:        char c;
                    124:        char *n;
                    125:        const char *f;
                    126:        size_t len;
                    127:        char *err;
                    128: 
                    129:        n = nfmt;
                    130:        f = fmt;
                    131:        while ((c = *f++) != '\0' && n < (nfmt + lennfmt - 1)) {
                    132:                if (c != '%') {
                    133:                        *n++ = c;
                    134:                        continue;
                    135:                }
                    136:                if ((c = *f++) != 'm') {
                    137:                        *n++ = '%';
                    138:                        if ('\0' == c)
                    139:                                break;
                    140:                        *n++ = c;
                    141:                        continue;
                    142:                }
                    143:                err = strerror(errval);
                    144:                len = strlen(err);
                    145: 
                    146:                /* Make sure we have enough space for the error message */
                    147:                if ((n + len) < (nfmt + lennfmt - 1)) {
                    148:                        memcpy(n, err, len);
                    149:                        n += len;
                    150:                }
                    151:        }
                    152:        *n = '\0';
                    153: }
                    154: 
                    155: 
                    156: int
                    157: mvsnprintf(
                    158:        char *          buf,
                    159:        size_t          bufsiz,
                    160:        const char *    fmt,
                    161:        va_list         ap
                    162:        )
                    163: {
                    164: #ifndef VSNPRINTF_PERCENT_M
                    165:        char            nfmt[256];
                    166: #else
                    167:        const char *    nfmt = fmt;
                    168: #endif
                    169:        int             errval;
                    170: 
                    171:        /*
                    172:         * Save the error value as soon as possible
                    173:         */
                    174: #ifdef SYS_WINNT
                    175:        errval = GetLastError();
                    176:        if (NO_ERROR == errval)
                    177: #endif /* SYS_WINNT */
                    178:                errval = errno;
                    179: 
                    180: #ifndef VSNPRINTF_PERCENT_M
                    181:        format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
                    182: #else
                    183:        errno = errval;
                    184: #endif
                    185:        return vsnprintf(buf, bufsiz, nfmt, ap);
                    186: }
                    187: 
                    188: 
                    189: int
                    190: mvfprintf(
                    191:        FILE *          fp,
                    192:        const char *    fmt,
                    193:        va_list         ap
                    194:        )
                    195: {
                    196: #ifndef VSNPRINTF_PERCENT_M
                    197:        char            nfmt[256];
                    198: #else
                    199:        const char *    nfmt = fmt;
                    200: #endif
                    201:        int             errval;
                    202: 
                    203:        /*
                    204:         * Save the error value as soon as possible
                    205:         */
                    206: #ifdef SYS_WINNT
                    207:        errval = GetLastError();
                    208:        if (NO_ERROR == errval)
                    209: #endif /* SYS_WINNT */
                    210:                errval = errno;
                    211: 
                    212: #ifndef VSNPRINTF_PERCENT_M
                    213:        format_errmsg(nfmt, sizeof(nfmt), fmt, errval);
                    214: #else
                    215:        errno = errval;
                    216: #endif
                    217:        return vfprintf(fp, nfmt, ap);
                    218: }
                    219: 
                    220: 
                    221: int
                    222: mfprintf(
                    223:        FILE *          fp,
                    224:        const char *    fmt,
                    225:        ...
                    226:        )
                    227: {
                    228:        va_list         ap;
                    229:        int             rc;
                    230: 
                    231:        va_start(ap, fmt);
                    232:        rc = mvfprintf(fp, fmt, ap);
                    233:        va_end(ap);
                    234: 
                    235:        return rc;
                    236: }
                    237: 
                    238: 
                    239: int
                    240: mprintf(
                    241:        const char *    fmt,
                    242:        ...
                    243:        )
                    244: {
                    245:        va_list         ap;
                    246:        int             rc;
                    247: 
                    248:        va_start(ap, fmt);
                    249:        rc = mvfprintf(stdout, fmt, ap);
                    250:        va_end(ap);
                    251: 
                    252:        return rc;
                    253: }
                    254: 
                    255: 
                    256: int
                    257: msnprintf(
                    258:        char *          buf,
                    259:        size_t          bufsiz,
                    260:        const char *    fmt,
                    261:        ...
                    262:        )
                    263: {
                    264:        va_list ap;
                    265:        size_t  rc;
                    266: 
                    267:        va_start(ap, fmt);
                    268:        rc = mvsnprintf(buf, bufsiz, fmt, ap);
                    269:        va_end(ap);
                    270: 
                    271:        return rc;
                    272: }
                    273: 
                    274: 
                    275: void
                    276: msyslog(
                    277:        int             level,
                    278:        const char *    fmt,
                    279:        ...
                    280:        )
                    281: {
                    282:        char    buf[1024];
                    283:        va_list ap;
                    284: 
                    285:        va_start(ap, fmt);
                    286:        mvsnprintf(buf, sizeof(buf), fmt, ap);
                    287:        va_end(ap);
                    288:        addto_syslog(level, buf);
                    289: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>