summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2012-05-25 08:28:10 +0000
committerNicholas Marriott <nicm@openbsd.org>2012-05-25 08:28:10 +0000
commit196710e2d3602832595626aa8ec5973247aaff17 (patch)
tree7ba3069864e0396dac28ec8acecc424a99c06679
parent1f23f6d68608716f93e9262cbb2abc335ef88401 (diff)
Simplify logging and just fprintf(stderr, ...) for early errors.
-rw-r--r--client.c10
-rw-r--r--log.c75
-rw-r--r--tmux.c6
-rw-r--r--tmux.h3
4 files changed, 28 insertions, 66 deletions
diff --git a/client.c b/client.c
index 4ebcf2ad..1a318d3f 100644
--- a/client.c
+++ b/client.c
@@ -209,15 +209,15 @@ client_main(int argc, char **argv, int flags)
if (shell_cmd == NULL && environ_path != NULL &&
(cmdflags & CMD_CANTNEST) &&
strcmp(socket_path, environ_path) == 0) {
- log_warnx("sessions should be nested with care. "
- "unset $TMUX to force.");
+ fprintf(stderr, "sessions should be nested with care, "
+ "unset $TMUX to force\n");
return (1);
}
/* Initialise the client socket and start the server. */
fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
if (fd == -1) {
- log_warn("failed to connect to server");
+ fprintf(stderr, "failed to connect to server\n");
return (1);
}
@@ -252,7 +252,7 @@ client_main(int argc, char **argv, int flags)
cmddata.argc = argc;
if (cmd_pack_argv(
argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
- log_warnx("command too long");
+ fprintf(stderr, "command too long\n");
return (1);
}
@@ -538,7 +538,7 @@ client_dispatch_attached(void)
return (0);
datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
- log_debug("client got %d", imsg.hdr.type);
+ log_debug("got %d from server", imsg.hdr.type);
switch (imsg.hdr.type) {
case MSG_DETACHKILL:
case MSG_DETACH:
diff --git a/log.c b/log.c
index 184f9597..dbae1972 100644
--- a/log.c
+++ b/log.c
@@ -27,20 +27,14 @@
#include "tmux.h"
-/* Logging type. */
-#define LOG_TYPE_OFF 0
-#define LOG_TYPE_TTY 1
-#define LOG_TYPE_FILE 2
-int log_type = LOG_TYPE_OFF;
-
/* Log file, if needed. */
-FILE *log_file;
+FILE *log_file = stderr;
/* Debug level. */
-int log_level;
+int log_level = 0;
void log_event_cb(int, const char *);
-void log_vwrite(int, const char *, va_list);
+void log_vwrite(const char *, va_list);
__dead void log_vfatal(const char *, va_list);
/* Log callback for libevent. */
@@ -50,29 +44,13 @@ log_event_cb(unused int severity, const char *msg)
log_warnx("%s", msg);
}
-/* Open logging to tty. */
-void
-log_open_tty(int level)
-{
- log_type = LOG_TYPE_TTY;
- log_level = level;
-
- setlinebuf(stderr);
- setlinebuf(stdout);
- event_set_log_callback(log_event_cb);
-
- tzset();
-}
-
/* Open logging to file. */
void
-log_open_file(int level, const char *path)
+log_open(int level, const char *path)
{
log_file = fopen(path, "w");
if (log_file == NULL)
return;
-
- log_type = LOG_TYPE_FILE;
log_level = level;
setlinebuf(log_file);
@@ -85,37 +63,24 @@ log_open_file(int level, const char *path)
void
log_close(void)
{
- if (log_type == LOG_TYPE_FILE)
+ if (log_file != stderr)
fclose(log_file);
event_set_log_callback(NULL);
-
- log_type = LOG_TYPE_OFF;
}
/* Write a log message. */
void
-log_vwrite(int pri, const char *msg, va_list ap)
+log_vwrite(const char *msg, va_list ap)
{
char *fmt;
- FILE *f = log_file;
-
- switch (log_type) {
- case LOG_TYPE_TTY:
- if (pri == LOG_INFO)
- f = stdout;
- else
- f = stderr;
- /* FALLTHROUGH */
- case LOG_TYPE_FILE:
- if (asprintf(&fmt, "%s\n", msg) == -1)
- exit(1);
- if (vfprintf(f, fmt, ap) == -1)
- exit(1);
- fflush(f);
- free(fmt);
- break;
- }
+
+ if (asprintf(&fmt, "%s\n", msg) == -1)
+ exit(1);
+ if (vfprintf(log_file, fmt, ap) == -1)
+ exit(1);
+ fflush(log_file);
+ free(fmt);
}
/* Log a warning with error string. */
@@ -128,7 +93,7 @@ log_warn(const char *msg, ...)
va_start(ap, msg);
if (asprintf(&fmt, "%s: %s", msg, strerror(errno)) == -1)
exit(1);
- log_vwrite(LOG_CRIT, fmt, ap);
+ log_vwrite(fmt, ap);
free(fmt);
va_end(ap);
}
@@ -140,7 +105,7 @@ log_warnx(const char *msg, ...)
va_list ap;
va_start(ap, msg);
- log_vwrite(LOG_CRIT, msg, ap);
+ log_vwrite(msg, ap);
va_end(ap);
}
@@ -152,7 +117,7 @@ log_info(const char *msg, ...)
if (log_level > -1) {
va_start(ap, msg);
- log_vwrite(LOG_INFO, msg, ap);
+ log_vwrite(msg, ap);
va_end(ap);
}
}
@@ -165,7 +130,7 @@ log_debug(const char *msg, ...)
if (log_level > 0) {
va_start(ap, msg);
- log_vwrite(LOG_DEBUG, msg, ap);
+ log_vwrite(msg, ap);
va_end(ap);
}
}
@@ -178,7 +143,7 @@ log_debug2(const char *msg, ...)
if (log_level > 1) {
va_start(ap, msg);
- log_vwrite(LOG_DEBUG, msg, ap);
+ log_vwrite(msg, ap);
va_end(ap);
}
}
@@ -192,11 +157,11 @@ log_vfatal(const char *msg, va_list ap)
if (errno != 0) {
if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
exit(1);
- log_vwrite(LOG_CRIT, fmt, ap);
+ log_vwrite(fmt, ap);
} else {
if (asprintf(&fmt, "fatal: %s", msg) == -1)
exit(1);
- log_vwrite(LOG_CRIT, fmt, ap);
+ log_vwrite(fmt, ap);
}
free(fmt);
diff --git a/tmux.c b/tmux.c
index 35d43909..468d37f7 100644
--- a/tmux.c
+++ b/tmux.c
@@ -73,7 +73,7 @@ logfile(const char *name)
log_close();
if (debug_level > 0) {
xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
- log_open_file(debug_level, path);
+ log_open(debug_level, path);
xfree(path);
}
}
@@ -294,8 +294,6 @@ main(int argc, char **argv)
if (shell_cmd != NULL && argc != 0)
usage();
- log_open_tty(debug_level);
-
if (!(flags & IDENTIFY_UTF8)) {
/*
* If the user has set whichever of LC_ALL, LC_CTYPE or LANG
@@ -379,7 +377,7 @@ main(int argc, char **argv)
/* -L or default set. */
if (label != NULL) {
if ((path = makesocketpath(label)) == NULL) {
- log_warn("can't create socket");
+ fprintf(stderr, "can't create socket\n");
exit(1);
}
}
diff --git a/tmux.h b/tmux.h
index 011c28c7..8da42144 100644
--- a/tmux.h
+++ b/tmux.h
@@ -2181,8 +2181,7 @@ char *get_proc_name(int, char *);
char *get_proc_cwd(pid_t);
/* log.c */
-void log_open_tty(int);
-void log_open_file(int, const char *);
+void log_open(int, const char *);
void log_close(void);
void printflike1 log_warn(const char *, ...);
void printflike1 log_warnx(const char *, ...);