[pulseaudio-discuss] [PATCH] add a new log target that enables to create new log file if it exists

Deng Zhengrong dzrongg at gmail.com
Sun Jul 1 02:25:57 PDT 2012


---
 src/daemon/cmdline.c     |    4 ++--
 src/daemon/daemon-conf.c |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/src/daemon/cmdline.c b/src/daemon/cmdline.c
index 2c4c4b5..8fffda1 100644
--- a/src/daemon/cmdline.c
+++ b/src/daemon/cmdline.c
@@ -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;
diff --git a/src/daemon/daemon-conf.c b/src/daemon/daemon-conf.c
index dd2e7b6..74ae26d 100644
--- a/src/daemon/daemon-conf.c
+++ b/src/daemon/daemon-conf.c
@@ -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;
 
-- 
1.7.7.6



More information about the pulseaudio-discuss mailing list