source: trunk/MagicSoft/Mars/mbase/MLog.h@ 8091

Last change on this file since 8091 was 8088, checked in by tbretz, 19 years ago
*** empty log message ***
File size: 7.2 KB
Line 
1#ifndef MARS_MLog
2#define MARS_MLog
3
4#ifndef ROOT_TObject
5#include <TObject.h>
6#endif
7#ifndef ROOT_TString
8#include <TString.h>
9#endif
10#ifndef ROOT_TSystem
11#include <TSystem.h>
12#endif
13
14#include <iostream> // base classes for MLog
15
16class MArgs;
17
18class TEnv;
19class TMutex;
20class TGTextView;
21
22class MLogPlugin;
23
24class MLog : public std::streambuf, public std::ostream, public TObject
25{
26public:
27 typedef enum _flags {
28 eStdout = 0x001,
29 eStderr = 0x002,
30 eFile = 0x004,
31 eGui = 0x008,
32 eNoColors = 0x400 //BIT(15)
33 } Flags_t;
34
35 enum ELogBits {
36 kHasChanged = BIT(14) // if gui has changed
37 };
38
39 enum ELogType {
40 kDefault,
41 kColor,
42 kBlackWhite
43 };
44
45private:
46 enum {
47 kIsUnderlined = BIT(15)//, kIsAutoIntro = BIT(16)
48 };
49 static const char kESC;
50 static const char *const kEsc;
51 static const char *const kReset;
52 static const char *const kRed;
53 static const char *const kGreen;
54 static const char *const kYellow;
55 static const char *const kBlue;
56 static const char *const kUnderline;
57 static const char *const kBlink;
58 static const char *const kBright;
59 static const char *const kDark;
60
61 static const int fgBufferSize = 160; // two standard lines
62
63 char fBuffer; //!
64 char fBase[fgBufferSize+1]; //! Buffer to store the data in
65 char *fPPtr; //! Pointer to present position in buffer
66 const char *fEPtr; //! Pointer to end of buffer
67
68 UInt_t fOutputLevel; //! Present output level of the stream
69 UInt_t fDebugLevel; //! Present global debug level
70 UInt_t fDevice; //! Flag to indicate the present streams
71
72 Bool_t fIsNull; //! Switch output completely off
73
74 ofstream *fOut; //! possible file output stream
75 Bool_t fOutAllocated; //! flag if fout is created by MLogging
76 TGTextView *fGui; //! Text View output
77
78 Bool_t fIsDirectGui; //! Pipe text directly to the GUI (for single threaded environments)
79 TString **fGuiLines; //! Lines to pipe to gui
80 Int_t fNumLines; //!
81 TString fGuiLine; //!
82
83#ifdef _REENTRANT
84 TMutex *fMuxGui; //! Mutex locking access of TGListBox
85 TMutex *fMuxStream; //! Mutex locking access to streaming
86#endif
87
88 TList *fPlugins;
89
90 void Init();
91
92 void WriteBuffer();
93 int sync();
94 int overflow(int i); // i=EOF means not a real overflow
95
96 void AllocateFile(const char *f);
97 void DeallocateFile();
98 void ReallocateFile(const char *f);
99 void CheckFlag(Flags_t chk, int flag);
100 void Output(ostream &out, int len);
101 void AddGuiLine(const TString& line);
102
103 // User defined error handling (see TError.h)
104 static bool ErrorHandlerIgnore(Int_t level);
105 static void ErrorHandlerPrint(Int_t level, const char *location, const char *msg);
106 static void ErrorHandlerCol(Int_t level, Bool_t abort, const char *location, const char *msg);
107 static void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg);
108
109public:
110
111 MLog(int i=eStdout);
112 MLog(ofstream &out);
113 MLog(TGTextView &out);
114 MLog(const char *fname, int flag=-1);
115
116 MLog(MLog const& log) : std::ios(), std::streambuf(), ostream((std::streambuf*)&log), TObject()
117 {
118 fOutputLevel = log.fOutputLevel;
119 fDebugLevel = log.fDebugLevel;
120 fDevice = log.fDevice;
121 }
122 ~MLog();
123
124 static TString Intro();
125 static void RedirectErrorHandler(ELogType typ=kColor);
126
127 bool LockUpdate(const char *msg);
128 bool UnLockUpdate(const char *msg);
129
130 bool Lock(const char *msg="");
131 bool UnLock(const char *msg="");
132
133 void EnableDirectGui() { fIsDirectGui = kTRUE; }
134 void DisableDirectGui() { fIsDirectGui = kFALSE; }
135 void UpdateGui();
136
137 void Underline();
138
139 void SetDebugLevel(int i) { fDebugLevel = i; }
140 int GetDebugLevel() const { return fDebugLevel; }
141 void SetOutputLevel(int i) { fOutputLevel = i; }
142 void SetOutputDevice(int i) { fDevice = i; }
143 void EnableOutputDevice(Flags_t f) { fDevice |= f; }
144 void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
145 void operator=(ofstream &sout) { SetOutputFile(sout); }
146 void operator=(TGTextView *sout) { SetOutputGui(sout); }
147 //void SetAutoIntro(Bool_t b=kTRUE) { b ? SetBit(kIsAutoIntro) : SetBit(kIsAutoIntro); }
148
149 Bool_t IsNullOutput() const { return fIsNull; }
150 Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
151
152 void SetOutputGui(TGTextView *sout, int flag=-1)
153 {
154 fGui = sout;
155 CheckFlag(eGui, flag);
156 }
157
158 void SetOutputFile(ofstream &sout, int flag=-1)
159 {
160 //
161 // Set new output file by a given stream. The new output
162 // file is not deleted automatically. If no flag is specified
163 // the state of the file-device stream is unchanged.
164 // if the a flag is specified the state is changed
165 // in correspondance to the flag
166 //
167 DeallocateFile();
168 fOut = &sout;
169 CheckFlag(eFile, flag);
170 }
171
172 void SetOutputFile(const char *f=NULL, int flag=-1)
173 {
174 //
175 // Set new output file by name. The new output file must
176 // not be deleted by the user. If no flag is specified
177 // the state of the file-device stream is unchanged.
178 // if the a flag is specified the state is changed
179 // in correspondance to the flag
180 //
181 ReallocateFile(f);
182 CheckFlag(eFile, flag);
183 }
184
185 ofstream &GetOutputFile()
186 {
187 //
188 // Get the file output stream from MLogging
189 // if no file output stream is existing yet it will be created.
190 // For the creating a C-Temporary file name is used.
191 // if the stream is created here the user must not delete it
192 //
193 // What a pitty, but it seems, that there is now way to ask
194 // an ofstream for the corresponding file name. Elsewhise
195 // I would implement a GetFileName-function, too.
196 //
197 if (!fOut)
198 ReallocateFile(NULL);
199 return *fOut;
200 }
201
202 // FIXME: Switch off colors when on....
203 void SetNullOutput(Bool_t n=kTRUE) { fIsNull = n; }
204
205 void SetNoColors(Bool_t flag=kTRUE) { flag ? SetBit(eNoColors) : ResetBit(eNoColors); }
206
207 void Setup(MArgs &arg);
208 void Usage();
209
210 void ReadEnv(const TEnv &env, TString prefix="", Bool_t print=kFALSE);
211 void WriteEnv(TEnv &env, TString prefix="", Bool_t print=kFALSE) const;
212
213 MLog &Separator(TString str="", int outlvl=0)
214 {
215 if (!str.IsNull())
216 {
217 const Int_t l = (78-str.Length())/2-3;
218 str.Prepend("={ ");
219 str.Prepend('-', l);
220 str.Append(" }=");
221 str.Append('-', l);
222 }
223 if (str.Length()<78)
224 str.Append('-', 78-str.Length());
225
226 const int save = fOutputLevel;
227 SetOutputLevel(outlvl);
228 (*this) << str << std::endl;
229 fOutputLevel = save;
230
231 return *this;
232 }
233
234 void AddPlugin(MLogPlugin *plug);
235
236 ClassDef(MLog, 0) // This is what we call 'The logging system'
237};
238
239//
240// This is the definition of a global output stream, which by
241// default pipes all output to the stdout
242//
243R__EXTERN MLog gLog;
244
245#endif
Note: See TracBrowser for help on using the repository browser.