void threshold_float(const float t);
+ /**
+ * set the precision for the underlying stream and returns the
+ * previous stream precision. This fuction mimics
+ * http://www.cplusplus.com/reference/ios/ios_base/precision/
+ */
+ std::streamsize precision (const std::streamsize prec);
+
+
+ /**
+ * set the width for the underlying stream and returns the
+ * previous stream width. This fuction mimics
+ * http://www.cplusplus.com/reference/ios/ios_base/width/
+ */
+ std::streamsize width (const std::streamsize wide);
+
+
+ /**
+ * set the flags for the underlying stream and returns the
+ * previous stream flags. This fuction mimics
+ * http://www.cplusplus.com/reference/ios/ios_base/flags/
+ */
+ std::ios::fmtflags flags(const std::ios::fmtflags f);
+
+
/**
* Output a constant something through this stream.
*/
*/
bool at_newline;
+ /**
+ * The flags used to modify how
+ * the stream returned by get_stream() is used.
+ */
+ std::ios_base::fmtflags stream_flags;
+
+ /**
+ * The minimum width of characters
+ * used to represent a number. Used by width() to
+ * change the stream return by get_stream()
+ */
+ std::streamsize stream_width;
+
+ /**
+ * The maximum width of characters
+ * used to represent a floating point number.
+ * Used by precision() to
+ * change the stream return by get_stream()
+ */
+ std::streamsize stream_precision;
+
/**
* Print head of line. This prints optional time information and the
* contents of the prefix stack.
*/
std::ostringstream &get_stream()
{
- outstreams.get().setf(std::ios::showpoint | std::ios::left);
return outstreams.get();
}
LogStream &
LogStream::operator<< (const T &t)
{
- // print to the internal stringstream:
- get_stream() << t;
+ // save the state of the output stream
+ std::ostringstream &stream = get_stream();
+
+ const std::ios::fmtflags old_flags = stream.flags(stream_flags);
+ const unsigned int old_precision = stream.precision (stream_precision);
+ const unsigned int old_width = stream.width (stream_width);
+
+ // print to the internal stringstream, using the flags we have just set for
+ // the stream object:
+ stream << t;
+
+ // reset output format to whatever it was before
+ stream.flags (old_flags);
+ stream.precision(old_precision);
+ stream.width(old_width);
+
return *this;
}
LogStream &
LogStream::operator<< (const double t)
{
+ // save the state of out stream
+ std::ostringstream &stream = get_stream();
+ const std::ios::fmtflags old_flags = stream.flags(stream_flags);
+ const unsigned int old_precision = stream.precision (stream_precision);
+ const unsigned int old_width = stream.width (stream_width);
+
// we have to make sure that we don't catch NaN's and +-Inf's with the
// test, because for these denormals all comparisons are always false.
// thus, for a NaN, both t<=0 and t>=0 are false at the same time, which
// can't be said for any other number
if (! (t<=0) && !(t>=0))
- get_stream() << t;
+ stream << t;
else if (std::fabs(t) < double_threshold)
- get_stream() << '0';
+ stream << '0';
else
- get_stream() << t*(1.+offset);
+ stream << t*(1.+offset);
+
+ // reset output format
+ stream.flags (old_flags);
+ stream.precision(old_precision);
+ stream.width(old_width);
return *this;
}
LogStream &
LogStream::operator<< (const float t)
{
+ // save the state of out stream
+ std::ostringstream &stream = get_stream();
+ const std::ios::fmtflags old_flags = stream.flags(stream_flags);
+ const unsigned int old_precision = stream.precision (stream_precision);
+ const unsigned int old_width = stream.width (stream_width);
+
// we have to make sure that we don't catch NaN's and +-Inf's with the
// test, because for these denormals all comparisons are always false.
// thus, for a NaN, both t<=0 and t>=0 are false at the same time, which
// can't be said for any other number
if (! (t<=0) && !(t>=0))
- get_stream() << t;
+ stream << t;
else if (std::fabs(t) < float_threshold)
- get_stream() << '0';
+ stream << '0';
else
- get_stream() << t*(1.+offset);
+ stream << t*(1.+offset);
+
+ // reset output format
+ stream.flags (old_flags);
+ stream.precision(old_precision);
+ stream.width(old_width);
return *this;
}
--- /dev/null
+//---------------------------- log_precision.cc ---------------------------
+// $Id$
+// Version: $Name$
+//
+// Copyright (C) 2005, 2006, 2007, 2013 by the deal.II authors
+//
+// This file is subject to QPL and may not be distributed
+// without copyright and license information. Please refer
+// to the file deal.II/doc/license.html for the text and
+// further information on this license.
+//
+//---------------------------- log_precision.cc ---------------------------
+
+// test that we can set the precision of LogStream objects
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <fstream>
+#include <iomanip>
+#include <limits>
+
+int main ()
+{
+ std::ofstream logfile("log_precision/output");
+ deallog << std::setprecision(3);
+ deallog.attach(logfile);
+ deallog.depth_console(0);
+ deallog.threshold_double(1.e-10);
+
+ deallog << numbers::PI << std::endl;
+
+ // test with a different precision
+ deallog.precision(12);
+ deallog << numbers::PI << std::endl;
+
+ // ensure that the precision of the underlying file stream object remained
+ // unchanged
+ deallog.get_file_stream() << numbers::PI << std::endl;
+
+ return 0;
+}
+
--- /dev/null
+//---------------------------- log_width.cc ---------------------------
+// $Id$
+// Version: $Name$
+//
+// Copyright (C) 2005, 2006, 2007, 2013 by the deal.II authors
+//
+// This file is subject to QPL and may not be distributed
+// without copyright and license information. Please refer
+// to the file deal.II/doc/license.html for the text and
+// further information on this license.
+//
+//---------------------------- log_width.cc ---------------------------
+
+// test that we can set the width of LogStream objects
+
+#include "../tests.h"
+#include <deal.II/base/logstream.h>
+#include <fstream>
+#include <iomanip>
+#include <limits>
+
+int main ()
+{
+ std::ofstream logfile("log_width/output");
+ deallog << std::setprecision(3);
+ deallog.attach(logfile);
+ deallog.depth_console(0);
+ deallog.threshold_double(1.e-10);
+
+ deallog << numbers::PI*2 << 42 << std::endl;
+
+ // test with a different width
+ deallog.width(18);
+ deallog << numbers::PI*2 << 42 << std::endl;
+
+ // ensure that the width of the underlying file stream object remained
+ // unchanged
+ deallog.get_file_stream() << numbers::PI*2 << 42 << std::endl;
+
+ return 0;
+}
+