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

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

1.9     ! nicm        1: /* $OpenBSD: log.c,v 1.8 2012/05/30 15:01:21 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
                     15:  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
                     16:  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
                     18:
                     19: #include <sys/types.h>
                     20:
                     21: #include <errno.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <syslog.h>
                     26: #include <time.h>
                     27:
                     28: #include "tmux.h"
                     29:
                     30: /* Log file, if needed. */
1.8       nicm       31: FILE           *log_file;
1.1       nicm       32:
                     33: /* Debug level. */
1.7       nicm       34: int             log_level = 0;
1.1       nicm       35:
1.6       nicm       36: void            log_event_cb(int, const char *);
1.7       nicm       37: void            log_vwrite(const char *, va_list);
1.2       nicm       38: __dead void     log_vfatal(const char *, va_list);
1.1       nicm       39:
1.5       nicm       40: /* Log callback for libevent. */
                     41: void
                     42: log_event_cb(unused int severity, const char *msg)
                     43: {
1.9     ! nicm       44:        log_debug("%s", msg);
1.5       nicm       45: }
                     46:
1.1       nicm       47: /* Open logging to file. */
                     48: void
1.7       nicm       49: log_open(int level, const char *path)
1.1       nicm       50: {
                     51:        log_file = fopen(path, "w");
                     52:        if (log_file == NULL)
                     53:                return;
                     54:        log_level = level;
                     55:
                     56:        setlinebuf(log_file);
1.5       nicm       57:        event_set_log_callback(log_event_cb);
1.1       nicm       58:
                     59:        tzset();
                     60: }
                     61:
                     62: /* Close logging. */
                     63: void
                     64: log_close(void)
                     65: {
1.8       nicm       66:        if (log_file != NULL)
1.1       nicm       67:                fclose(log_file);
1.5       nicm       68:
                     69:        event_set_log_callback(NULL);
1.1       nicm       70: }
                     71:
                     72: /* Write a log message. */
                     73: void
1.7       nicm       74: log_vwrite(const char *msg, va_list ap)
1.1       nicm       75: {
                     76:        char    *fmt;
1.8       nicm       77:
                     78:        if (log_file == NULL)
                     79:                return;
1.1       nicm       80:
1.7       nicm       81:        if (asprintf(&fmt, "%s\n", msg) == -1)
                     82:                exit(1);
                     83:        if (vfprintf(log_file, fmt, ap) == -1)
                     84:                exit(1);
                     85:        fflush(log_file);
                     86:        free(fmt);
1.1       nicm       87: }
                     88:
                     89: /* Log a debug message. */
                     90: void printflike1
                     91: log_debug(const char *msg, ...)
                     92: {
                     93:        va_list ap;
                     94:
                     95:        if (log_level > 0) {
                     96:                va_start(ap, msg);
1.7       nicm       97:                log_vwrite(msg, ap);
1.1       nicm       98:                va_end(ap);
                     99:        }
                    100: }
                    101:
                    102: /* Log a debug message at level 2. */
                    103: void printflike1
                    104: log_debug2(const char *msg, ...)
                    105: {
                    106:        va_list ap;
                    107:
                    108:        if (log_level > 1) {
                    109:                va_start(ap, msg);
1.7       nicm      110:                log_vwrite(msg, ap);
1.1       nicm      111:                va_end(ap);
                    112:        }
                    113: }
                    114:
                    115: /* Log a critical error, with error string if necessary, and die. */
                    116: __dead void
                    117: log_vfatal(const char *msg, va_list ap)
                    118: {
                    119:        char    *fmt;
                    120:
                    121:        if (errno != 0) {
                    122:                if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
                    123:                        exit(1);
1.7       nicm      124:                log_vwrite(fmt, ap);
1.1       nicm      125:        } else {
                    126:                if (asprintf(&fmt, "fatal: %s", msg) == -1)
1.4       nicm      127:                        exit(1);
1.7       nicm      128:                log_vwrite(fmt, ap);
1.1       nicm      129:        }
                    130:        free(fmt);
                    131:
                    132:        exit(1);
                    133: }
                    134:
                    135: /* Log a critical error, with error string, and die. */
                    136: __dead void printflike1
                    137: log_fatal(const char *msg, ...)
                    138: {
                    139:        va_list ap;
                    140:
                    141:        va_start(ap, msg);
                    142:        log_vfatal(msg, ap);
                    143: }
                    144:
                    145: /* Log a critical error and die. */
                    146: __dead void printflike1
                    147: log_fatalx(const char *msg, ...)
                    148: {
                    149:        va_list ap;
                    150:
                    151:        errno = 0;
                    152:        va_start(ap, msg);
                    153:        log_vfatal(msg, ap);
                    154: }