Make the log stuff a bit tidier with some helper functions.

pull/218/head
nicm 2015-11-24 21:19:46 +00:00
parent 4ec61bef46
commit 9cccb8c115
6 changed files with 36 additions and 27 deletions

View File

@ -61,7 +61,6 @@ cmd_show_messages_server(struct cmd_q *cmdq)
cmdq_print(cmdq, "started %s", tim);
cmdq_print(cmdq, "socket path %s", socket_path);
cmdq_print(cmdq, "debug level %d", debug_level);
cmdq_print(cmdq, "protocol version %d", PROTOCOL_VERSION);
return (1);

35
log.c
View File

@ -22,30 +22,53 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <vis.h>
#include "tmux.h"
FILE *log_file;
static FILE *log_file;
static int log_level;
void log_event_cb(int, const char *);
void log_vwrite(const char *, va_list);
static void log_event_cb(int, const char *);
static void log_vwrite(const char *, va_list);
/* Log callback for libevent. */
void
static void
log_event_cb(__unused int severity, const char *msg)
{
log_debug("%s", msg);
}
/* Increment log level. */
void
log_add_level(void)
{
log_level++;
}
/* Get log level. */
int
log_get_level(void)
{
return (log_level);
}
/* Open logging to file. */
void
log_open(const char *path)
log_open(const char *name)
{
char *path;
if (log_level == 0)
return;
if (log_file != NULL)
fclose(log_file);
xasprintf(&path, "tmux-%s-%ld.log", name, (long)getpid());
log_file = fopen(path, "w");
free(path);
if (log_file == NULL)
return;
@ -65,7 +88,7 @@ log_close(void)
}
/* Write a log message. */
void
static void
log_vwrite(const char *msg, va_list ap)
{
char *fmt, *out;

2
proc.c
View File

@ -188,7 +188,7 @@ proc_start(const char *name, struct event_base *base, int forkflag,
fatalx("event_reinit failed");
}
logfile(name);
log_open(name);
setproctitle("%s (%s)", name, socket_path);
log_debug("%s started (%ld): socket %s, protocol %d", name,

View File

@ -173,7 +173,7 @@ server_start(struct event_base *base, int lockfd, char *lockfile)
}
close(pair[0]);
if (debug_level > 3)
if (log_get_level() > 3)
tty_create_log();
if (pledge("stdio rpath wpath cpath fattr unix getpw recvfd proc exec "
"tty ps", NULL) != 0)

15
tmux.c
View File

@ -44,7 +44,6 @@ struct options *global_w_options; /* window options */
struct environ *global_environ;
char *shell_cmd;
int debug_level;
time_t start_time;
char socket_path[PATH_MAX];
@ -61,18 +60,6 @@ usage(void)
exit(1);
}
void
logfile(const char *name)
{
char *path;
if (debug_level > 0) {
xasprintf(&path, "tmux-%s-%ld.log", name, (long) getpid());
log_open(path);
free(path);
}
}
const char *
getshell(void)
{
@ -243,7 +230,7 @@ main(int argc, char **argv)
flags |= CLIENT_UTF8;
break;
case 'v':
debug_level++;
log_add_level();
break;
default:
usage();

8
tmux.h
View File

@ -1432,10 +1432,8 @@ extern struct options *global_s_options;
extern struct options *global_w_options;
extern struct environ *global_environ;
extern char *shell_cmd;
extern int debug_level;
extern time_t start_time;
extern char socket_path[PATH_MAX];
void logfile(const char *);
const char *getshell(void);
int checkshell(const char *);
int areshell(const char *);
@ -2210,8 +2208,10 @@ char *utf8_padcstr(const char *, u_int);
char *get_proc_name(int, char *);
/* log.c */
void log_open(const char *);
void log_close(void);
void log_add_level(void);
int log_get_level(void);
void log_open(const char *);
void log_close(void);
void printflike(1, 2) log_debug(const char *, ...);
__dead void printflike(1, 2) fatal(const char *, ...);
__dead void printflike(1, 2) fatalx(const char *, ...);