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

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