Message ID | 1481559490-13844-3-git-send-email-ross.lagerwall@citrix.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On Mon, Dec 12, 2016 at 04:18:05PM +0000, Ross Lagerwall wrote: > Using both stdout and stderr interleaved without newlines can result in > strange output when using line buffered mode (e.g. a terminal) or when > fully buffered (e.g. redirected to a file). Set both to unbuffered mode > to fix this. > > Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com> > --- > tools/misc/xen-livepatch.c | 8 +++++++- > 1 file changed, 7 insertions(+), 1 deletion(-) > > diff --git a/tools/misc/xen-livepatch.c b/tools/misc/xen-livepatch.c > index f6c7c8a..2f6721b 100644 > --- a/tools/misc/xen-livepatch.c > +++ b/tools/misc/xen-livepatch.c > @@ -330,7 +330,6 @@ int action_func(int argc, char *argv[], unsigned int idx) > } > > printf("."); > - fflush(stdout); > usleep(DELAY); > } while ( ++retry < RETRIES ); > > @@ -416,6 +415,13 @@ int main(int argc, char *argv[]) > { > int i, j = 0, ret; > > + /* > + * Set stdout and stderr to be unbuffered to avoid having to fflush > + * when printing without a newline. > + */ > + setvbuf(stdout, NULL, _IONBF, 0); > + setvbuf(stderr, NULL, _IONBF, 0); > + OOI why would you need to set stderr? It is unbuffered by default. There is no harm in setting it though. And perhaps you should check the return value of setvbuf? Wei. > if ( argc <= 1 ) > { > show_help(); > -- > 2.7.4 >
On 12/12/2016 05:02 PM, Wei Liu wrote: > On Mon, Dec 12, 2016 at 04:18:05PM +0000, Ross Lagerwall wrote: >> Using both stdout and stderr interleaved without newlines can result in >> strange output when using line buffered mode (e.g. a terminal) or when >> fully buffered (e.g. redirected to a file). Set both to unbuffered mode >> to fix this. >> >> Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com> >> --- >> tools/misc/xen-livepatch.c | 8 +++++++- >> 1 file changed, 7 insertions(+), 1 deletion(-) >> >> diff --git a/tools/misc/xen-livepatch.c b/tools/misc/xen-livepatch.c >> index f6c7c8a..2f6721b 100644 >> --- a/tools/misc/xen-livepatch.c >> +++ b/tools/misc/xen-livepatch.c >> @@ -330,7 +330,6 @@ int action_func(int argc, char *argv[], unsigned int idx) >> } >> >> printf("."); >> - fflush(stdout); >> usleep(DELAY); >> } while ( ++retry < RETRIES ); >> >> @@ -416,6 +415,13 @@ int main(int argc, char *argv[]) >> { >> int i, j = 0, ret; >> >> + /* >> + * Set stdout and stderr to be unbuffered to avoid having to fflush >> + * when printing without a newline. >> + */ >> + setvbuf(stdout, NULL, _IONBF, 0); >> + setvbuf(stderr, NULL, _IONBF, 0); >> + > > OOI why would you need to set stderr? It is unbuffered by default. > > There is no harm in setting it though. Good point. I'll drop it since it is unnecessary. > > And perhaps you should check the return value of setvbuf? > I don't think there's anything that can be done if it fails, so there's not much point in checking the return value.
On Wed, Dec 14, 2016 at 06:44:43AM +0000, Ross Lagerwall wrote: [...] > > > >And perhaps you should check the return value of setvbuf? > > > > I don't think there's anything that can be done if it fails, so there's not > much point in checking the return value. > This is a fair point. Wei. > -- > Ross Lagerwall
diff --git a/tools/misc/xen-livepatch.c b/tools/misc/xen-livepatch.c index f6c7c8a..2f6721b 100644 --- a/tools/misc/xen-livepatch.c +++ b/tools/misc/xen-livepatch.c @@ -330,7 +330,6 @@ int action_func(int argc, char *argv[], unsigned int idx) } printf("."); - fflush(stdout); usleep(DELAY); } while ( ++retry < RETRIES ); @@ -416,6 +415,13 @@ int main(int argc, char *argv[]) { int i, j = 0, ret; + /* + * Set stdout and stderr to be unbuffered to avoid having to fflush + * when printing without a newline. + */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + if ( argc <= 1 ) { show_help();
Using both stdout and stderr interleaved without newlines can result in strange output when using line buffered mode (e.g. a terminal) or when fully buffered (e.g. redirected to a file). Set both to unbuffered mode to fix this. Signed-off-by: Ross Lagerwall <ross.lagerwall@citrix.com> --- tools/misc/xen-livepatch.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)