[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.17

1.17    ! nicm        1: /* $OpenBSD: log.c,v 1.16 2015/09/14 12:12:24 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>
1.15      nicm       25: #include <vis.h>
1.1       nicm       26:
                     27: #include "tmux.h"
                     28:
1.10      nicm       29: FILE   *log_file;
1.1       nicm       30:
1.10      nicm       31: void    log_event_cb(int, const char *);
                     32: void    log_vwrite(const char *, va_list);
1.1       nicm       33:
1.5       nicm       34: /* Log callback for libevent. */
                     35: void
                     36: log_event_cb(unused int severity, const char *msg)
                     37: {
1.9       nicm       38:        log_debug("%s", msg);
1.5       nicm       39: }
                     40:
1.1       nicm       41: /* Open logging to file. */
                     42: void
1.10      nicm       43: log_open(const char *path)
1.1       nicm       44: {
1.17    ! nicm       45:        if (log_file != NULL)
        !            46:                fclose(log_file);
        !            47:
1.1       nicm       48:        log_file = fopen(path, "w");
                     49:        if (log_file == NULL)
                     50:                return;
                     51:
1.12      millert    52:        setvbuf(log_file, NULL, _IOLBF, 0);
1.5       nicm       53:        event_set_log_callback(log_event_cb);
1.1       nicm       54: }
                     55:
                     56: /* Close logging. */
                     57: void
                     58: log_close(void)
                     59: {
1.8       nicm       60:        if (log_file != NULL)
1.1       nicm       61:                fclose(log_file);
1.10      nicm       62:        log_file = NULL;
1.5       nicm       63:
                     64:        event_set_log_callback(NULL);
1.1       nicm       65: }
                     66:
                     67: /* Write a log message. */
                     68: void
1.7       nicm       69: log_vwrite(const char *msg, va_list ap)
1.1       nicm       70: {
1.15      nicm       71:        char            *fmt, *out;
1.14      nicm       72:        struct timeval   tv;
1.8       nicm       73:
                     74:        if (log_file == NULL)
                     75:                return;
1.1       nicm       76:
1.15      nicm       77:        if (vasprintf(&fmt, msg, ap) == -1)
                     78:                exit(1);
                     79:        if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1)
                     80:                exit(1);
                     81:
1.14      nicm       82:        gettimeofday(&tv, NULL);
1.15      nicm       83:        if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec,
                     84:            (int)tv.tv_usec, out) == -1)
1.7       nicm       85:                exit(1);
                     86:        fflush(log_file);
1.15      nicm       87:
                     88:        free(out);
1.7       nicm       89:        free(fmt);
1.1       nicm       90: }
                     91:
                     92: /* Log a debug message. */
1.11      nicm       93: void
1.1       nicm       94: log_debug(const char *msg, ...)
                     95: {
                     96:        va_list ap;
                     97:
1.10      nicm       98:        va_start(ap, msg);
                     99:        log_vwrite(msg, ap);
                    100:        va_end(ap);
1.1       nicm      101: }
                    102:
1.10      nicm      103: /* Log a critical error with error string and die. */
1.11      nicm      104: __dead void
1.1       nicm      105: log_fatal(const char *msg, ...)
                    106: {
1.10      nicm      107:        char    *fmt;
                    108:        va_list  ap;
1.1       nicm      109:
                    110:        va_start(ap, msg);
1.10      nicm      111:        if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
                    112:                exit(1);
                    113:        log_vwrite(fmt, ap);
                    114:        exit(1);
1.1       nicm      115: }
                    116:
                    117: /* Log a critical error and die. */
1.11      nicm      118: __dead void
1.1       nicm      119: log_fatalx(const char *msg, ...)
                    120: {
1.10      nicm      121:        char    *fmt;
                    122:        va_list  ap;
1.1       nicm      123:
                    124:        va_start(ap, msg);
1.10      nicm      125:        if (asprintf(&fmt, "fatal: %s", msg) == -1)
                    126:                exit(1);
                    127:        log_vwrite(fmt, ap);
                    128:        exit(1);
1.1       nicm      129: }