/
Console.h
273 lines (225 loc) · 8.87 KB
/
Console.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/***************************************************************************
* (c) Jürgen Riegel (juergen.riegel@web.de) 2002 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License (LGPL) *
* as published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* for detail see the LICENCE text file. *
* *
* FreeCAD is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with FreeCAD; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
* USA *
* *
* Juergen Riegel 2002 *
***************************************************************************/
#ifndef BASE_CONSOLE_H
#define BASE_CONSOLE_H
// Std. configurations
#include <Base/PyExport.h>
#include <Base/Stream.h>
//#pragma warning(disable: 4786) // specifier longer then 255 chars
#include <assert.h>
#include <set>
#include <string>
//**************************************************************************
// Loging levels
#ifdef FC_DEBUG
/// switch on the loging of python object creation and destruction
# undef FC_LOGPYOBJECTS
/// switch on the loging of Feature update and execution
# define FC_LOGFEATUREUPDATE
/// switch on the loging of the Update execution through Doc, App, GuiApp and GuiDoc
# undef FC_LOGUPDATECHAIN
#endif
namespace Base {
class ConsoleSingleton;
} // namespace Base
typedef Base::ConsoleSingleton ConsoleMsgType;
typedef unsigned int ConsoleMsgFlags;
namespace Base {
/** The console observer class
* This class distribute the Messages issued to the FCConsole class.
* If you need to catch some of the Messages you need to inherit from
* this class and implement some of the methods.
* @see Console
*/
class BaseExport ConsoleObserver
{
public:
ConsoleObserver()
:bErr(true),bMsg(true),bLog(true),bWrn(true) {}
virtual ~ConsoleObserver() {}
/// get calles when a Warning is issued
virtual void Warning(const char *){}
/// get calles when a Message is issued
virtual void Message(const char *){}
/// get calles when a Error is issued
virtual void Error (const char *)=0;
/// get calles when a Log Message is issued
virtual void Log (const char *){}
virtual const char *Name(void){return 0L;}
bool bErr,bMsg,bLog,bWrn;
};
/** The console class
* This class manage all the stdio stuff. This includes
* Messages, Warnings, Log entries and Errors. The incomming
* Messages are distributed with the FCConsoleObserver. The
* FCConsole class itself makes no IO, it's more like a manager.
* \par
* ConsoleSingleton is a singleton! That means you can access the only
* instance of the class from every where in c++ by simply using:
* \code
* #include <Base/Console.h>
* Base::Console().Log("Stage: %d",i);
* \endcode
* \par
* ConsoleSingleton is abel to switch between several modes to, e.g. switch
* the logging on or off, or treat Warnings as Errors, and so on...
* @see ConsoleObserver
*/
class BaseExport ConsoleSingleton
{
public:
// exported functions goes here +++++++++++++++++++++++++++++++++++++++
/// Prints a Message
virtual void Message ( const char * pMsg, ... ) ;
/// Prints a warning Message
virtual void Warning ( const char * pMsg, ... ) ;
/// Prints a error Message
virtual void Error ( const char * pMsg, ... ) ;
/// Prints a log Message
virtual void Log ( const char * pMsg, ... ) ;
/// Delivers a time/date string
const char* Time(void);
/// Attaches an Observer to FCConsole
void AttachObserver(ConsoleObserver *pcObserver);
/// Detaches an Observer from FCConsole
void DetachObserver(ConsoleObserver *pcObserver);
/// enumaration for the console modes
enum ConsoleMode{
Verbose = 1, // supress Log messages
};
enum FreeCAD_ConsoleMsgType {
MsgType_Txt = 1,
MsgType_Log = 2,
MsgType_Wrn = 4,
MsgType_Err = 8
} ;
/// Change mode
void SetMode(ConsoleMode m);
/// Change mode
void UnsetMode(ConsoleMode m);
/// Enables or disables message types of a cetain console observer
ConsoleMsgFlags SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool b);
/// Enables or disables message types of a cetain console observer
bool IsMsgTypeEnabled(const char* sObs, FreeCAD_ConsoleMsgType type) const;
/// singleton
static ConsoleSingleton &Instance(void);
// retrieval of an observer by name
ConsoleObserver *Get(const char *Name) const;
static PyMethodDef Methods[];
protected:
// python exports goes here +++++++++++++++++++++++++++++++++++++++++++
// static python wrapper of the exported functions
static PyObject *sPyLog (PyObject *self,PyObject *args,PyObject *kwd);
static PyObject *sPyMessage (PyObject *self,PyObject *args,PyObject *kwd);
static PyObject *sPyWarning (PyObject *self,PyObject *args,PyObject *kwd);
static PyObject *sPyError (PyObject *self,PyObject *args,PyObject *kwd);
static PyObject *sPySetStatus(PyObject *self,PyObject *args,PyObject *kwd);
static PyObject *sPyGetStatus(PyObject *self,PyObject *args,PyObject *kwd);
bool _bVerbose;
// Singleton!
ConsoleSingleton(void);
virtual ~ConsoleSingleton();
private:
// singleton
static void Destruct(void);
static ConsoleSingleton *_pcSingleton;
// observer processing
void NotifyMessage(const char *sMsg);
void NotifyWarning(const char *sMsg);
void NotifyError (const char *sMsg);
void NotifyLog (const char *sMsg);
// observer list
std::set<ConsoleObserver * > _aclObservers;
};
/** Access to the Console
* This method is used to gain access to the one and only instance of
* the ConsoleSingleton class.
*/
inline ConsoleSingleton &Console(void){
return ConsoleSingleton::Instance();
}
//=========================================================================
// some special observers
/** The LoggingConsoleObserver class
* This class is used by the main modules to write Console messages and logs to a file
*/
class BaseExport ConsoleObserverFile : public ConsoleObserver
{
public:
ConsoleObserverFile(const char *sFileName);
virtual ~ConsoleObserverFile();
virtual void Warning(const char *sWarn);
virtual void Message(const char *sMsg);
virtual void Error (const char *sErr);
virtual void Log (const char *sLog);
const char* Name(void){return "File";}
protected:
Base::ofstream cFileStream;
};
/** The CmdConsoleObserver class
* This class is used by the main modules to write Console messages and logs the system con.
*/
class BaseExport ConsoleObserverStd: public ConsoleObserver
{
public:
ConsoleObserverStd();
virtual ~ConsoleObserverStd();
virtual void Warning(const char *sWarn);
virtual void Message(const char *sMsg);
virtual void Error (const char *sErr);
virtual void Log (const char *sErr);
const char* Name(void){return "Console";}
};
class BaseExport RedirectStdOutput : public std::streambuf
{
public:
RedirectStdOutput();
protected:
int overflow(int c = EOF);
int sync();
private:
std::string buffer;
};
class BaseExport RedirectStdError : public std::streambuf
{
public:
RedirectStdError();
protected:
int overflow(int c = EOF);
int sync();
private:
std::string buffer;
};
class BaseExport RedirectStdLog : public std::streambuf
{
public:
RedirectStdLog();
protected:
int overflow(int c = EOF);
int sync();
private:
std::string buffer;
};
} // namespace Base
#endif // BASE_CONSOLE_H