TBTK
Streams.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Björnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_STREAMS
24 #define COM_DAFER45_TBTK_STREAMS
25 
26 #include <ostream>
27 #include <fstream>
28 #include <string>
29 #include <vector>
30 
31 namespace TBTK{
32 namespace Util{
33 
34 class Streams{
35 public:
37  static std::ostream out;
38 
40  static std::ostream log;
41 
43  static std::ostream err;
44 
46  static void setStdMuteOut();
47 
49  static void setStdMuteErr();
50 
52  static void openLog(std::string filename = "TBTKLog");
53 
55  static void closeLog();
56 
58  static bool logIsOpen();
59 private:
61  static class NullBuffer : public std::streambuf{
62  public:
63  int overflow(int c);
64  } nullBuffer;
65 
67  class ForkBuffer : public std::streambuf{
68  public:
70  ForkBuffer(
71  std::basic_ostream<char, std::char_traits<char>> *ostream1,
72  std::basic_ostream<char, std::char_traits<char>> *ostream2
73  );
74 
76  void mute(int n, bool isMute);
77  private:
79  std::basic_ostream<char, std::char_traits<char>> *ostreams[2];
80 
82  bool isMute[2];
83 
86  int overflow(int c);
87  };
88 
90  class LogBuffer : public std::streambuf{
91  public:
93  LogBuffer();
94 
96  ~LogBuffer();
97 
99  void open(std::string fileName);
100 
102  void close();
103 
105  bool isOpen();
106  private:
108  std::ofstream fout;
109 
112  int overflow(int c);
113  };
114 
116  static std::ostream null;
117 
119  static ForkBuffer stdOutBuffer;
120 
122  static LogBuffer stdLogBuffer;
123 
125  static ForkBuffer stdErrBuffer;
126 
128  static std::ofstream logFile;
129 };
130 
131 }; //End of namespace Util
132 }; //End of namespace TBTK
133 
134 #endif
135 
static std::ostream err
Definition: Streams.h:43
static void setStdMuteErr()
Definition: Streams.cpp:57
static bool logIsOpen()
Definition: Streams.cpp:70
static std::ostream out
Definition: Streams.h:37
static void setStdMuteOut()
Definition: Streams.cpp:52
Definition: AbstractOperator.h:26
static void openLog(std::string filename="TBTKLog")
Definition: Streams.cpp:62
Definition: Streams.h:34
static void closeLog()
Definition: Streams.cpp:66
static std::ostream log
Definition: Streams.h:40