Sync OpenBSD patchset 1123:

Simplify logging and just fprintf(stderr, ...) for early errors.
pull/1/head
Tiago Cunha 2012-05-30 13:42:57 +00:00
parent c6fc8771a9
commit 169d362945
4 changed files with 27 additions and 65 deletions

View File

@ -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);
}
@ -254,7 +254,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);
}
@ -540,7 +540,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:

73
log.c
View File

@ -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);

6
tmux.c
View File

@ -76,7 +76,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);
}
}
@ -300,8 +300,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
@ -385,7 +383,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);
}
}

3
tmux.h
View File

@ -2178,8 +2178,7 @@ char *osdep_get_cwd(pid_t);
struct event_base *osdep_event_init(void);
/* 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 *, ...);