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

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