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