bin: workaround flushing issues

* bin/common_cout.cc (check_cout): Force a flush of cout if more than
20ms has elapsed since the last explicit flush.
* bin/common_setup.cc (setup): Untie cin and cout if the input
is not a TTY, so that cout is flush less often.
* NEWS: Mention the change.
This commit is contained in:
Alexandre Duret-Lutz 2016-10-03 15:19:02 +02:00
parent 0678d1a662
commit 0c9c4be4ae
3 changed files with 44 additions and 6 deletions

View file

@ -1,5 +1,5 @@
// -*- coding: utf-8 -*-
// Copyright (C) 2012 Laboratoire de Recherche et Développement de
// Copyright (C) 2012, 2016 Laboratoire de Recherche et Développement de
// l'Epita (LRDE).
//
// This file is part of Spot, a model checking library.
@ -20,18 +20,42 @@
#include "common_sys.hh"
#include "common_cout.hh"
#include <iostream>
#include <chrono>
#include "error.h"
namespace
{
static std::chrono::steady_clock::time_point last_flush;
static void do_check_cout()
{
// Make sure we abort if we can't write to std::cout anymore
// (like disk full or broken pipe with SIGPIPE ignored).
if (!std::cout)
error(2, 0, "error writing to standard output");
}
}
void check_cout()
{
// Make sure we abort if we can't write to std::cout anymore
// (like disk full or broken pipe with SIGPIPE ignored).
if (!std::cout)
error(2, 0, "error writing to standard output");
// If we haven't flushed explicitly for more than 20ms, do it now.
// Otherwise we would have to wait for the buffer to fill up, and
// this could take a long time.
auto now = std::chrono::steady_clock::now();
auto ms =
std::chrono::duration_cast<std::chrono::milliseconds>(now - last_flush);
if (ms.count() >= 20)
{
last_flush = now;
std::cout.flush();
}
do_check_cout();
}
void flush_cout()
{
last_flush = std::chrono::steady_clock::now();
std::cout.flush();
check_cout();
do_check_cout();
}