Skip to contentSkip to conversation

qemu-progress: redirct qemu progress message to another file stream

PATCH In review zhenwei pi 2 participants 3 messages Apr 16, 2018 – Apr 19, 2018

lore
Share & tools
lore

zhenwei pi

author
#0 lore raw

PATCH 1/1 qemu-progress: redirct qemu progress message to another file stream

currently qemu progress message only print into stdout, and other thread may print some log into stdout at the same time.
add a new api qemu_progress_set_output to redirect progress message to another file stream.

---
 include/qemu-common.h |  1 +
 util/qemu-progress.c  | 22 +++++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)
include/qemu-common.h +1 -0
index 8a4f63c..511e7e0 100644
@@ -128,6 +128,7 @@ ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
 void qemu_progress_init(int enabled, float min_skip);
 void qemu_progress_end(void);
 void qemu_progress_print(float delta, int max);
+int qemu_progress_set_output(FILE *output);
 const char *qemu_get_vm_name(void);
 
 #define QEMU_FILE_TYPE_BIOS   0
util/qemu-progress.c +19 -3
index 3c2223c..dd7aa52 100644
@@ -31,6 +31,7 @@ struct progress_state {
     float min_skip;
     void (*print)(void);
     void (*end)(void);
+    FILE *output;
 };
 
 static struct progress_state state;
@@ -43,17 +44,18 @@ static volatile sig_atomic_t print_pending;
  */
 static void progress_simple_print(void)
 {
-    printf("    (%3.2f/100%%)\r", state.current);
-    fflush(stdout);
+    fprintf(state.output, "    (%3.2f/100%%)\r", state.current);
+    fflush(state.output);
 }
 
 static void progress_simple_end(void)
 {
-    printf("\n");
+    fprintf(state.output, "\n");
 }
 
 static void progress_simple_init(void)
 {
+    state.output = stdout;
     state.print = progress_simple_print;
     state.end = progress_simple_end;
 }
@@ -129,6 +131,20 @@ void qemu_progress_end(void)
 }
 
 /*
+ * Redirect progress into another file stream.
+ * @output is the new file stream.
+ */
+int qemu_progress_set_output(FILE *output)
+{
+    if (!output) {
+        return -EINVAL;
+    }
+
+    state.output = output;
+    return 0;
+}
+
+/*
  * Report progress.
  * @delta is how much progress we made.
  * If @max is zero, @delta is an absolut value of the total job done.
Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines · zhenwei pi

currently qemu progress message only print into stdout, and other thread may print some log into stdout at the same time.
add a new api qemu_progress_set_output to redirect progress message to another file stream.

---
 include/qemu-common.h |  1 +
 util/qemu-progress.c  | 22 +++++++++++++++++++---
 2 files changed, 20 insertions(+), 3 deletions(-)

Well, but after this patch, qemu will still only print progress to stdout because qemu_progress_set_output() isn't used anywhere...

Quoted 25 lines
include/qemu-common.h +1 -0
index 8a4f63c..511e7e0 100644
@@ -128,6 +128,7 @@ ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
 void qemu_progress_init(int enabled, float min_skip);
 void qemu_progress_end(void);
 void qemu_progress_print(float delta, int max);
+int qemu_progress_set_output(FILE *output);
 const char *qemu_get_vm_name(void);
 
 #define QEMU_FILE_TYPE_BIOS   0
util/qemu-progress.c +1 -0
index 3c2223c..dd7aa52 100644
@@ -31,6 +31,7 @@ struct progress_state {
     float min_skip;
     void (*print)(void);
     void (*end)(void);
+    FILE *output;
 };
 
 static struct progress_state state;

util/qemu-progress.c static volatile sig_atomic_t print_pending; · line 48

Quoted 7 lines
util/qemu-progress.c +2 -2
@@ -43,17 +44,18 @@ static volatile sig_atomic_t print_pending;
  */
 static void progress_simple_print(void)
 {
-    printf("    (%3.2f/100%%)\r", state.current);
-    fflush(stdout);
+    fprintf(state.output, "    (%3.2f/100%%)\r", state.current);
+    fflush(state.output);

Also, I'm not sure we should use \r when printing to anything but stdout.

Max

Quoted 36 lines
util/qemu-progress.c +16 -1
 }
 
 static void progress_simple_end(void)
 {
-    printf("\n");
+    fprintf(state.output, "\n");
 }
 
 static void progress_simple_init(void)
 {
+    state.output = stdout;
     state.print = progress_simple_print;
     state.end = progress_simple_end;
 }
@@ -129,6 +131,20 @@ void qemu_progress_end(void)
 }
 
 /*
+ * Redirect progress into another file stream.
+ * @output is the new file stream.
+ */
+int qemu_progress_set_output(FILE *output)
+{
+    if (!output) {
+        return -EINVAL;
+    }
+
+    state.output = output;
+    return 0;
+}
+
+/*
  * Report progress.
  * @delta is how much progress we made.
  * If @max is zero, @delta is an absolut value of the total job done.
author
#2 lore raw
Quoted 6 lines · Max Reitz
Quoted 5 lines · zhenwei pi

currently qemu progress message only print into stdout, and other thread may print some log into stdout at the same time.
add a new api qemu_progress_set_output to redirect progress message to another file stream.

Quoted 8 lines
Quoted 5 lines
---
  include/qemu-common.h |  1 +
  util/qemu-progress.c  | 22 +++++++++++++++++++---
  2 files changed, 20 insertions(+), 3 deletions(-)

Well, but after this patch, qemu will still only print progress to stdout because qemu_progress_set_output() isn't used anywhere...

this patch only provides the basic redirection function. if this patch is approved, another patch (Ex, qemu convert progress in qemu-img.c) will be send.

Quoted 34 lines
Quoted 25 lines
include/qemu-common.h +1 -0
index 8a4f63c..511e7e0 100644
@@ -128,6 +128,7 @@ ssize_t qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send);
  void qemu_progress_init(int enabled, float min_skip);
  void qemu_progress_end(void);
  void qemu_progress_print(float delta, int max);
+int qemu_progress_set_output(FILE *output);
  const char *qemu_get_vm_name(void);
 
  #define QEMU_FILE_TYPE_BIOS   0
util/qemu-progress.c +1 -0
index 3c2223c..dd7aa52 100644
@@ -31,6 +31,7 @@ struct progress_state {
      float min_skip;
      void (*print)(void);
      void (*end)(void);
+    FILE *output;
  };
 
  static struct progress_state state;

util/qemu-progress.c static volatile sig_atomic_t print_pending; · line 48

Quoted 7 lines
util/qemu-progress.c +2 -2
@@ -43,17 +44,18 @@ static volatile sig_atomic_t print_pending;
   */
  static void progress_simple_print(void)
  {
-    printf("    (%3.2f/100%%)\r", state.current);
-    fflush(stdout);
+    fprintf(state.output, "    (%3.2f/100%%)\r", state.current);
+    fflush(state.output);

Also, I'm not sure we should use \r when printing to anything but stdout.

Quoted 1 line

Max

qemu redirects progress message into a file, other process reads the file and uses '\r' to split the string, and we can get the progress exactly.

Quoted 37 lines
Quoted 36 lines
util/qemu-progress.c +16 -1
  }
 
  static void progress_simple_end(void)
  {
-    printf("\n");
+    fprintf(state.output, "\n");
  }
 
  static void progress_simple_init(void)
  {
+    state.output = stdout;
      state.print = progress_simple_print;
      state.end = progress_simple_end;
  }
@@ -129,6 +131,20 @@ void qemu_progress_end(void)
  }
 
  /*
+ * Redirect progress into another file stream.
+ * @output is the new file stream.
+ */
+int qemu_progress_set_output(FILE *output)
+{
+    if (!output) {
+        return -EINVAL;
+    }
+
+    state.output = output;
+    return 0;
+}
+
+/*
   * Report progress.
   * @delta is how much progress we made.
   * If @max is zero, @delta is an absolut value of the total job done.