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

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