Simplify logging and just fprintf(stderr, ...) for early errors.

pull/1/head
Nicholas Marriott 2012-05-25 08:28:10 +00:00
parent 1f23f6d686
commit 196710e2d3
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 && if (shell_cmd == NULL && environ_path != NULL &&
(cmdflags & CMD_CANTNEST) && (cmdflags & CMD_CANTNEST) &&
strcmp(socket_path, environ_path) == 0) { strcmp(socket_path, environ_path) == 0) {
log_warnx("sessions should be nested with care. " fprintf(stderr, "sessions should be nested with care, "
"unset $TMUX to force."); "unset $TMUX to force\n");
return (1); return (1);
} }
/* Initialise the client socket and start the server. */ /* Initialise the client socket and start the server. */
fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER); fd = client_connect(socket_path, cmdflags & CMD_STARTSERVER);
if (fd == -1) { if (fd == -1) {
log_warn("failed to connect to server"); fprintf(stderr, "failed to connect to server\n");
return (1); return (1);
} }
@ -252,7 +252,7 @@ client_main(int argc, char **argv, int flags)
cmddata.argc = argc; cmddata.argc = argc;
if (cmd_pack_argv( if (cmd_pack_argv(
argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) { argc, argv, cmddata.argv, sizeof cmddata.argv) != 0) {
log_warnx("command too long"); fprintf(stderr, "command too long\n");
return (1); return (1);
} }
@ -538,7 +538,7 @@ client_dispatch_attached(void)
return (0); return (0);
datalen = imsg.hdr.len - IMSG_HEADER_SIZE; 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) { switch (imsg.hdr.type) {
case MSG_DETACHKILL: case MSG_DETACHKILL:
case MSG_DETACH: case MSG_DETACH:

73
log.c
View File

@ -27,20 +27,14 @@
#include "tmux.h" #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. */ /* Log file, if needed. */
FILE *log_file; FILE *log_file = stderr;
/* Debug level. */ /* Debug level. */
int log_level; int log_level = 0;
void log_event_cb(int, const char *); 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); __dead void log_vfatal(const char *, va_list);
/* Log callback for libevent. */ /* Log callback for libevent. */
@ -50,29 +44,13 @@ log_event_cb(unused int severity, const char *msg)
log_warnx("%s", 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. */ /* Open logging to file. */
void void
log_open_file(int level, const char *path) log_open(int level, const char *path)
{ {
log_file = fopen(path, "w"); log_file = fopen(path, "w");
if (log_file == NULL) if (log_file == NULL)
return; return;
log_type = LOG_TYPE_FILE;
log_level = level; log_level = level;
setlinebuf(log_file); setlinebuf(log_file);
@ -85,37 +63,24 @@ log_open_file(int level, const char *path)
void void
log_close(void) log_close(void)
{ {
if (log_type == LOG_TYPE_FILE) if (log_file != stderr)
fclose(log_file); fclose(log_file);
event_set_log_callback(NULL); event_set_log_callback(NULL);
log_type = LOG_TYPE_OFF;
} }
/* Write a log message. */ /* Write a log message. */
void void
log_vwrite(int pri, const char *msg, va_list ap) log_vwrite(const char *msg, va_list ap)
{ {
char *fmt; char *fmt;
FILE *f = log_file;
switch (log_type) { if (asprintf(&fmt, "%s\n", msg) == -1)
case LOG_TYPE_TTY: exit(1);
if (pri == LOG_INFO) if (vfprintf(log_file, fmt, ap) == -1)
f = stdout; exit(1);
else fflush(log_file);
f = stderr; free(fmt);
/* 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;
}
} }
/* Log a warning with error string. */ /* Log a warning with error string. */
@ -128,7 +93,7 @@ log_warn(const char *msg, ...)
va_start(ap, msg); va_start(ap, msg);
if (asprintf(&fmt, "%s: %s", msg, strerror(errno)) == -1) if (asprintf(&fmt, "%s: %s", msg, strerror(errno)) == -1)
exit(1); exit(1);
log_vwrite(LOG_CRIT, fmt, ap); log_vwrite(fmt, ap);
free(fmt); free(fmt);
va_end(ap); va_end(ap);
} }
@ -140,7 +105,7 @@ log_warnx(const char *msg, ...)
va_list ap; va_list ap;
va_start(ap, msg); va_start(ap, msg);
log_vwrite(LOG_CRIT, msg, ap); log_vwrite(msg, ap);
va_end(ap); va_end(ap);
} }
@ -152,7 +117,7 @@ log_info(const char *msg, ...)
if (log_level > -1) { if (log_level > -1) {
va_start(ap, msg); va_start(ap, msg);
log_vwrite(LOG_INFO, msg, ap); log_vwrite(msg, ap);
va_end(ap); va_end(ap);
} }
} }
@ -165,7 +130,7 @@ log_debug(const char *msg, ...)
if (log_level > 0) { if (log_level > 0) {
va_start(ap, msg); va_start(ap, msg);
log_vwrite(LOG_DEBUG, msg, ap); log_vwrite(msg, ap);
va_end(ap); va_end(ap);
} }
} }
@ -178,7 +143,7 @@ log_debug2(const char *msg, ...)
if (log_level > 1) { if (log_level > 1) {
va_start(ap, msg); va_start(ap, msg);
log_vwrite(LOG_DEBUG, msg, ap); log_vwrite(msg, ap);
va_end(ap); va_end(ap);
} }
} }
@ -192,11 +157,11 @@ log_vfatal(const char *msg, va_list ap)
if (errno != 0) { if (errno != 0) {
if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1) if (asprintf(&fmt, "fatal: %s: %s", msg, strerror(errno)) == -1)
exit(1); exit(1);
log_vwrite(LOG_CRIT, fmt, ap); log_vwrite(fmt, ap);
} else { } else {
if (asprintf(&fmt, "fatal: %s", msg) == -1) if (asprintf(&fmt, "fatal: %s", msg) == -1)
exit(1); exit(1);
log_vwrite(LOG_CRIT, fmt, ap); log_vwrite(fmt, ap);
} }
free(fmt); free(fmt);

6
tmux.c
View File

@ -73,7 +73,7 @@ logfile(const char *name)
log_close(); log_close();
if (debug_level > 0) { if (debug_level > 0) {
xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid()); xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
log_open_file(debug_level, path); log_open(debug_level, path);
xfree(path); xfree(path);
} }
} }
@ -294,8 +294,6 @@ main(int argc, char **argv)
if (shell_cmd != NULL && argc != 0) if (shell_cmd != NULL && argc != 0)
usage(); usage();
log_open_tty(debug_level);
if (!(flags & IDENTIFY_UTF8)) { if (!(flags & IDENTIFY_UTF8)) {
/* /*
* If the user has set whichever of LC_ALL, LC_CTYPE or LANG * 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. */ /* -L or default set. */
if (label != NULL) { if (label != NULL) {
if ((path = makesocketpath(label)) == NULL) { if ((path = makesocketpath(label)) == NULL) {
log_warn("can't create socket"); fprintf(stderr, "can't create socket\n");
exit(1); exit(1);
} }
} }

3
tmux.h
View File

@ -2181,8 +2181,7 @@ char *get_proc_name(int, char *);
char *get_proc_cwd(pid_t); char *get_proc_cwd(pid_t);
/* log.c */ /* log.c */
void log_open_tty(int); void log_open(int, const char *);
void log_open_file(int, const char *);
void log_close(void); void log_close(void);
void printflike1 log_warn(const char *, ...); void printflike1 log_warn(const char *, ...);
void printflike1 log_warnx(const char *, ...); void printflike1 log_warnx(const char *, ...);