xref: /openbsd-src/usr.bin/tmux/log.c (revision 9b9d2a55a62c8e82206c25f94fcc7f4e2765250e)
1 /* $OpenBSD: log.c,v 1.14 2015/08/29 00:24:44 nicm Exp $ */
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);
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 	log_file = fopen(path, "w");
45 	if (log_file == NULL)
46 		return;
47 
48 	setvbuf(log_file, NULL, _IOLBF, 0);
49 	event_set_log_callback(log_event_cb);
50 
51 	tzset();
52 }
53 
54 /* Close logging. */
55 void
56 log_close(void)
57 {
58 	if (log_file != NULL)
59 		fclose(log_file);
60 	log_file = NULL;
61 
62 	event_set_log_callback(NULL);
63 }
64 
65 /* Write a log message. */
66 void
67 log_vwrite(const char *msg, va_list ap)
68 {
69 	char		*fmt;
70 	struct timeval	 tv;
71 
72 	if (log_file == NULL)
73 		return;
74 
75 	gettimeofday(&tv, NULL);
76 	if (asprintf(&fmt, "%lld.%06d %s\n", (long long)tv.tv_sec,
77 	    (int)tv.tv_usec, msg) == -1)
78 		exit(1);
79 	if (vfprintf(log_file, fmt, ap) == -1)
80 		exit(1);
81 	fflush(log_file);
82 	free(fmt);
83 }
84 
85 /* Log a debug message. */
86 void
87 log_debug(const char *msg, ...)
88 {
89 	va_list	ap;
90 
91 	va_start(ap, msg);
92 	log_vwrite(msg, ap);
93 	va_end(ap);
94 }
95 
96 /* Log a critical error with error string and die. */
97 __dead void
98 log_fatal(const char *msg, ...)
99 {
100 	char	*fmt;
101 	va_list	 ap;
102 
103 	va_start(ap, msg);
104 	if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
105 		exit(1);
106 	log_vwrite(fmt, ap);
107 	exit(1);
108 }
109 
110 /* Log a critical error and die. */
111 __dead void
112 log_fatalx(const char *msg, ...)
113 {
114 	char	*fmt;
115 	va_list	 ap;
116 
117 	va_start(ap, msg);
118 	if (asprintf(&fmt, "fatal: %s", msg) == -1)
119 		exit(1);
120 	log_vwrite(fmt, ap);
121 	exit(1);
122 }
123