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