Annotation of src/usr.bin/tmux/log.c, Revision 1.28
1.28 ! nicm 1: /* $OpenBSD: log.c,v 1.27 2021/03/31 08:37:48 nicm Exp $ */
1.1 nicm 2:
3: /*
1.23 nicm 4: * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1 nicm 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.5 nicm 33: /* Log callback for libevent. */
1.20 nicm 34: static void
1.19 nicm 35: log_event_cb(__unused int severity, const char *msg)
1.5 nicm 36: {
1.9 nicm 37: log_debug("%s", msg);
1.5 nicm 38: }
39:
1.20 nicm 40: /* Increment log level. */
41: void
42: log_add_level(void)
43: {
44: log_level++;
45: }
46:
47: /* Get log level. */
48: int
49: log_get_level(void)
50: {
51: return (log_level);
52: }
53:
1.1 nicm 54: /* Open logging to file. */
55: void
1.20 nicm 56: log_open(const char *name)
1.1 nicm 57: {
1.20 nicm 58: char *path;
59:
60: if (log_level == 0)
61: return;
1.25 nicm 62: log_close();
1.17 nicm 63:
1.20 nicm 64: xasprintf(&path, "tmux-%s-%ld.log", name, (long)getpid());
1.25 nicm 65: log_file = fopen(path, "a");
1.20 nicm 66: free(path);
1.1 nicm 67: if (log_file == NULL)
68: return;
69:
1.12 millert 70: setvbuf(log_file, NULL, _IOLBF, 0);
1.5 nicm 71: event_set_log_callback(log_event_cb);
1.25 nicm 72: }
73:
74: /* Toggle logging. */
75: void
76: log_toggle(const char *name)
77: {
78: if (log_level == 0) {
79: log_level = 1;
80: log_open(name);
81: log_debug("log opened");
82: } else {
83: log_debug("log closed");
84: log_level = 0;
85: log_close();
86: }
1.1 nicm 87: }
88:
89: /* Close logging. */
90: void
91: log_close(void)
92: {
1.8 nicm 93: if (log_file != NULL)
1.1 nicm 94: fclose(log_file);
1.10 nicm 95: log_file = NULL;
1.5 nicm 96:
97: event_set_log_callback(NULL);
1.1 nicm 98: }
99:
100: /* Write a log message. */
1.28 ! nicm 101: static void printflike(1, 0)
1.7 nicm 102: log_vwrite(const char *msg, va_list ap)
1.1 nicm 103: {
1.15 nicm 104: char *fmt, *out;
1.14 nicm 105: struct timeval tv;
1.8 nicm 106:
107: if (log_file == NULL)
108: return;
1.1 nicm 109:
1.15 nicm 110: if (vasprintf(&fmt, msg, ap) == -1)
1.27 nicm 111: return;
112: if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1) {
113: free(fmt);
114: return;
115: }
1.15 nicm 116:
1.14 nicm 117: gettimeofday(&tv, NULL);
1.15 nicm 118: if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec,
1.27 nicm 119: (int)tv.tv_usec, out) != -1)
120: fflush(log_file);
1.15 nicm 121:
122: free(out);
1.7 nicm 123: free(fmt);
1.1 nicm 124: }
125:
126: /* Log a debug message. */
1.11 nicm 127: void
1.1 nicm 128: log_debug(const char *msg, ...)
129: {
130: va_list ap;
1.26 nicm 131:
132: if (log_file == NULL)
133: return;
1.1 nicm 134:
1.10 nicm 135: va_start(ap, msg);
136: log_vwrite(msg, ap);
137: va_end(ap);
1.1 nicm 138: }
139:
1.10 nicm 140: /* Log a critical error with error string and die. */
1.11 nicm 141: __dead void
1.18 nicm 142: fatal(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: %s", msg, strerror(errno)) == -1)
149: exit(1);
150: log_vwrite(fmt, ap);
1.24 nicm 151: va_end(ap);
1.10 nicm 152: exit(1);
1.1 nicm 153: }
154:
155: /* Log a critical error and die. */
1.11 nicm 156: __dead void
1.18 nicm 157: fatalx(const char *msg, ...)
1.1 nicm 158: {
1.10 nicm 159: char *fmt;
160: va_list ap;
1.1 nicm 161:
162: va_start(ap, msg);
1.10 nicm 163: if (asprintf(&fmt, "fatal: %s", msg) == -1)
164: exit(1);
165: log_vwrite(fmt, ap);
1.24 nicm 166: va_end(ap);
1.10 nicm 167: exit(1);
1.1 nicm 168: }