cli, log: Improve the set-log-target functionality

Add a new log target 'newfile:PATH', and refactoring 'pa_log_target_type'.

Signed-off-by: Shuai Fan <shuai900217@126.com>
This commit is contained in:
Shuai Fan 2013-06-19 11:59:08 +08:00 committed by Tanu Kaskinen
parent e05c12f28b
commit b0bf132f8f
7 changed files with 205 additions and 102 deletions

View file

@ -188,7 +188,7 @@ static const struct command commands[] = {
{ "kill-client", pa_cli_command_kill_client, "Kill a client (args: index)", 2},
{ "kill-sink-input", pa_cli_command_kill_sink_input, "Kill a sink input (args: index)", 2},
{ "kill-source-output", pa_cli_command_kill_source_output, "Kill a source output (args: index)", 2},
{ "set-log-target", pa_cli_command_log_target, "Change the log target (args: null,auto,syslog,stderr,file:PATH)", 2},
{ "set-log-target", pa_cli_command_log_target, "Change the log target (args: null|auto|syslog|stderr|file:PATH|newfile:PATH)", 2},
{ "set-log-level", pa_cli_command_log_level, "Change the log level (args: numeric level)", 2},
{ "set-log-meta", pa_cli_command_log_meta, "Show source code location in log messages (args: bool)", 2},
{ "set-log-time", pa_cli_command_log_time, "Show timestamps in log messages (args: bool)", 2},
@ -1501,6 +1501,7 @@ static int pa_cli_command_suspend(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, p
static int pa_cli_command_log_target(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, pa_bool_t *fail) {
const char *m;
pa_log_target *log_target = NULL;
pa_core_assert_ref(c);
pa_assert(t);
@ -1508,34 +1509,30 @@ static int pa_cli_command_log_target(pa_core *c, pa_tokenizer *t, pa_strbuf *buf
pa_assert(fail);
if (!(m = pa_tokenizer_get(t, 1))) {
pa_strbuf_puts(buf, "You need to specify a log target (null,auto,syslog,stderr,file:PATH).\n");
pa_strbuf_puts(buf, "You need to specify a log target (null|auto|syslog|stderr|file:PATH|newfile:PATH).\n");
return -1;
}
if (pa_streq(m, "null"))
pa_log_set_target(PA_LOG_NULL);
else if (pa_streq(m, "syslog"))
pa_log_set_target(PA_LOG_SYSLOG);
else if (pa_streq(m, "stderr") || pa_streq(m, "auto")) {
/* 'auto' is actually the effect with 'stderr' */
pa_log_set_target(PA_LOG_STDERR);
} else if (pa_startswith(m, "file:")) {
const char *file_path = m + 5;
int log_fd;
/* 'auto' is actually the effect with 'stderr' */
if (pa_streq(m, "auto"))
log_target = pa_log_target_new(PA_LOG_STDERR, NULL);
else {
log_target = pa_log_parse_target(m);
/* Open target file with user rights */
if ((log_fd = open(file_path, O_RDWR|O_TRUNC|O_CREAT, S_IRUSR | S_IWUSR)) >= 0) {
pa_log_set_target(PA_LOG_FD);
pa_log_set_fd(log_fd);
} else {
pa_strbuf_printf(buf, "Failed to open target file %s, error : %s\n", file_path, pa_cstrerror(errno));
if (!log_target) {
pa_strbuf_puts(buf, "Invalid log target.\n");
return -1;
}
} else {
pa_strbuf_puts(buf, "You need to specify a log target (null,auto,syslog,stderr,file:PATH).\n");
}
if (pa_log_set_target(log_target) < 0) {
pa_strbuf_puts(buf, "Failed to set log target.\n");
pa_log_target_free(log_target);
return -1;
}
pa_log_target_free(log_target);
return 0;
}

View file

@ -29,6 +29,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifdef HAVE_EXECINFO_H
#include <execinfo.h>
@ -47,9 +48,11 @@
#include <pulsecore/macro.h>
#include <pulsecore/core-util.h>
#include <pulsecore/core-error.h>
#include <pulsecore/once.h>
#include <pulsecore/ratelimit.h>
#include <pulsecore/thread.h>
#include <pulsecore/i18n.h>
#include "log.h"
@ -63,9 +66,11 @@
#define ENV_LOG_BACKTRACE "PULSE_LOG_BACKTRACE"
#define ENV_LOG_BACKTRACE_SKIP "PULSE_LOG_BACKTRACE_SKIP"
#define ENV_LOG_NO_RATELIMIT "PULSE_LOG_NO_RATE_LIMIT"
#define LOG_MAX_SUFFIX_NUMBER 99
static char *ident = NULL; /* in local charset format */
static pa_log_target_t target = PA_LOG_STDERR, target_override;
static pa_log_target target = { PA_LOG_STDERR, NULL };
static pa_log_target_type_t target_override;
static pa_bool_t target_override_set = FALSE;
static pa_log_level_t maximum_level = PA_LOG_ERROR, maximum_level_override = PA_LOG_ERROR;
static unsigned show_backtrace = 0, show_backtrace_override = 0, skip_backtrace = 0;
@ -113,10 +118,65 @@ void pa_log_set_level(pa_log_level_t l) {
maximum_level = l;
}
void pa_log_set_target(pa_log_target_t t) {
pa_assert(t < PA_LOG_TARGET_MAX);
int pa_log_set_target(pa_log_target *t) {
int fd = -1;
int old_fd;
target = t;
pa_assert(t);
switch (t->type) {
case PA_LOG_STDERR:
case PA_LOG_SYSLOG:
case PA_LOG_NULL:
break;
case PA_LOG_FILE:
if ((fd = pa_open_cloexec(t->file, O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
pa_log(_("Failed to open target file '%s'."), t->file);
return -1;
}
break;
case PA_LOG_NEWFILE: {
char *file_path;
char *p;
unsigned version;
file_path = pa_sprintf_malloc("%s.xx", t->file);
p = file_path + strlen(t->file);
for (version = 0; version <= LOG_MAX_SUFFIX_NUMBER; version++) {
memset(p, 0, 3); /* Overwrite the ".xx" part in file_path with zero bytes. */
if (version > 0)
pa_snprintf(p, 4, ".%u", version); /* Why 4? ".xx" + termitating zero byte. */
if ((fd = pa_open_cloexec(file_path, O_WRONLY | O_TRUNC | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) >= 0)
break;
}
if (version > LOG_MAX_SUFFIX_NUMBER) {
pa_log(_("Tried to open target file '%s', '%s.1', '%s.2' ... '%s.%d', but all failed."),
file_path, file_path, file_path, file_path, LOG_MAX_SUFFIX_NUMBER);
pa_xfree(file_path);
return -1;
} else
pa_log_debug("Opened target file %s\n", file_path);
pa_xfree(file_path);
break;
}
}
target.type = t->type;
pa_xfree(target.file);
target.file = pa_xstrdup(t->file);
old_fd = log_fd;
log_fd = fd;
if (old_fd >= 0)
pa_close(old_fd);
return 0;
}
void pa_log_set_flags(pa_log_flags_t _flags, pa_log_merge_t merge) {
@ -276,7 +336,7 @@ void pa_log_levelv_meta(
char *t, *n;
int saved_errno = errno;
char *bt = NULL;
pa_log_target_t _target;
pa_log_target_type_t _target;
pa_log_level_t _maximum_level;
unsigned _show_backtrace;
pa_log_flags_t _flags;
@ -290,7 +350,7 @@ void pa_log_levelv_meta(
init_defaults();
_target = target_override_set ? target_override : target;
_target = target_override_set ? target_override : target.type;
_maximum_level = PA_MAX(maximum_level, maximum_level_override);
_show_backtrace = PA_MAX(show_backtrace, show_backtrace_override);
_flags = flags | flags_override;
@ -410,18 +470,20 @@ void pa_log_levelv_meta(
}
#endif
case PA_LOG_FD: {
case PA_LOG_FILE:
case PA_LOG_NEWFILE: {
if (log_fd >= 0) {
char metadata[256];
pa_snprintf(metadata, sizeof(metadata), "\n%c %s %s", level_to_char[level], timestamp, location);
if ((write(log_fd, metadata, strlen(metadata)) < 0) || (write(log_fd, t, strlen(t)) < 0)) {
pa_log_target new_target = { .type = PA_LOG_STDERR, .file = NULL };
saved_errno = errno;
pa_log_set_fd(-1);
fprintf(stderr, "%s\n", "Error writing logs to a file descriptor. Redirect log messages to console.");
fprintf(stderr, "%s %s\n", metadata, t);
pa_log_set_target(PA_LOG_STDERR);
pa_log_set_target(&new_target);
}
}
@ -473,3 +535,68 @@ pa_bool_t pa_log_ratelimit(pa_log_level_t level) {
return pa_ratelimit_test(&ratelimit, level);
}
pa_log_target *pa_log_target_new(pa_log_target_type_t type, const char *file) {
pa_log_target *t = NULL;
t = pa_xnew(pa_log_target, 1);
t->type = type;
t->file = pa_xstrdup(file);
return t;
}
void pa_log_target_free(pa_log_target *t) {
pa_assert(t);
pa_xfree(t->file);
pa_xfree(t);
}
pa_log_target *pa_log_parse_target(const char *string) {
pa_log_target *t = NULL;
pa_assert(string);
if (pa_streq(string, "stderr"))
t = pa_log_target_new(PA_LOG_STDERR, NULL);
else if (pa_streq(string, "syslog"))
t = pa_log_target_new(PA_LOG_SYSLOG, NULL);
else if (pa_streq(string, "null"))
t = pa_log_target_new(PA_LOG_NULL, NULL);
else if (pa_startswith(string, "file:"))
t = pa_log_target_new(PA_LOG_FILE, string + 5);
else if (pa_startswith(string, "newfile:"))
t = pa_log_target_new(PA_LOG_NEWFILE, string + 8);
else
pa_log(_("Invalid log target."));
return t;
}
char *pa_log_target_to_string(const pa_log_target *t) {
char *string = NULL;
pa_assert(t);
switch (t->type) {
case PA_LOG_STDERR:
string = pa_xstrdup("stderr");
break;
case PA_LOG_SYSLOG:
string = pa_xstrdup("syslog");
break;
case PA_LOG_NULL:
string = pa_xstrdup("null");
break;
case PA_LOG_FILE:
string = pa_sprintf_malloc("file:%s", t->file);
break;
case PA_LOG_NEWFILE:
string = pa_sprintf_malloc("newfile:%s", t->file);
break;
}
return string;
}

View file

@ -32,13 +32,13 @@
/* A simple logging subsystem */
/* Where to log to */
typedef enum pa_log_target {
PA_LOG_STDERR, /* default */
typedef enum pa_log_target_type {
PA_LOG_STDERR, /* default */
PA_LOG_SYSLOG,
PA_LOG_NULL, /* to /dev/null */
PA_LOG_FD, /* to a file descriptor, e.g. a char device */
PA_LOG_TARGET_MAX
} pa_log_target_t;
PA_LOG_NULL, /* to /dev/null */
PA_LOG_FILE, /* to a user specified file */
PA_LOG_NEWFILE, /* with an automatic suffix to avoid overwriting anything */
} pa_log_target_type_t;
typedef enum pa_log_level {
PA_LOG_ERROR = 0, /* Error messages */
@ -63,11 +63,16 @@ typedef enum pa_log_merge {
PA_LOG_RESET
} pa_log_merge_t;
typedef struct {
pa_log_target_type_t type;
char *file;
} pa_log_target;
/* Set an identification for the current daemon. Used when logging to syslog. */
void pa_log_set_ident(const char *p);
/* Set a log target. */
void pa_log_set_target(pa_log_target_t t);
int pa_log_set_target(pa_log_target *t);
/* Maximal log level */
void pa_log_set_level(pa_log_level_t l);
@ -109,6 +114,14 @@ void pa_log_levelv(
const char *format,
va_list ap);
pa_log_target *pa_log_target_new(pa_log_target_type_t type, const char *file);
void pa_log_target_free(pa_log_target *t);
pa_log_target *pa_log_parse_target(const char *string);
char *pa_log_target_to_string(const pa_log_target *t);
#if __STDC_VERSION__ >= 199901L
/* ISO varargs available */