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

Last change on this file since 7949 was 7949, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 6.7 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
39private:
40 enum {
41 kIsUnderlined = BIT(15)//, kIsAutoIntro = BIT(16)
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) : std::ios(), std::streambuf(), ostream((std::streambuf*)&log), TObject()
104 {
105 fOutputLevel = log.fOutputLevel;
106 fDebugLevel = log.fDebugLevel;
107 fDevice = log.fDevice;
108 }
109 ~MLog();
110
111 static TString Intro();
112
113 bool LockUpdate(const char *msg);
114 bool UnLockUpdate(const char *msg);
115
116 bool Lock(const char *msg="");
117 bool UnLock(const char *msg="");
118
119 void EnableDirectGui() { fIsDirectGui = kTRUE; }
120 void DisableDirectGui() { fIsDirectGui = kFALSE; }
121 void UpdateGui();
122
123 void Underline();
124
125 void SetDebugLevel(int i) { fDebugLevel = i; }
126 int GetDebugLevel() const { return fDebugLevel; }
127 void SetOutputLevel(int i) { fOutputLevel = i; }
128 void SetOutputDevice(int i) { fDevice = i; }
129 void EnableOutputDevice(Flags_t f) { fDevice |= f; }
130 void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
131 void operator=(ofstream &sout) { SetOutputFile(sout); }
132 void operator=(TGTextView *sout) { SetOutputGui(sout); }
133 //void SetAutoIntro(Bool_t b=kTRUE) { b ? SetBit(kIsAutoIntro) : SetBit(kIsAutoIntro); }
134
135 Bool_t IsNullOutput() const { return fIsNull; }
136 Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
137
138 void SetOutputGui(TGTextView *sout, int flag=-1)
139 {
140 fGui = sout;
141 CheckFlag(eGui, flag);
142 }
143
144 void SetOutputFile(ofstream &sout, int flag=-1)
145 {
146 //
147 // Set new output file by a given stream. The new output
148 // file is not deleted automatically. If no flag is specified
149 // the state of the file-device stream is unchanged.
150 // if the a flag is specified the state is changed
151 // in correspondance to the flag
152 //
153 DeallocateFile();
154 fOut = &sout;
155 CheckFlag(eFile, flag);
156 }
157
158 void SetOutputFile(const char *f=NULL, int flag=-1)
159 {
160 //
161 // Set new output file by name. The new output file must
162 // not be deleted by the user. 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 ReallocateFile(f);
168 CheckFlag(eFile, flag);
169 }
170
171 ofstream &GetOutputFile()
172 {
173 //
174 // Get the file output stream from MLogging
175 // if no file output stream is existing yet it will be created.
176 // For the creating a C-Temporary file name is used.
177 // if the stream is created here the user must not delete it
178 //
179 // What a pitty, but it seems, that there is now way to ask
180 // an ofstream for the corresponding file name. Elsewhise
181 // I would implement a GetFileName-function, too.
182 //
183 if (!fOut)
184 ReallocateFile(NULL);
185 return *fOut;
186 }
187
188 // FIXME: Switch off colors when on....
189 void SetNullOutput(Bool_t n=kTRUE) { fIsNull = n; }
190
191 void SetNoColors(Bool_t flag=kTRUE) { flag ? SetBit(eNoColors) : ResetBit(eNoColors); }
192
193 void Setup(MArgs &arg);
194 void Usage();
195
196 void ReadEnv(const TEnv &env, TString prefix="", Bool_t print=kFALSE);
197 void WriteEnv(TEnv &env, TString prefix="", Bool_t print=kFALSE) const;
198
199 MLog &Separator(TString str="", int outlvl=0)
200 {
201 if (!str.IsNull())
202 {
203 const Int_t l = (78-str.Length())/2-3;
204 str.Prepend("={ ");
205 str.Prepend('-', l);
206 str.Append(" }=");
207 str.Append('-', l);
208 }
209 if (str.Length()<78)
210 str.Append('-', 78-str.Length());
211
212 const int save = fOutputLevel;
213 SetOutputLevel(outlvl);
214 (*this) << str << std::endl;
215 fOutputLevel = save;
216
217 return *this;
218 }
219
220 void AddPlugin(MLogPlugin *plug);
221
222 ClassDef(MLog, 0) // This is what we call 'The logging system'
223};
224
225//
226// This is the definition of a global output stream, which by
227// default pipes all output to the stdout
228//
229R__EXTERN MLog gLog;
230
231#endif
Note: See TracBrowser for help on using the repository browser.