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