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

Last change on this file since 2124 was 2123, checked in by tbretz, 22 years ago
*** empty log message ***
File size: 5.6 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#ifndef __CINT__
12#include <iostream.h> // base classes for MLog
13#else
14class streambuf;
15class ostream;
16#endif
17
18#define bsz 160 // two standard lines
19
20class TGTextView;
21
22class MLog : public streambuf, public ostream, public TObject
23{
24public:
25 typedef enum _flags {
26 eStdout = 0x001,
27 eStderr = 0x002,
28 eFile = 0x004,
29 eGui = 0x008,
30 eNoColors = 0x400 //BIT(15)
31 } Flags_t;
32
33 enum ELogBits {
34 kHasChanged = BIT(14) // if gui has changed
35 };
36
37private:
38 enum {
39 kIsUnderlined = BIT(15)
40 };
41 static const char kESC;
42 static const char *const kEsc;
43 static const char *const kReset;
44 static const char *const kRed;
45 static const char *const kGreen;
46 static const char *const kYellow;
47 static const char *const kBlue;
48 static const char *const kUnderline;
49 static const char *const kBlink;
50 static const char *const kBright;
51 static const char *const kDark;
52
53 char fBuffer; //!
54 char fBase[bsz+1]; //! Buffer to store the data in
55 char *fPPtr; //! Pointer to present position in buffer
56 const char *fEPtr; //! Pointer to end of buffer
57
58 UInt_t fOutputLevel; //! Present output level of the stream
59 UInt_t fDebugLevel; //! Present global debug level
60 UInt_t fDevice; //! Flag to indicate the present streams
61
62 Bool_t fIsNull; //! Switch output completely off
63
64 ofstream *fout; //! possible file output stream
65 Bool_t fOutAllocated; //! flag if fout is created by MLogging
66 TGTextView *fgui; //! Text View output
67
68 Bool_t fIsDirectGui; //! Pipe text directly to the GUI (for single threaded environments)
69 TString **fGuiLines; //! Lines to pipe to gui
70 Int_t fNumLines; //!
71 TString fGuiLine; //!
72
73#ifdef _REENTRANT
74 void *fMuxGui; //! Mutex locking access of TGListBox
75#endif
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 &log);
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 << 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.