1 /* $OpenBSD$ */ 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 <unistd.h> 26 27 #include "tmux.h" 28 29 static FILE *log_file; 30 static int log_level; 31 32 static void log_event_cb(int, const char *); 33 static void log_vwrite(const char *, va_list) printflike(1, 0); 34 35 /* Log callback for libevent. */ 36 static void 37 log_event_cb(__unused int severity, const char *msg) 38 { 39 log_debug("%s", msg); 40 } 41 42 /* Increment log level. */ 43 void 44 log_add_level(void) 45 { 46 log_level++; 47 } 48 49 /* Get log level. */ 50 int 51 log_get_level(void) 52 { 53 return (log_level); 54 } 55 56 /* Open logging to file. */ 57 void 58 log_open(const char *name) 59 { 60 char *path; 61 62 if (log_level == 0) 63 return; 64 65 if (log_file != NULL) 66 fclose(log_file); 67 68 xasprintf(&path, "tmux-%s-%ld.log", name, (long)getpid()); 69 log_file = fopen(path, "w"); 70 free(path); 71 if (log_file == NULL) 72 return; 73 74 setvbuf(log_file, NULL, _IOLBF, 0); 75 event_set_log_callback(log_event_cb); 76 } 77 78 /* Close logging. */ 79 void 80 log_close(void) 81 { 82 if (log_file != NULL) 83 fclose(log_file); 84 log_file = NULL; 85 86 event_set_log_callback(NULL); 87 } 88 89 /* Write a log message. */ 90 static void 91 log_vwrite(const char *msg, va_list ap) 92 { 93 char *fmt, *out; 94 struct timeval tv; 95 96 if (log_file == NULL) 97 return; 98 99 if (vasprintf(&fmt, msg, ap) == -1) 100 exit(1); 101 #ifdef notyet 102 if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1) 103 exit(1); 104 #else 105 size_t len = strlen(fmt) * 4 + 1; 106 out = xmalloc(len); 107 if (strnvis(out, len, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1) 108 exit(1); 109 #endif 110 111 gettimeofday(&tv, NULL); 112 if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec, 113 (int)tv.tv_usec, out) == -1) 114 exit(1); 115 fflush(log_file); 116 free(out); 117 free(fmt); 118 } 119 120 /* Log a debug message. */ 121 void 122 log_debug(const char *msg, ...) 123 { 124 va_list ap; 125 126 va_start(ap, msg); 127 log_vwrite(msg, ap); 128 va_end(ap); 129 } 130 131 #if __GNUC_PREREQ__(4, 6) || defined(__clang__) 132 #pragma GCC diagnostic push 133 #pragma GCC diagnostic ignored "-Wformat-nonliteral" 134 #endif 135 136 /* Log a critical error with error string and die. */ 137 __dead void 138 fatal(const char *msg, ...) 139 { 140 char *fmt; 141 va_list ap; 142 143 va_start(ap, msg); 144 if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1) 145 exit(1); 146 log_vwrite(fmt, ap); 147 exit(1); 148 } 149 150 /* Log a critical error and die. */ 151 __dead void 152 fatalx(const char *msg, ...) 153 { 154 char *fmt; 155 va_list ap; 156 157 va_start(ap, msg); 158 if (asprintf(&fmt, "fatal: %s", msg) == -1) 159 exit(1); 160 log_vwrite(fmt, ap); 161 exit(1); 162 } 163 164 #if __GNUC_PREREQ__(4, 6) || defined(__clang__) 165 #pragma GCC diagnostic pop 166 #endif 167