4 * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
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.
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.
19 #include <sys/types.h>
29 static FILE *log_file
;
32 /* Log callback for libevent. */
34 log_event_cb(__unused
int severity
, const char *msg
)
39 /* Increment log level. */
53 /* Open logging to file. */
55 log_open(const char *name
)
63 xasprintf(&path
, "tmux-%s-%ld.log", name
, (long)getpid());
64 log_file
= fopen(path
, "a");
69 setvbuf(log_file
, NULL
, _IOLBF
, 0);
70 event_set_log_callback(log_event_cb
);
75 log_toggle(const char *name
)
80 log_debug("log opened");
82 log_debug("log closed");
96 event_set_log_callback(NULL
);
99 /* Write a log message. */
100 static void printflike(1, 0)
101 log_vwrite(const char *msg
, va_list ap
, const char *prefix
)
106 if (log_file
== NULL
)
109 if (vasprintf(&s
, msg
, ap
) == -1)
111 if (stravis(&out
, s
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
) == -1) {
117 gettimeofday(&tv
, NULL
);
118 if (fprintf(log_file
, "%lld.%06d %s%s\n", (long long)tv
.tv_sec
,
119 (int)tv
.tv_usec
, prefix
, out
) != -1)
124 /* Log a debug message. */
126 log_debug(const char *msg
, ...)
130 if (log_file
== NULL
)
134 log_vwrite(msg
, ap
, "");
138 /* Log a critical error with error string and die. */
140 fatal(const char *msg
, ...)
145 if (snprintf(tmp
, sizeof tmp
, "fatal: %s: ", strerror(errno
)) < 0)
149 log_vwrite(msg
, ap
, tmp
);
155 /* Log a critical error and die. */
157 fatalx(const char *msg
, ...)
162 log_vwrite(msg
, ap
, "fatal: ");