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

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