1 /* $Id$ */
2 
3 /*
4  * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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 #include <time.h>
26 #include <unistd.h>
27 
28 #include "tmux.h"
29 #include "tmate.h"
30 
31 FILE *log_file;
32 
33 static void	log_event_cb(int, const char *);
34 static void	log_vwrite(const char *, va_list);
35 
36 static int log_level;
37 char *log_prefix;
38 
39 static void
log_event_cb(__unused int severity,const char * msg)40 log_event_cb(__unused int severity, const char *msg)
41 {
42 	log_debug("%s", msg);
43 }
44 
45 /* Increment log level. */
46 void
log_add_level(void)47 log_add_level(void)
48 {
49 	log_level++;
50 }
51 
52 /* Get log level. */
53 int
log_get_level(void)54 log_get_level(void)
55 {
56 	return log_level;
57 }
58 
init_logging(int _log_level)59 void init_logging(int _log_level)
60 {
61 	log_level = _log_level;
62 	log_prefix = xstrdup("");
63 
64 	log_file = fdopen(dup(STDERR_FILENO), "a");
65 	if (!log_file)
66 		exit(1);
67 
68 	event_set_log_callback(log_event_cb);
69 }
70 
set_log_prefix(char * _log_prefix)71 void set_log_prefix(char *_log_prefix)
72 {
73 	free(log_prefix);
74 	log_prefix = xstrdup(_log_prefix);
75 }
76 
77 /* Write a log message. */
78 __attribute__((__format__(__printf__, 1, 0)))
79 static void
log_vwrite(const char * msg,va_list ap)80 log_vwrite(const char *msg, va_list ap)
81 {
82 	char		*fmt, *out;
83 	struct timeval	 tv;
84 
85 	if (log_file == NULL)
86 		return;
87 
88 	if (vasprintf(&fmt, msg, ap) == -1)
89 		exit(1);
90 	if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1)
91 		exit(1);
92 
93 	if (fprintf(log_file, "%s%s\n", log_prefix, out) == -1)
94 		exit(1);
95 
96 	fflush(log_file);
97 
98 	free(out);
99 	free(fmt);
100 }
101 
102 void
log_emit(int level,const char * msg,...)103 log_emit(int level, const char *msg, ...)
104 {
105 	va_list	ap;
106 
107 	if (log_level < level)
108 		return;
109 
110 	va_start(ap, msg);
111 	log_vwrite(msg, ap);
112 	va_end(ap);
113 }
114 
115 /* Log a critical error with error string and die. */
116 __attribute__((__format__(__printf__, 1, 0)))
117 __dead void
fatal(const char * msg,...)118 fatal(const char *msg, ...)
119 {
120 	char	*fmt;
121 	va_list	 ap;
122 
123 	va_start(ap, msg);
124 	if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
125 		exit(1);
126 	msg = fmt;
127 	log_vwrite(msg, ap);
128 	exit(1);
129 }
130 
131 /* Log a critical error and die. */
132 __attribute__((__format__(__printf__, 1, 0)))
133 __dead void
fatalx(const char * msg,...)134 fatalx(const char *msg, ...)
135 {
136 	char	*fmt;
137 	va_list	 ap;
138 
139 	va_start(ap, msg);
140 	if (asprintf(&fmt, "fatal: %s", msg) == -1)
141 		exit(1);
142 	msg = fmt;
143 	log_vwrite(msg, ap);
144 	exit(1);
145 }
146