Annotation of embedaddon/tmux/log.c, revision 1.1.1.1
1.1 misho 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);
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: if (stravis(&out, fmt, VIS_OCTAL|VIS_CSTYLE|VIS_TAB|VIS_NL) == -1)
102: exit(1);
103:
104: gettimeofday(&tv, NULL);
105: if (fprintf(log_file, "%lld.%06d %s\n", (long long)tv.tv_sec,
106: (int)tv.tv_usec, out) == -1)
107: exit(1);
108: fflush(log_file);
109:
110: free(out);
111: free(fmt);
112: }
113:
114: /* Log a debug message. */
115: void
116: log_debug(const char *msg, ...)
117: {
118: va_list ap;
119:
120: va_start(ap, msg);
121: log_vwrite(msg, ap);
122: va_end(ap);
123: }
124:
125: /* Log a critical error with error string and die. */
126: __dead void
127: fatal(const char *msg, ...)
128: {
129: char *fmt;
130: va_list ap;
131:
132: va_start(ap, msg);
133: if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
134: exit(1);
135: log_vwrite(fmt, ap);
136: va_end(ap);
137: exit(1);
138: }
139:
140: /* Log a critical error and die. */
141: __dead void
142: fatalx(const char *msg, ...)
143: {
144: char *fmt;
145: va_list ap;
146:
147: va_start(ap, msg);
148: if (asprintf(&fmt, "fatal: %s", msg) == -1)
149: exit(1);
150: log_vwrite(fmt, ap);
151: va_end(ap);
152: exit(1);
153: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>