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

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