xref: /netbsd-src/external/bsd/tmux/dist/log.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* $OpenBSD$ */
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 
28 FILE	*log_file;
29 
30 void	 log_event_cb(int, const char *);
31 void	 log_vwrite(const char *, va_list) __printflike(1, 0);
32 
33 /* Log callback for libevent. */
34 void
35 log_event_cb(unused int severity, const char *msg)
36 {
37 	log_debug("%s", msg);
38 }
39 
40 /* Open logging to file. */
41 void
42 log_open(const char *path)
43 {
44 	if (log_file != NULL)
45 		fclose(log_file);
46 
47 	log_file = fopen(path, "w");
48 	if (log_file == NULL)
49 		return;
50 
51 	setvbuf(log_file, NULL, _IOLBF, 0);
52 	event_set_log_callback(log_event_cb);
53 }
54 
55 /* Close logging. */
56 void
57 log_close(void)
58 {
59 	if (log_file != NULL)
60 		fclose(log_file);
61 	log_file = NULL;
62 
63 	event_set_log_callback(NULL);
64 }
65 
66 /* Write a log message. */
67 void
68 log_vwrite(const char *msg, va_list ap)
69 {
70 	char		*fmt, *out;
71 	struct timeval	 tv;
72 
73 	if (log_file == NULL)
74 		return;
75 
76 	if (vasprintf(&fmt, msg, ap) == -1)
77 		exit(1);
78 #ifdef notyet
79 	if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1)
80 		exit(1);
81 #else
82 	size_t len = strlen(fmt) * 4 + 1;
83 	out = xmalloc(len);
84 	if (strnvis(out, len, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1)
85 		exit(1);
86 #endif
87 
88 	gettimeofday(&tv, NULL);
89 	if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec,
90 	    (int)tv.tv_usec, out) == -1)
91 		exit(1);
92 	fflush(log_file);
93 	free(out);
94 	free(fmt);
95 }
96 #if __GNUC_PREREQ__(4, 6) || defined(__clang__)
97 #pragma GCC diagnostic push
98 #endif
99 
100 /* Log a debug message. */
101 void
102 log_debug(const char *msg, ...)
103 {
104 	va_list	ap;
105 
106 	va_start(ap, msg);
107 	log_vwrite(msg, ap);
108 	va_end(ap);
109 }
110 
111 /* Log a critical error with error string and die. */
112 __dead void
113 log_fatal(const char *msg, ...)
114 {
115 	char	*fmt;
116 	va_list	 ap;
117 
118 	va_start(ap, msg);
119 	if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
120 		exit(1);
121 	log_vwrite(fmt, ap);
122 	exit(1);
123 }
124 
125 /* Log a critical error and die. */
126 __dead void
127 log_fatalx(const char *msg, ...)
128 {
129 	char	*fmt;
130 	va_list	 ap;
131 
132 	va_start(ap, msg);
133 	if (asprintf(&fmt, "fatal: %s", msg) == -1)
134 		exit(1);
135 	log_vwrite(fmt, ap);
136 	exit(1);
137 }
138