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

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