QMCPACK
InfoStream.h
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 /** @file InfoStream.h
14  * @brief Declaration of InfoStream class.
15  */
16 
17 #ifndef INFOSTREAM_H
18 #define INFOSTREAM_H
19 
20 #include <fstream>
21 #include <memory>
22 #include <iostream>
23 
24 /**
25  * Interface to output streams. Can redirect output to stdout/stderr, a file, or a null stream.
26  */
27 
29 {
30 public:
31  InfoStream(std::ostream* output_stream);
32 
33  InfoStream(const InfoStream& in) = delete;
34  InfoStream& operator=(const InfoStream& in) = delete;
35 
36  /// returns current stream
37  std::ostream& getStream(const std::string& tag = "") { return *currStream; }
38 
39  void setStream(std::ostream* output_stream);
40 
41  /// flush stream buffer
42  void flush();
43 
44  /// Stop output (redirect to a null stream)
45  void pause();
46 
47  /// Continue output on the stream used before pausing
48  void resume();
49 
50  /// check if the stream is active
51  bool isActive() const { return currStream != &nullStream; }
52 
53  /// Open a file and output to that file
54  void redirectToFile(const std::string& fname);
55 
56  /// Copy a stream
57  void redirectToSameStream(InfoStream& info);
58 
59  /// Permanently turn off the stream
60  void shutOff();
61 
62 private:
63  // check currStream, it can only be outputStream or &nullStream
64  bool checkCurr() const { return currStream == outputStream || currStream == &nullStream; }
65 
66  // current stream that InfoStream represents
67  std::ostream* currStream;
68 
69  // the output stream when InfoStream IsActive. (1. from external, 2. nullStream (off), 3. fileStream)
70  std::ostream* outputStream;
71 
72  // Created at construction. Used during pause and shutoff
73  std::ostream nullStream;
74 
75  // ofstream pointer allocated when file output is used
76  std::unique_ptr<std::ofstream> fileStream;
77 };
78 
79 template<class T>
80 inline InfoStream& operator<<(InfoStream& o, const T& val)
81 {
82  o.getStream() << val;
83  return o;
84 }
85 
86 
87 #endif
bool isActive() const
check if the stream is active
Definition: InfoStream.h:51
std::ostream nullStream
Definition: InfoStream.h:73
Interface to output streams.
Definition: InfoStream.h:28
std::ostream & getStream(const std::string &tag="")
returns current stream
Definition: InfoStream.h:37
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
InfoStream & operator<<(InfoStream &o, const T &val)
Definition: InfoStream.h:80
void flush()
flush stream buffer
Definition: InfoStream.cpp:39
void redirectToSameStream(InfoStream &info)
Copy a stream.
Definition: InfoStream.cpp:70
InfoStream & operator=(const InfoStream &in)=delete
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