Skip to contentSkip to conversation

util/log: do not close and reopen log files when flags are turned off

PATCH In review Paolo Bonzini 3 participants 6 messages Oct 25, 2022 – Oct 26, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util/log: do not close and reopen log files when flags are turned off

log_append makes sure that if you turn off the logging (which clears log_flags and makes need_to_open_file false) the old log is not overwritten. The usecase is that if you remove or move the file QEMU will not keep writing to the old file. However, this is not always the desited behavior, in particular having log_append==1 after changing the file name makes little sense.

When qemu_set_log_internal is called from the logfile monitor command, filename must be non-NULL and therefore changed_name must be true. Therefore, the only case where the file is closed and need_to_open_file == false is indeed when log_flags becomes zero. In this case, just flush the file and do not bother closing it, thus faking the same append behavior as previously.

The behavioral change is that changing the logfile twice, for example log1 -> log2 -> log1, will cause log1 to be overwritten. This can simply be documented, since it is not a particularly surprising behavior.

---
 util/log.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
util/log.c +5 -8
index d6eb0378c3a3..06d0173788dc 100644
@@ -44,7 +44,6 @@ static FILE *global_file;
 static __thread FILE *thread_file;
 
 int qemu_loglevel;
-static bool log_append;
 static bool log_per_thread;
 static GArray *debug_regions;
 
@@ -259,19 +258,19 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
     daemonized = is_daemonized();
     need_to_open_file = log_flags && !per_thread && (!daemonized || filename);
 
-    if (logfile && (!need_to_open_file || changed_name)) {
-        qatomic_rcu_set(&global_file, NULL);
-        if (logfile != stderr) {
+    if (logfile) {
+        fflush(logfile);
+        if (changed_name && logfile != stderr) {
             RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
             r->fd = logfile;
             call_rcu(r, rcu_close_file, rcu);
+            logfile = NULL;
         }
-        logfile = NULL;
     }
 
     if (!logfile && need_to_open_file) {
         if (filename) {
-            logfile = fopen(filename, log_append ? "a" : "w");
+            logfile = fopen(filename, "w");
             if (!logfile) {
                 error_setg_errno(errp, errno, "Error opening logfile %s",
                                  filename);
@@ -290,8 +289,6 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
             logfile = stderr;
         }
 
-        log_append = 1;
-
         qatomic_rcu_set(&global_file, logfile);
     }
     return true;
Signature · 1 line
2.37.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Paolo Bonzini

log_append makes sure that if you turn off the logging (which clears log_flags and makes need_to_open_file false) the old log is not overwritten. The usecase is that if you remove or move the file QEMU will not keep writing to the old file. However, this is not always the desited behavior, in particular having log_append==1 after changing the file name makes little sense.

When qemu_set_log_internal is called from the logfile monitor command, filename must be non-NULL and therefore changed_name must be true. Therefore, the only case where the file is closed and need_to_open_file == false is indeed when log_flags becomes zero. In this case, just flush the file and do not bother closing it, thus faking the same append behavior as previously.

The behavioral change is that changing the logfile twice, for example log1 -> log2 -> log1, will cause log1 to be overwritten. This can simply be documented, since it is not a particularly surprising behavior.

Quoted 5 lines
---
  util/log.c | 13 +++++--------
  1 file changed, 5 insertions(+), 8 deletions(-)

r~

Reviewed-by on v1 1/1
#2 lore raw
In reply to Paolo Bonzini
Quoted 14 lines · On Tue, 25 Oct 2022 11:21:19 +0200 Paolo Bonzini <[email protected]> wrote:

log_append makes sure that if you turn off the logging (which clears log_flags and makes need_to_open_file false) the old log is not overwritten. The usecase is that if you remove or move the file QEMU will not keep writing to the old file. However, this is not always the desited behavior, in particular having log_append==1 after changing the file name makes little sense.

When qemu_set_log_internal is called from the logfile monitor command, filename must be non-NULL and therefore changed_name must be true. Therefore, the only case where the file is closed and need_to_open_file == false is indeed when log_flags becomes zero. In this case, just flush the file and do not bother closing it, thus faking the same append behavior as previously.

Quoted 8 lines

The behavioral change is that changing the logfile twice, for example log1 -> log2 -> log1, will cause log1 to be overwritten. This can simply be documented, since it is not a particularly surprising behavior.

---

Heh I currently have a very similar patch in my tree :-)

I'll include this and other bug fixes as prerequisites for my on-going work on logging when daemonized.

Quoted 49 lines
 util/log.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
util/log.c +5 -8
index d6eb0378c3a3..06d0173788dc 100644
@@ -44,7 +44,6 @@ static FILE *global_file;
 static __thread FILE *thread_file;
 
 int qemu_loglevel;
-static bool log_append;
 static bool log_per_thread;
 static GArray *debug_regions;
 
@@ -259,19 +258,19 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
     daemonized = is_daemonized();
     need_to_open_file = log_flags && !per_thread && (!daemonized || filename);
 
-    if (logfile && (!need_to_open_file || changed_name)) {
-        qatomic_rcu_set(&global_file, NULL);
-        if (logfile != stderr) {
+    if (logfile) {
+        fflush(logfile);
+        if (changed_name && logfile != stderr) {
             RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
             r->fd = logfile;
             call_rcu(r, rcu_close_file, rcu);
+            logfile = NULL;
         }
-        logfile = NULL;
     }
 
     if (!logfile && need_to_open_file) {
         if (filename) {
-            logfile = fopen(filename, log_append ? "a" : "w");
+            logfile = fopen(filename, "w");
             if (!logfile) {
                 error_setg_errno(errp, errno, "Error opening logfile %s",
                                  filename);
@@ -290,8 +289,6 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
             logfile = stderr;
         }
 
-        log_append = 1;
-
         qatomic_rcu_set(&global_file, logfile);
     }
     return true;
#3 lore raw
Quoted 48 lines · On Tue, 25 Oct 2022 14:33:15 +0200 Greg Kurz <[email protected]> wrote:
Quoted 14 lines · On Tue, 25 Oct 2022 11:21:19 +0200 Paolo Bonzini <[email protected]> wrote:

log_append makes sure that if you turn off the logging (which clears log_flags and makes need_to_open_file false) the old log is not overwritten. The usecase is that if you remove or move the file QEMU will not keep writing to the old file. However, this is not always the desited behavior, in particular having log_append==1 after changing the file name makes little sense.

When qemu_set_log_internal is called from the logfile monitor command, filename must be non-NULL and therefore changed_name must be true. Therefore, the only case where the file is closed and need_to_open_file == false is indeed when log_flags becomes zero. In this case, just flush the file and do not bother closing it, thus faking the same append behavior as previously.

Quoted 8 lines

The behavioral change is that changing the logfile twice, for example log1 -> log2 -> log1, will cause log1 to be overwritten. This can simply be documented, since it is not a particularly surprising behavior.

---

Heh I currently have a very similar patch in my tree :-)

I'll include this and other bug fixes as prerequisites for my on-going work on logging when daemonized.

Quoted 15 lines
 util/log.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
util/log.c +0 -1
index d6eb0378c3a3..06d0173788dc 100644
@@ -44,7 +44,6 @@ static FILE *global_file;
 static __thread FILE *thread_file;
 
 int qemu_loglevel;
-static bool log_append;
 static bool log_per_thread;
 static GArray *debug_regions;
 
Quoted 6 lines

util/log.c static bool qemu_set_log_internal(const char *filename, bool changed_name, · line 263

Quoted 6 lines
util/log.c +0 -2
@@ -259,19 +258,19 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
     daemonized = is_daemonized();
     need_to_open_file = log_flags && !per_thread && (!daemonized || filename);
 
-    if (logfile && (!need_to_open_file || changed_name)) {
-        qatomic_rcu_set(&global_file, NULL);

Hmm... wait, shouldn't this NULLifying be performed...

Quoted 6 lines

util/log.c static bool qemu_set_log_internal(const char *filename, bool changed_name, · line 265

Quoted 6 lines
util/log.c +3 -1
-        if (logfile != stderr) {
+    if (logfile) {
+        fflush(logfile);
+        if (changed_name && logfile != stderr) {
             RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
             r->fd = logfile;

... here since we the following closes the global_file ?

Quoted 24 lines
Quoted 22 lines
util/log.c +2 -4
             call_rcu(r, rcu_close_file, rcu);
+            logfile = NULL;
         }
-        logfile = NULL;
     }
 
     if (!logfile && need_to_open_file) {
         if (filename) {
-            logfile = fopen(filename, log_append ? "a" : "w");
+            logfile = fopen(filename, "w");
             if (!logfile) {
                 error_setg_errno(errp, errno, "Error opening logfile %s",
                                  filename);
@@ -290,8 +289,6 @@ static bool qemu_set_log_internal(const char *filename, bool changed_name,
             logfile = stderr;
         }
 
-        log_append = 1;
-
         qatomic_rcu_set(&global_file, logfile);
     }
     return true;
#4 lore raw

Il mar 25 ott 2022, 16:39 Greg Kurz <[email protected]> ha scritto:

Quoted 13 lines
Quoted 2 lines

util/log.c static bool qemu_set_log_internal(const char *filename, bool changed_name, · line 263

Quoted 2 lines
util/log.c +0 -2
-    if (logfile && (!need_to_open_file || changed_name)) {
-        qatomic_rcu_set(&global_file, NULL);

Hmm... wait, shouldn't this NULLifying be performed...

Quoted 6 lines

util/log.c static bool qemu_set_log_internal(const char *filename, bool changed_name, · line 265

Quoted 6 lines
util/log.c +3 -1
-        if (logfile != stderr) {
+    if (logfile) {
+        fflush(logfile);
+        if (changed_name && logfile != stderr) {
             RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
             r->fd = logfile;
Quoted 4 lines

... here since we the following closes the global_file ?

Quoted 1 line
Quoted 1 line
             call_rcu(r, rcu_close_file, rcu);

Yes it should.

Paolo

Quoted 27 lines
Quoted 11 lines

+ logfile = NULL;

Quoted 10 lines
         }
util/log.c +1 -2
-        logfile = NULL;
     }
 
     if (!logfile && need_to_open_file) {
         if (filename) {
-            logfile = fopen(filename, log_append ? "a" : "w");
+            logfile = fopen(filename, "w");
             if (!logfile) {
                 error_setg_errno(errp, errno, "Error opening logfile

%s",

Quoted 2 lines

Review static bool qemu_set_log_internal(const char

Quoted 2 lines
                                  filename);
(quoted diff) +0 -0
@@ -290,8 +289,6 @@ static bool qemu_set_log_internal(const char

*filename, bool changed_name,

Quoted 10 lines
Quoted 8 lines
util/log.c +0 -2
             logfile = stderr;
         }
 
-        log_append = 1;
-
         qatomic_rcu_set(&global_file, logfile);
     }
     return true;
#5 lore raw
Quoted 15 lines · On Tue, 25 Oct 2022 22:27:36 +0200 Paolo Bonzini <[email protected]> wrote:

Il mar 25 ott 2022, 16:39 Greg Kurz <[email protected]> ha scritto:

Quoted 13 lines
Quoted 2 lines

util/log.c static bool qemu_set_log_internal(const char *filename, bool changed_name, · line 263

Quoted 2 lines
util/log.c +0 -2
-    if (logfile && (!need_to_open_file || changed_name)) {
-        qatomic_rcu_set(&global_file, NULL);

Hmm... wait, shouldn't this NULLifying be performed...

Quoted 6 lines
Quoted 6 lines
util/log.c +3 -1
-        if (logfile != stderr) {
+    if (logfile) {
+        fflush(logfile);
+        if (changed_name && logfile != stderr) {
             RCUCloseFILE *r = g_new0(RCUCloseFILE, 1);
             r->fd = logfile;
Quoted 7 lines
Quoted 4 lines

... here since we the following closes the global_file ?

Quoted 1 line
Quoted 1 line
             call_rcu(r, rcu_close_file, rcu);

Yes it should.

I'll fix this when I repost the full series.

Cheers,

--
Greg

Quoted 29 lines

Paolo

Quoted 27 lines
Quoted 11 lines

+ logfile = NULL;

Quoted 10 lines
         }
util/log.c +1 -2
-        logfile = NULL;
     }
 
     if (!logfile && need_to_open_file) {
         if (filename) {
-            logfile = fopen(filename, log_append ? "a" : "w");
+            logfile = fopen(filename, "w");
             if (!logfile) {
                 error_setg_errno(errp, errno, "Error opening logfile

%s",

Quoted 2 lines

Review static bool qemu_set_log_internal(const char

Quoted 2 lines
                                  filename);
(quoted diff) +0 -0
@@ -290,8 +289,6 @@ static bool qemu_set_log_internal(const char

*filename, bool changed_name,

Quoted 10 lines
Quoted 8 lines
util/log.c +0 -2
             logfile = stderr;
         }
 
-        log_append = 1;
-
         qatomic_rcu_set(&global_file, logfile);
     }
     return true;