QMCPACK
InfoStream.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2017 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Mark Dewing, mdewing@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 #include "InfoStream.h"
14 #include <fstream>
15 #include <cassert>
16 
17 InfoStream::InfoStream(std::ostream* output_stream)
18  : currStream(output_stream), outputStream(output_stream), nullStream(nullptr)
19 {
20  if (output_stream == nullptr)
22 }
23 
24 void InfoStream::setStream(std::ostream* output_stream)
25 {
26  if (outputStream == output_stream)
27  return;
28  assert(checkCurr());
29  if (output_stream == nullptr)
31  else
32  outputStream = output_stream;
33  if (currStream != &nullStream)
35  fileStream.reset();
36  assert(checkCurr());
37 }
38 
39 void InfoStream::flush() { outputStream->flush(); }
40 
42 {
43  assert(checkCurr());
45 }
46 
48 {
49  assert(checkCurr());
51 }
52 
54 {
55  assert(checkCurr());
57  fileStream.reset();
58 }
59 
60 void InfoStream::redirectToFile(const std::string& fname)
61 {
62  assert(checkCurr());
63  fileStream = std::make_unique<std::ofstream>(fname);
64  outputStream = fileStream.get();
65  if (currStream != &nullStream)
67  assert(checkCurr());
68 }
69 
71 {
72  if (this == &info)
73  return;
74  assert(checkCurr());
76  if (currStream != &nullStream)
78  fileStream.reset();
79  assert(checkCurr());
80 }
std::ostream nullStream
Definition: InfoStream.h:73
Interface to output streams.
Definition: InfoStream.h:28
InfoStream(std::ostream *output_stream)
Definition: InfoStream.cpp:17
void resume()
Continue output on the stream used before pausing.
Definition: InfoStream.cpp:47
void redirectToFile(const std::string &fname)
Open a file and output to that file.
Definition: InfoStream.cpp:60
void setStream(std::ostream *output_stream)
Definition: InfoStream.cpp:24
Declaration of InfoStream class.
void flush()
flush stream buffer
Definition: InfoStream.cpp:39
void redirectToSameStream(InfoStream &info)
Copy a stream.
Definition: InfoStream.cpp:70
void shutOff()
Permanently turn off the stream.
Definition: InfoStream.cpp:53
std::ostream * outputStream
Definition: InfoStream.h:70
std::unique_ptr< std::ofstream > fileStream
Definition: InfoStream.h:76
bool checkCurr() const
Definition: InfoStream.h:64
std::ostream * currStream
Definition: InfoStream.h:67
void pause()
Stop output (redirect to a null stream)
Definition: InfoStream.cpp:41