IFEM 90A354
LogStream.h
Go to the documentation of this file.
1// $Id$
2//==============================================================================
12//==============================================================================
13
14#ifndef LOG_STREAM_H_
15#define LOG_STREAM_H_
16
17#include <iostream>
18#include <iomanip>
19#include <memory>
20#include <vector>
21
22namespace utl {
23
26{
27public:
30 explicit LogStream(std::ostream* out) : m_out(out) { m_ppid = m_pid = 0; }
35 explicit LogStream(std::ostream& out, int ppid = 0, int mypid = 0);
36
38 void setNull() { m_out = nullptr; }
40 void setStream(std::ostream& out) { m_out = &out; }
42 void setPIDs(int ppid, int mypid) { m_ppid = ppid; m_pid = mypid; }
43
45 void addExtraLog(std::ostream* extra, bool clear = false);
47 void addExtraLog(std::shared_ptr<std::ostream> extra, bool clear = false);
49 void removeExtraLog(std::shared_ptr<std::ostream> extra);
50
52 template<typename T>
53 LogStream& operator<<(const T& data) { return this->write(data); }
54
56 template<typename T>
57 LogStream& write(const T& data)
58 {
59 if (m_ppid == m_pid && m_out)
60 *m_out << data;
61 for (auto extra : m_extra)
62 *extra << data;
63
64 return *this;
65 }
66
68 int precision(int streamsize);
70 int precision() const { return m_out ? m_out->precision() : 6; }
71
73 bool good() const { return m_out ? m_out->good() : true; }
74
76 void flush();
77
79 std::ios_base::fmtflags flags() const { return m_out?m_out->flags():std::ios_base::fmtflags(); }
81 std::ios_base::fmtflags flags(std::ios_base::fmtflags fmtfl);
82
85
87 typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
89 typedef CoutType& (*StandardEndLine)(CoutType&);
90
93
94protected:
95 std::ostream* m_out;
96 std::vector<std::shared_ptr<std::ostream>> m_extra;
97 int m_ppid;
98 int m_pid;
99};
100
101}
102
103#endif
Logging stream class.
Definition LogStream.h:26
int m_pid
This process' PID.
Definition LogStream.h:98
void setStream(std::ostream &out)
Sets the output stream.
Definition LogStream.h:40
CoutType &(* StandardEndLine)(CoutType &)
Function pointer type.
Definition LogStream.h:89
std::ios_base::fmtflags flags() const
Obtain stream flags.
Definition LogStream.h:79
LogStream & write(const T &data)
Write data to stream.
Definition LogStream.h:57
std::vector< std::shared_ptr< std::ostream > > m_extra
Extra output streams.
Definition LogStream.h:96
void removeExtraLog(std::shared_ptr< std::ostream > extra)
Drop an extra logging stream.
Definition LogStream.C:64
bool good() const
Check state of stream.
Definition LogStream.h:73
LogStream & operator=(const LogStream &)
Assignment operator.
Definition LogStream.C:37
LogStream(std::ostream *out)
Default constructor.
Definition LogStream.h:30
void setNull()
Nullifies the output stream.
Definition LogStream.h:38
void addExtraLog(std::ostream *extra, bool clear=false)
Adds an extra logging stream.
Definition LogStream.C:48
void flush()
Flush streams.
Definition LogStream.C:86
std::ostream * m_out
Main output stream.
Definition LogStream.h:95
void setPIDs(int ppid, int mypid)
Sets PIDs for the stream.
Definition LogStream.h:42
std::basic_ostream< char, std::char_traits< char > > CoutType
This is the type of std::cout.
Definition LogStream.h:87
LogStream & operator<<(const T &data)
Write data to stream.
Definition LogStream.h:53
int m_ppid
PID to print on.
Definition LogStream.h:97
int precision() const
Get current precision.
Definition LogStream.h:70
General utility classes and functions.
Definition SIMoptions.h:22