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