Debian Codegraph @

src/Base/ConsoleObserver.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2002 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26 #ifndef BASE_CONSOLEOBSERVER_H
27 #define BASE_CONSOLEOBSERVER_H
28
29 #include <Base/Console.h>
30 #include <Base/Stream.h>
31
32
33 namespace Base
34 {
35
36 //=========================================================================
37 // some special observers
38
39 /** The LoggingConsoleObserver class
40 * This class is used by the main modules to write Console messages and logs to a file
41 */
42 class BaseExport ConsoleObserverFile: public ILogger
43 {
44 public:
45 explicit ConsoleObserverFile(const char* sFileName);
46 ~ConsoleObserverFile() override;
47
48 void sendLog(
49 const std::string& notifiername,
50 const std::string& msg,
51 LogStyle level,
52 IntendedRecipient recipient,
53 ContentType content
54 ) override;
55 const char* name() override
56 {
57 return "File";
58 }
59
60 ConsoleObserverFile(const ConsoleObserverFile&) = delete;
61 ConsoleObserverFile(ConsoleObserverFile&&) = delete;
62 ConsoleObserverFile& operator=(const ConsoleObserverFile&) = delete;
63 ConsoleObserverFile& operator=(ConsoleObserverFile&&) = delete;
64
65 private:
66 Base::ofstream cFileStream;
67 };
68
69 /** The CmdConsoleObserver class
70 * This class is used by the main modules to write Console messages and logs the system con.
71 */
72 class BaseExport ConsoleObserverStd: public ILogger
73 {
74 public:
75 ConsoleObserverStd();
76 ~ConsoleObserverStd() override;
77 void sendLog(
78 const std::string& notifiername,
79 const std::string& msg,
80 LogStyle level,
81 IntendedRecipient recipient,
82 ContentType content
83 ) override;
84 const char* name() override
85 {
86 return "Console";
87 }
88
89 ConsoleObserverStd(const ConsoleObserverStd&) = delete;
90 ConsoleObserverStd(ConsoleObserverStd&&) = delete;
91 ConsoleObserverStd& operator=(const ConsoleObserverStd&) = delete;
92 ConsoleObserverStd& operator=(ConsoleObserverStd&&) = delete;
93
94 private:
95 bool useColorStderr;
96 void Warning(const char* sWarn);
97 void Message(const char* sMsg);
98 void Error(const char* sErr);
99 void Log(const char* sLog);
100 void Critical(const char* sCritical);
101 };
102
103 /** The ILoggerBlocker class
104 * This class allows one to temporary block then automatically restore arbitrary message types
105 * on a particular console observer.
106 */
107 class BaseExport ILoggerBlocker
108 {
109 public:
110 // Constructor that will block message types passed as parameter. By default, all types are
111 // blocked.
112 inline explicit ILoggerBlocker(
113 const char* co,
114 ConsoleMsgFlags msgTypes = ConsoleSingleton::MsgType_Txt | ConsoleSingleton::MsgType_Log
115 | ConsoleSingleton::MsgType_Wrn | ConsoleSingleton::MsgType_Err
116 | ConsoleSingleton::MsgType_Critical | ConsoleSingleton::MsgType_Notification
117 );
118 // Disable copy & move constructors
119 ILoggerBlocker(ILoggerBlocker const&) = delete;
120 ILoggerBlocker(ILoggerBlocker const&&) = delete;
121 // Disable assignment & move-assignment operator
122 ILoggerBlocker& operator=(ILoggerBlocker const&) = delete;
123 ILoggerBlocker& operator=(ILoggerBlocker const&&) = delete;
124 // Destructor that will restore message type settings.
125 inline ~ILoggerBlocker();
126
127 private:
128 ConsoleMsgFlags msgTypesBlocked = 0; // Stores message types blocked by the blocker
129 const char* conObs; // Stores console observer name that blocker acts on
130 };
131
132 ILoggerBlocker::ILoggerBlocker(const char* co, ConsoleMsgFlags msgTypes)
133 : conObs(co)
134 {
135 msgTypesBlocked = Console().setEnabledMsgType(conObs, msgTypes, false);
136 }
137
138 ILoggerBlocker::~ILoggerBlocker()
139 {
140 try {
141 #ifdef FC_DEBUG
142 auto debug = Console().setEnabledMsgType(conObs, msgTypesBlocked, true);
143 if (debug != msgTypesBlocked) {
144 Console().warning("Enabled message types have been changed while ILoggerBlocker was set\n");
145 }
146 #else
147 Console().setEnabledMsgType(conObs, msgTypesBlocked, true);
148 #endif
149 }
150 catch (...) {
151 }
152 }
153
154 class BaseExport RedirectStdOutput: public std::streambuf
155 {
156 public:
157 RedirectStdOutput();
158
159 protected:
160 int overflow(int ch = EOF) override;
161 int sync() override;
162
163 private:
164 std::string buffer;
165 };
166
167 class BaseExport RedirectStdError: public std::streambuf
168 {
169 public:
170 RedirectStdError();
171
172 protected:
173 int overflow(int ch = EOF) override;
174 int sync() override;
175
176 private:
177 std::string buffer;
178 };
179
180 class BaseExport RedirectStdLog: public std::streambuf
181 {
182 public:
183 RedirectStdLog();
184
185 protected:
186 int overflow(int ch = EOF) override;
187 int sync() override;
188
189 private:
190 std::string buffer;
191 };
192
193
194 } // namespace Base
195
196 #endif // BASE_CONSOLEOBSERVER_H