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

Last change on this file since 4450 was 4253, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 6.2 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 TMutex *fMuxStream; //! Mutex locking access to streaming
74#endif
75
76 TList *fPlugins;
77
78 void Init();
79
80 void WriteBuffer();
81 int sync();
82 int overflow(int i); // i=EOF means not a real overflow
83
84 void AllocateFile(const char *f);
85 void DeallocateFile();
86 void ReallocateFile(const char *f);
87 void CheckFlag(Flags_t chk, int flag);
88 void Output(ostream &out, int len);
89 void AddGuiLine(const TString& line);
90
91public:
92 MLog(int i=eStdout);
93 MLog(ofstream &out);
94 MLog(TGTextView &out);
95 MLog(const char *fname, int flag=-1);
96
97 MLog(MLog const& log) : ostream((std::streambuf*)&log)
98 {
99 fOutputLevel = log.fOutputLevel;
100 fDebugLevel = log.fDebugLevel;
101 fDevice = log.fDevice;
102 }
103 ~MLog();
104
105 bool LockUpdate(const char *msg);
106 bool UnLockUpdate(const char *msg);
107
108 bool Lock(const char *msg="");
109 bool UnLock(const char *msg="");
110
111 void EnableDirectGui() { fIsDirectGui = kTRUE; }
112 void DisableDirectGui() { fIsDirectGui = kFALSE; }
113 void UpdateGui();
114
115 void Underline();
116
117 void SetDebugLevel(int i) { fDebugLevel = i; }
118 int GetDebugLevel() const { return fDebugLevel; }
119 void SetOutputLevel(int i) { fOutputLevel = i; }
120 void SetOutputDevice(int i) { fDevice = i; }
121 void EnableOutputDevice(Flags_t f) { fDevice |= f; }
122 void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
123 void operator=(ofstream &out) { SetOutputFile(out); }
124 void operator=(TGTextView *out) { SetOutputGui(out); }
125
126 Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
127
128 void SetOutputGui(TGTextView *out, int flag=-1)
129 {
130 fGui = out;
131 CheckFlag(eGui, flag);
132 }
133
134 void SetOutputFile(ofstream &out, int flag=-1)
135 {
136 //
137 // Set new output file by a given stream. The new output
138 // file is not deleted automatically. If no flag is specified
139 // the state of the file-device stream is unchanged.
140 // if the a flag is specified the state is changed
141 // in correspondance to the flag
142 //
143 DeallocateFile();
144 fOut = &out;
145 CheckFlag(eFile, flag);
146 }
147
148 void SetOutputFile(const char *f=NULL, int flag=-1)
149 {
150 //
151 // Set new output file by name. The new output file must
152 // not be deleted by the user. If no flag is specified
153 // the state of the file-device stream is unchanged.
154 // if the a flag is specified the state is changed
155 // in correspondance to the flag
156 //
157 ReallocateFile(f);
158 CheckFlag(eFile, flag);
159 }
160
161 ofstream &GetOutputFile()
162 {
163 //
164 // Get the file output stream from MLogging
165 // if no file output stream is existing yet it will be created.
166 // For the creating a C-Temporary file name is used.
167 // if the stream is created here the user must not delete it
168 //
169 // What a pitty, but it seems, that there is now way to ask
170 // an ofstream for the corresponding file name. Elsewhise
171 // I would implement a GetFileName-function, too.
172 //
173 if (!fOut)
174 ReallocateFile(NULL);
175 return *fOut;
176 }
177
178 // FIXME: Switch off colors when on....
179 void SetNullOutput(Bool_t n=kTRUE) { fIsNull = n; }
180
181 void SetNoColors(Bool_t flag=kTRUE) { flag ? SetBit(eNoColors) : ResetBit(eNoColors); }
182
183 void Separator(TString str="", int outlvl=0)
184 {
185 if (!str.IsNull())
186 {
187 const Int_t l = (78-str.Length())/2-3;
188 str.Prepend("={ ");
189 str.Prepend('-', l);
190 str.Append(" }=");
191 str.Append('-', l);
192 }
193 if (str.Length()<78)
194 str.Append('-', 78-str.Length());
195
196 const int save = fOutputLevel;
197 SetOutputLevel(outlvl);
198 (*this) << str << std::endl;
199 fOutputLevel = save;
200 }
201
202 void AddPlugin(MLogPlugin *plug);
203
204 ClassDef(MLog, 0) // This is what we call 'The logging system'
205};
206
207//
208// This is the definition of a global output stream, which by
209// default pipes all output to the stdout
210//
211R__EXTERN MLog gLog;
212
213#endif
Note: See TracBrowser for help on using the repository browser.