add a new log target that enables to create new log file if it exists

This commit is contained in:
Deng Zhengrong 2012-06-28 15:27:57 +08:00 committed by David Henningsson
parent 270b1a7d74
commit 508ca489d2
2 changed files with 35 additions and 2 deletions

View file

@ -141,7 +141,7 @@ void pa_cmdline_help(const char *argv0) {
" this time passed\n"
" --log-level[=LEVEL] Increase or set verbosity level\n"
" -v Increase the verbosity level\n"
" --log-target={auto,syslog,stderr,file:PATH}\n"
" --log-target={auto,syslog,stderr,file:PATH,newfile:PATH}\n"
" Specify the log target\n"
" --log-meta[=BOOL] Include code location in log messages\n"
" --log-time[=BOOL] Include timestamps in log messages\n"
@ -315,7 +315,7 @@ int pa_cmdline_parse(pa_daemon_conf *conf, int argc, char *const argv [], int *d
case ARG_LOG_TARGET:
if (pa_daemon_conf_set_log_target(conf, optarg) < 0) {
pa_log(_("Invalid log target: use either 'syslog', 'stderr' or 'auto' or a valid file name 'file:<path>'."));
pa_log(_("Invalid log target: use either 'syslog', 'stderr' or 'auto' or a valid file name 'file:<path>', 'newfile:<path>'."));
goto fail;
}
break;

View file

@ -176,6 +176,8 @@ void pa_daemon_conf_free(pa_daemon_conf *c) {
pa_xfree(c);
}
#define PA_LOG_MAX_SUFFIX_NUMBER 100
int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
pa_assert(c);
pa_assert(string);
@ -203,6 +205,37 @@ int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
printf("Failed to open target file %s, error : %s\n", file_path, pa_cstrerror(errno));
return -1;
}
} else if (pa_startswith(string, "newfile:")) {
char file_path[512];
int log_fd;
int version = 0;
int left_size;
char *p;
pa_strlcpy(file_path, string + 8, sizeof(file_path));
left_size = sizeof(file_path) - strlen(file_path);
p = file_path + strlen(file_path);
do {
memset(p, 0, left_size);
if (version > 0)
pa_snprintf(p, left_size, ".%d", version);
} while (++version <= PA_LOG_MAX_SUFFIX_NUMBER &&
(log_fd = open(file_path, O_RDWR|O_TRUNC|O_CREAT|O_EXCL, S_IRUSR | S_IWUSR)) < 0);
if (version > PA_LOG_MAX_SUFFIX_NUMBER) {
memset(p, 0, left_size);
printf("Tried to open target files '%s', '%s.1', '%s.2' ... '%s.%d', but all failed.\n",
file_path, file_path, file_path, file_path, PA_LOG_MAX_SUFFIX_NUMBER - 1);
return -1;
} else {
printf("Opened target file %s\n", file_path);
c->auto_log_target = 0;
c->log_target = PA_LOG_FD;
pa_log_set_fd(log_fd);
}
} else
return -1;