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>
30 static FILE *log_file
;
33 /* Log callback for libevent. */
35 log_event_cb(__unused
int severity
, const char *msg
)
40 /* Increment log level. */
54 /* Open logging to file. */
56 log_open(const char *name
)
64 xasprintf(&path
, "tmux-%s-%ld.log", name
, (long)getpid());
65 log_file
= fopen(path
, "a");
70 setvbuf(log_file
, NULL
, _IOLBF
, 0);
71 event_set_log_callback(log_event_cb
);
76 log_toggle(const char *name
)
81 log_debug("log opened");
83 log_debug("log closed");
97 event_set_log_callback(NULL
);
100 /* Write a log message. */
101 static void printflike(1, 0)
102 log_vwrite(const char *msg
, va_list ap
, const char *prefix
)
107 if (log_file
== NULL
)
110 if (vasprintf(&s
, msg
, ap
) == -1)
112 if (stravis(&out
, s
, VIS_OCTAL
|VIS_CSTYLE
|VIS_TAB
|VIS_NL
) == -1) {
118 gettimeofday(&tv
, NULL
);
119 if (fprintf(log_file
, "%lld.%06d %s%s\n", (long long)tv
.tv_sec
,
120 (int)tv
.tv_usec
, prefix
, out
) != -1)
125 /* Log a debug message. */
127 log_debug(const char *msg
, ...)
131 if (log_file
== NULL
)
135 log_vwrite(msg
, ap
, "");
139 /* Log a critical error with error string and die. */
141 fatal(const char *msg
, ...)
146 if (snprintf(tmp
, sizeof tmp
, "fatal: %s: ", strerror(errno
)) < 0)
150 log_vwrite(msg
, ap
, tmp
);
156 /* Log a critical error and die. */
158 fatalx(const char *msg
, ...)
163 log_vwrite(msg
, ap
, "fatal: ");