The Original CHomP Software
Classes | Public Member Functions | Public Attributes | Protected Attributes | List of all members
chomp::homology::outputstream Class Reference

This class defines an output stream for replacing the standard 'cout'. More...

#include <textfile.h>

Classes

struct  mute
 Local mute of the stream. More...
 

Public Member Functions

 outputstream (std::ostream &_out=std::cout, bool _show=true, bool _flush=false)
 The default constructor. More...
 
 ~outputstream ()
 The destructor. More...
 
void logfile (const char *filename)
 Turns on logging to a file. More...
 
void logfile (const outputstream &other)
 Turns on logging to the same file as some other stream. More...
 
std::ofstream * getlogstream (void)
 Returns a pointer to the stream which is working as a log file. More...
 
void keepforever (void)
 Makes this stream keep the allocated file open for ever, and not close it even in the destructor. More...
 
int precision () const
 Returns the precision of the main output stream. More...
 

Public Attributes

bool show
 If this variable is set to true then everything that is written to this stream is also written to 'cout'. More...
 
bool log
 If this variable is set to true then everything that is written to this stream is also written to the log file. More...
 
bool flush
 If this variable is set to true then both the output stream 'cout' and the log file are flushed after every write operation. More...
 
std::ostream & out
 A reference to the main output stream bound with this stream. More...
 

Protected Attributes

std::ofstream * logstream
 A pointer to the log file stream. More...
 
bool copied
 This variable is set to true if this log file pointer was copied from another output stream, and therefore it should not be deleted by this stream. More...
 

Detailed Description

This class defines an output stream for replacing the standard 'cout'.

It has the additional features of flushing the output after every operation, suppressing the output, or logging the output to a file.

Definition at line 63 of file textfile.h.

Constructor & Destructor Documentation

◆ outputstream()

chomp::homology::outputstream::outputstream ( std::ostream &  _out = std::cout,
bool  _show = true,
bool  _flush = false 
)
inline

The default constructor.

Definition at line 140 of file textfile.h.

141 : out (_out)
142{
143 show = _show;
144 flush = _flush;
145 log = false;
146 logstream = NULL;
147 copied = false;
148 return;
149} /* outputstream::outputstream */
bool copied
This variable is set to true if this log file pointer was copied from another output stream,...
Definition: textfile.h:134
bool flush
If this variable is set to true then both the output stream 'cout' and the log file are flushed after...
Definition: textfile.h:94
bool show
If this variable is set to true then everything that is written to this stream is also written to 'co...
Definition: textfile.h:84
std::ostream & out
A reference to the main output stream bound with this stream.
Definition: textfile.h:101
std::ofstream * logstream
A pointer to the log file stream.
Definition: textfile.h:129
bool log
If this variable is set to true then everything that is written to this stream is also written to the...
Definition: textfile.h:88

References copied, flush, log, logstream, and show.

◆ ~outputstream()

chomp::homology::outputstream::~outputstream ( )
inline

The destructor.

Definition at line 201 of file textfile.h.

202{
203 if (!copied && logstream)
204 delete logstream;
205 return;
206} /* outputstream::~outputstream */

References copied, and logstream.

Member Function Documentation

◆ getlogstream()

std::ofstream * chomp::homology::outputstream::getlogstream ( void  )
inline

Returns a pointer to the stream which is working as a log file.

Returns 0 if no such stream is bound with this output stream.

Definition at line 208 of file textfile.h.

209{
210 return logstream;
211} /* outputstream::getlogstream */

References logstream.

◆ keepforever()

void chomp::homology::outputstream::keepforever ( void  )
inline

Makes this stream keep the allocated file open for ever, and not close it even in the destructor.

It is assumed then that the file will be closed automatically by the operating system.

Definition at line 213 of file textfile.h.

214{
215 copied = true;
216 return;
217} /* outputstream::getlogstream */

References copied.

◆ logfile() [1/2]

void chomp::homology::outputstream::logfile ( const char *  filename)
inline

Turns on logging to a file.

If the file exists, its contents is ereased.

Definition at line 151 of file textfile.h.

152{
153 // delete the previous log stream
154 if (logstream && !copied)
155 delete logstream;
156
157 // make a note that this output stream is the master copy
158 copied = false;
159
160 // create a file stream to log the output to;
161 // note: if "keepforever" is applied to the output stream
162 // then this data structure is not released and produces
163 // a small memory leak; however, the file is automatically
164 // closed by the system; this may be necessary to avoid
165 // a crash of the program if any other output streams
166 // still want to write to a copy of this stream
167 // upon program's shutdown
168 logstream = new std::ofstream (filename);
169
170 // warn if the log file could not be created
171 if (!logstream || !*logstream)
172 {
173 out << "WARNING: Can't create '" << filename <<
174 "'. Logging to a file has been turned off." <<
175 std::endl;
176 if (logstream)
177 delete logstream;
178 logstream = NULL;
179 }
180
181 // otherwise set a flag indicating that logging to a file is on
182 else
183 log = true;
184
185 return;
186} /* outputstream::logfile */

References copied, log, logstream, and out.

◆ logfile() [2/2]

void chomp::homology::outputstream::logfile ( const outputstream other)
inline

Turns on logging to the same file as some other stream.

Definition at line 188 of file textfile.h.

189{
190 if (!copied && logstream)
191 delete logstream;
192 logstream = other. logstream;
193 if (logstream)
194 {
195 copied = true;
196 log = true;
197 }
198 return;
199} /* outputstream::logfile */

References copied, log, and logstream.

◆ precision()

int chomp::homology::outputstream::precision ( ) const
inline

Returns the precision of the main output stream.

Definition at line 219 of file textfile.h.

220{
221 return out. precision ();
222} /* outputstream::precision */
int precision() const
Returns the precision of the main output stream.
Definition: textfile.h:219

References out, and precision().

Referenced by precision().

Member Data Documentation

◆ copied

bool chomp::homology::outputstream::copied
protected

This variable is set to true if this log file pointer was copied from another output stream, and therefore it should not be deleted by this stream.

Definition at line 134 of file textfile.h.

Referenced by keepforever(), logfile(), outputstream(), and ~outputstream().

◆ flush

bool chomp::homology::outputstream::flush

If this variable is set to true then both the output stream 'cout' and the log file are flushed after every write operation.

Note that this may slow down the output significantly if the log file is located on a network drive.

Definition at line 94 of file textfile.h.

Referenced by outputstream().

◆ log

bool chomp::homology::outputstream::log

If this variable is set to true then everything that is written to this stream is also written to the log file.

Definition at line 88 of file textfile.h.

Referenced by logfile(), and outputstream().

◆ logstream

std::ofstream* chomp::homology::outputstream::logstream
protected

A pointer to the log file stream.

Definition at line 129 of file textfile.h.

Referenced by getlogstream(), logfile(), outputstream(), and ~outputstream().

◆ out

std::ostream& chomp::homology::outputstream::out

A reference to the main output stream bound with this stream.

Definition at line 101 of file textfile.h.

Referenced by logfile(), and precision().

◆ show

bool chomp::homology::outputstream::show

If this variable is set to true then everything that is written to this stream is also written to 'cout'.

If this variable is set to false then the screen output is suppressed, while logging can still be turned on.

Definition at line 84 of file textfile.h.

Referenced by outputstream().


The documentation for this class was generated from the following file: