Belofte version 2.2.0
A promising chess program using the UCI or Winboard interface
outputwriter.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------+
2 * File: outputwriter.h
3 * Project: part of belofte - A Promising Chess Program
4 * Author: yves
5 * SPDX-License-Identifier: GPL-2.0-only
6+----------------------------------------------------------------------*/
7
8#if !defined(OUTPUTWRITER_H)
9#define OUTPUTWRITER_H
10
11/**
12 * Outputwriter class will log to debug file, stdout and log file
13 * in this order if they have been initialized before
14 */
15class outputWriter : public std::ostream {
16public:
17 explicit outputWriter(std::ostream& os)
18 : m_filename{}
19 , m_os{os}
20 , m_fos{}
21 {}
22
23 ~outputWriter() override
24 { detach(); }
25
26 // explicitly deny access to these functions
27 outputWriter(outputWriter const&) = delete;
31
32 inline void setLevel(int const l)
33 { m_level = l; }
34 inline int getLevel() const
35 { return m_level; }
36
37 bool attach(std::string const& fn)
38 { m_fos.open(fn.c_str(), std::ios::out | std::ios::app );
39 if (m_fos.is_open()) {
40 m_attached = true;
41 m_filename = fn;
42 }
43 return m_fos.is_open();
44 }
45 void detach()
46 { if (isAttached()) {
47 m_attached = false;
48 m_fos.close();
49 m_filename = "";
50 }
51 }
52
53 inline bool isAttached() const
54 { return m_attached; }
55 inline std::string const& fn() const
56 { return m_filename; }
57
59 { m_curlinelevel = l;
60 return *this;
61 }
62
64 { if (m_curlinelevel <= getLevel()) {
65 m_os << "\n";
66 m_os.flush();
67 if (isAttached()) {
68 m_fos << "\n";
69 m_fos.flush();
70 }
71 }
72 if (m_curlinelevel) m_curlinelevel = 0;
73 return *this;
74 }
75
76 std::ostream& getOs()
77 { return m_os; }
78 std::ostream& getFos()
79 { return m_fos; }
80
81private:
82 template<typename T> friend outputWriter& operator<<(outputWriter& ow, T o)
83 {
84 if (ow.m_curlinelevel <= ow.getLevel()) {
85 ow.getOs() << o;
86 if (ow.isAttached()) ow.getFos() << o;
87 }
88 return ow;
89 }
90
91 int m_curlinelevel = 0;
92 int m_level = 0;
93 bool m_attached = false;
94 std::string m_filename;
95
96 std::ostream& m_os;
97 std::ofstream m_fos;
98};
99
100#endif // defined OUTPUTWRITER_H
101
102// eof
outputWriter & operator=(outputWriter const &)=delete
outputWriter & operator()(int const l)
~outputWriter() override
std::ostream & getOs()
outputWriter & operator=(outputWriter &&)=delete
int getLevel() const
bool isAttached() const
outputWriter(std::ostream &os)
std::string const & fn() const
outputWriter(outputWriter const &)=delete
outputWriter(outputWriter &&)=delete
bool attach(std::string const &fn)
outputWriter & endl()
std::ostream & getFos()
friend outputWriter & operator<<(outputWriter &ow, T o)
void setLevel(int const l)