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

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