1 | /* ======================================================================== *\
|
---|
2 | !
|
---|
3 | ! *
|
---|
4 | ! * This file is part of MARS, the MAGIC Analysis and Reconstruction
|
---|
5 | ! * Software. It is distributed to you in the hope that it can be a useful
|
---|
6 | ! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
|
---|
7 | ! * It is distributed WITHOUT ANY WARRANTY.
|
---|
8 | ! *
|
---|
9 | ! * Permission to use, copy, modify and distribute this software and its
|
---|
10 | ! * documentation for any purpose is hereby granted without fee,
|
---|
11 | ! * provided that the above copyright notice appear in all copies and
|
---|
12 | ! * that both that copyright notice and this permission notice appear
|
---|
13 | ! * in supporting documentation. It is provided "as is" without express
|
---|
14 | ! * or implied warranty.
|
---|
15 | ! *
|
---|
16 | !
|
---|
17 | !
|
---|
18 | ! Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
|
---|
19 | !
|
---|
20 | ! Copyright: MAGIC Software Development, 2000-2004
|
---|
21 | !
|
---|
22 | !
|
---|
23 | \* ======================================================================== */
|
---|
24 |
|
---|
25 |
|
---|
26 | //////////////////////////////////////////////////////////////////////////////
|
---|
27 | //
|
---|
28 | // MLog
|
---|
29 | //
|
---|
30 | // This is what we call the logging-system.
|
---|
31 | //
|
---|
32 | // It is derived from the C++ streaming classes and can handle our
|
---|
33 | // logging. The log output can be redirected to stdout, stderr, any other
|
---|
34 | // stream or a root window.
|
---|
35 | //
|
---|
36 | // There is a global log-instance which you can use like cout, id is gLog.
|
---|
37 | // A log-instance of your choice (gLog by default) is destributed to all
|
---|
38 | // Task which are used in an eventloop, so that you can redirect the output
|
---|
39 | // of one eventloop to where you want..
|
---|
40 | //
|
---|
41 | // The MLog stream has the advantage, that it can be used like the common
|
---|
42 | // C++ streams (for example cout). It can redirect the stream to different
|
---|
43 | // outputs (console, file, GUI) if necessary at the same time.
|
---|
44 | //
|
---|
45 | // It supports different debug levels. The debug level of the current
|
---|
46 | // stream contents is set by SetDebugLevel, the output level of the
|
---|
47 | // current stream can be set by SetOutputLevel.
|
---|
48 | //
|
---|
49 | // The header file MLogManip.h contains so called manipulators (like flush
|
---|
50 | // or setw from iomanip.h) which can manipulate these levels from within
|
---|
51 | // stream, for example:
|
---|
52 | // gLog << debug(3) << "Hallo World " << endl;
|
---|
53 | // sets the debug level of the following stream to 3
|
---|
54 | //
|
---|
55 | // edev(), ddev() can be used to enable/disable an output device from
|
---|
56 | // within the stream. The enumerations are defined in MLog::_flags
|
---|
57 | //
|
---|
58 | // Commonly used abbreviations are also defined:
|
---|
59 | // dbginf Prints source file name and line number. Used for output
|
---|
60 | // which people may like to look up in the code
|
---|
61 | // all Is streamed to the output in any case. Used for outputs
|
---|
62 | // which are requested by the user (eg TObject::Print)
|
---|
63 | // err Should be used for fatal errors which stops the current
|
---|
64 | // processing, eg:
|
---|
65 | // gLog << err << "ERROR: TObject::Copy - Stopped" << endl;
|
---|
66 | // warn Warning means an error occured, but it is not clear whether
|
---|
67 | // this results further procesing or not.
|
---|
68 | // inf Informs the user about what's going on. Mostly usefull for
|
---|
69 | // debugging, but in general not necessary at all.
|
---|
70 | // dbg Use this for your private purpose to mark something as debug
|
---|
71 | // output. This is _not_ ment to be persistent!
|
---|
72 | //
|
---|
73 | // If your console is capable of ANSI colors the stream is displayed
|
---|
74 | // in several colors:
|
---|
75 | // all: default
|
---|
76 | // err: red
|
---|
77 | // warn: yellow/brown
|
---|
78 | // inf: green
|
---|
79 | // dbg: blue (and all other levels)
|
---|
80 | //
|
---|
81 | // If you have a dark background on your console you might want to set
|
---|
82 | // an environment variable, eg:
|
---|
83 | // export MARSDEFINES=-DHAVE_DARKBACKGROUND
|
---|
84 | // and recompile MLog.
|
---|
85 | //
|
---|
86 | // If your console can display it also 'underline' can be used. This
|
---|
87 | // underlines a text till the next 'endl', eg:
|
---|
88 | // gLog << underline << "This is important!" << endl;
|
---|
89 | //
|
---|
90 | // To switch off ANSI support call: SetNoColors()
|
---|
91 | //
|
---|
92 | // gLog is a global stream defined like cout or cerr
|
---|
93 | //
|
---|
94 | //////////////////////////////////////////////////////////////////////////////
|
---|
95 | #include "MLog.h"
|
---|
96 |
|
---|
97 | #include <stdlib.h> // mkstemp
|
---|
98 |
|
---|
99 | #include <fstream>
|
---|
100 | #include <iomanip>
|
---|
101 |
|
---|
102 | #include <TROOT.h> // gROOT->GetListOfCleanups()
|
---|
103 |
|
---|
104 | #ifdef _REENTRANT
|
---|
105 | #include <TMutex.h>
|
---|
106 | #endif
|
---|
107 | #include <TGTextView.h>
|
---|
108 |
|
---|
109 | #include "MLogPlugin.h"
|
---|
110 |
|
---|
111 | ClassImp(MLog);
|
---|
112 |
|
---|
113 | using namespace std;
|
---|
114 |
|
---|
115 | // root 3.02:
|
---|
116 | // check for TObjectWarning, TObject::Info, gErrorIgnoreLevel
|
---|
117 |
|
---|
118 | const char MLog::kESC = '\033'; // (char)27
|
---|
119 | const char *const MLog::kEsc = "\033[";
|
---|
120 | const char *const MLog::kReset = "\033[0m";
|
---|
121 | const char *const MLog::kRed = "\033[31m";
|
---|
122 | const char *const MLog::kGreen = "\033[32m";
|
---|
123 | #ifdef HAVE_DARKBACKGROUND
|
---|
124 | const char *const MLog::kYellow = "\033[33m\033[1m";
|
---|
125 | #else
|
---|
126 | const char *const MLog::kYellow = "\033[33m";
|
---|
127 | #endif
|
---|
128 | const char *const MLog::kBlue = "\033[34m";
|
---|
129 | const char *const MLog::kUnderline = "\033[4m";;
|
---|
130 | const char *const MLog::kBlink = "\033[5m";;
|
---|
131 | const char *const MLog::kBright = "\033[1m";;
|
---|
132 | const char *const MLog::kDark = "\033[2m";;
|
---|
133 |
|
---|
134 | //
|
---|
135 | // This is the definition of the global log facility
|
---|
136 | //
|
---|
137 | MLog gLog;
|
---|
138 |
|
---|
139 | // --------------------------------------------------------------------------
|
---|
140 | //
|
---|
141 | // this strange usage of an unbufferd buffer is a workaround
|
---|
142 | // to make it work on Alpha and Linux!
|
---|
143 | //
|
---|
144 | void MLog::Init()
|
---|
145 | {
|
---|
146 |
|
---|
147 | //
|
---|
148 | // Creat drawing semaphore
|
---|
149 | //
|
---|
150 | #ifdef _REENTRANT
|
---|
151 | fMuxGui = new TMutex;
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | fPlugins = new TList;
|
---|
155 | gROOT->GetListOfCleanups()->Add(fPlugins);
|
---|
156 |
|
---|
157 | setp(&fBuffer, &fBuffer+1);
|
---|
158 | *this << '\0';
|
---|
159 | }
|
---|
160 |
|
---|
161 | // --------------------------------------------------------------------------
|
---|
162 | //
|
---|
163 | // default constructor which initializes the streamer and sets the device
|
---|
164 | // which is used for the output (i)
|
---|
165 | //
|
---|
166 | MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), fIsNull(kFALSE), fOut(NULL), fOutAllocated(kFALSE), fGui(NULL), fNumLines(0)
|
---|
167 | {
|
---|
168 | Init();
|
---|
169 | }
|
---|
170 |
|
---|
171 | // --------------------------------------------------------------------------
|
---|
172 | //
|
---|
173 | // default constructor which initializes the streamer and sets the given
|
---|
174 | // ofstream as the default output device
|
---|
175 | //
|
---|
176 | MLog::MLog(ofstream &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fOut(&out), fOutAllocated(kFALSE), fGui(NULL), fNumLines(0)
|
---|
177 | {
|
---|
178 | Init();
|
---|
179 | }
|
---|
180 |
|
---|
181 | // --------------------------------------------------------------------------
|
---|
182 | //
|
---|
183 | // default constructor which initializes the streamer and sets the given
|
---|
184 | // TGTextView as the default output device
|
---|
185 | //
|
---|
186 | MLog::MLog(TGTextView &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), fOut(NULL), fOutAllocated(kFALSE), fGui(&out), fNumLines(0)
|
---|
187 | {
|
---|
188 | Init();
|
---|
189 | }
|
---|
190 |
|
---|
191 | // --------------------------------------------------------------------------
|
---|
192 | //
|
---|
193 | // default constructor which initializes the streamer and opens a file with
|
---|
194 | // the given name. Dependend on the flag the file is set as output device
|
---|
195 | // or not.
|
---|
196 | //
|
---|
197 | MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fGui(NULL), fNumLines(0)
|
---|
198 | {
|
---|
199 | Init();
|
---|
200 |
|
---|
201 | AllocateFile(fname);
|
---|
202 | CheckFlag(eFile, flag);
|
---|
203 | }
|
---|
204 |
|
---|
205 | // --------------------------------------------------------------------------
|
---|
206 | //
|
---|
207 | // Destructor, destroying the gui mutex.
|
---|
208 | //
|
---|
209 | MLog::~MLog()
|
---|
210 | {
|
---|
211 | DeallocateFile();
|
---|
212 |
|
---|
213 | delete fPlugins;
|
---|
214 | #ifdef _REENTRANT
|
---|
215 | delete fMuxGui;
|
---|
216 | #endif
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // copyt constructor
|
---|
222 | //
|
---|
223 | /*
|
---|
224 | MLog::MLog(MLog const& log)
|
---|
225 | {
|
---|
226 | // fOutputLevel = log.fOutputLevel;
|
---|
227 | // fDebugLevel = log.fDebugLevel;
|
---|
228 | // fDevice = log.fDevice;
|
---|
229 | }
|
---|
230 | */
|
---|
231 |
|
---|
232 | void MLog::Underline()
|
---|
233 | {
|
---|
234 | SetBit(kIsUnderlined);
|
---|
235 |
|
---|
236 | fPlugins->ForEach(MLogPlugin, Underline)();
|
---|
237 |
|
---|
238 | if (TestBit(eNoColors))
|
---|
239 | return;
|
---|
240 |
|
---|
241 | if (fDevice&eStdout)
|
---|
242 | cout << kUnderline;
|
---|
243 |
|
---|
244 | if (fDevice&eStderr)
|
---|
245 | cerr << kUnderline;
|
---|
246 | }
|
---|
247 |
|
---|
248 | void MLog::Output(ostream &out, int len)
|
---|
249 | {
|
---|
250 | if (!TestBit(eNoColors))
|
---|
251 | switch (fOutputLevel)
|
---|
252 | {
|
---|
253 | // do not output reset. Otherwise we reset underline in 0-mode
|
---|
254 | // case 1: out << MLog::kReset; break; // all
|
---|
255 | case 0: break; // all = background color
|
---|
256 | case 1: out << MLog::kRed; break; // err
|
---|
257 | case 2: out << MLog::kYellow; break; // warn
|
---|
258 | case 3: out << MLog::kGreen; break; // inf
|
---|
259 | default: out << MLog::kBlue; break; // all others (dbg)
|
---|
260 | }
|
---|
261 |
|
---|
262 | if (len>0)
|
---|
263 | {
|
---|
264 | // Check for EOL
|
---|
265 | const Int_t endline = fBase[len-1]=='\n' ? 1 : 0;
|
---|
266 | // output text to screen (without trailing '\n')
|
---|
267 | out << TString(fBase, len-endline);
|
---|
268 | // reset colors if working with colors
|
---|
269 | if (!TestBit(eNoColors))
|
---|
270 | out << kReset;
|
---|
271 | // output EOL of check found EOL
|
---|
272 | if (endline)
|
---|
273 | {
|
---|
274 | out << '\n';
|
---|
275 | // Check whether text was underlined
|
---|
276 | if (TestBit(kIsUnderlined) && TestBit(eNoColors))
|
---|
277 | {
|
---|
278 | out << setw(len-1) << setfill('-') << "" << "\n";
|
---|
279 | ResetBit(kIsUnderlined);
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | out.flush();
|
---|
284 | }
|
---|
285 |
|
---|
286 | void MLog::AddGuiLine(const TString &line)
|
---|
287 | {
|
---|
288 | // add a new TString* to the array of gui lines
|
---|
289 | TString **newstr = new TString*[fNumLines+1];
|
---|
290 | memcpy(newstr, fGuiLines, fNumLines*sizeof(TString*));
|
---|
291 | if (fNumLines>0)
|
---|
292 | delete fGuiLines;
|
---|
293 | fGuiLines = newstr;
|
---|
294 |
|
---|
295 | // add Gui line as last line of array
|
---|
296 | fGuiLines[fNumLines++] = new TString(line);
|
---|
297 | }
|
---|
298 |
|
---|
299 | // --------------------------------------------------------------------------
|
---|
300 | //
|
---|
301 | // This is the function which writes the stream physically to a device.
|
---|
302 | // If you want to add a new device this must be done here.
|
---|
303 | //
|
---|
304 | void MLog::WriteBuffer()
|
---|
305 | {
|
---|
306 | //
|
---|
307 | // restart writing to the buffer at its first char
|
---|
308 | //
|
---|
309 | const int len = fPPtr - fBase;
|
---|
310 |
|
---|
311 | fPPtr = fBase;
|
---|
312 |
|
---|
313 | if (fIsNull)
|
---|
314 | return;
|
---|
315 |
|
---|
316 | if (fDevice&eStdout)
|
---|
317 | Output(cout, len);
|
---|
318 |
|
---|
319 | if (fDevice&eStderr)
|
---|
320 | Output(cerr, len);
|
---|
321 |
|
---|
322 | if (fDevice&eFile && fOut)
|
---|
323 | fOut->write(fBase, len);
|
---|
324 |
|
---|
325 | fPlugins->ForEach(MLogPlugin, SetColor)(fOutputLevel);
|
---|
326 | fPlugins->ForEach(MLogPlugin, WriteBuffer)(fBase, len);
|
---|
327 |
|
---|
328 | if (fDevice&eGui && fGui)
|
---|
329 | {
|
---|
330 | // check whether the current text was flushed or endl'ed
|
---|
331 | const Int_t endline = fBase[len-1]=='\n' ? 1 : 0;
|
---|
332 |
|
---|
333 | // for the gui remove trailing characters ('\n' or '\0')
|
---|
334 | fBase[len-endline]='\0';
|
---|
335 |
|
---|
336 | // add new text to line storage
|
---|
337 | fGuiLine += fBase;
|
---|
338 |
|
---|
339 | if (endline)
|
---|
340 | {
|
---|
341 | AddGuiLine(fGuiLine);
|
---|
342 | fGuiLine = "";
|
---|
343 |
|
---|
344 | // Check whether text should be underlined
|
---|
345 | if (endline && TestBit(kIsUnderlined))
|
---|
346 | {
|
---|
347 | AddGuiLine("");
|
---|
348 | fGuiLines[fNumLines-1]->Append('-', fGuiLines[fNumLines-2]->Length());
|
---|
349 | ResetBit(kIsUnderlined);
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | void MLog::UpdateGui()
|
---|
356 | {
|
---|
357 | if (fNumLines==0)
|
---|
358 | return;
|
---|
359 |
|
---|
360 | // lock mutex
|
---|
361 | Lock();
|
---|
362 |
|
---|
363 | TGText &txt=*fGui->GetText();
|
---|
364 |
|
---|
365 | // copy lines to TGListBox
|
---|
366 | for (int i=0; i<fNumLines; i++)
|
---|
367 | {
|
---|
368 | // Replace all tabs by 7 white spaces
|
---|
369 | fGuiLines[i]->ReplaceAll("\t", " ");
|
---|
370 | txt.InsText(TGLongPosition(0, txt.RowCount()), *fGuiLines[i]);
|
---|
371 | delete fGuiLines[i];
|
---|
372 | }
|
---|
373 | delete fGuiLines;
|
---|
374 |
|
---|
375 | fNumLines=0;
|
---|
376 |
|
---|
377 | // cut text box top 1000 lines
|
---|
378 | // while (txt.RowCount()>1000)
|
---|
379 | // txt.DelLine(1);
|
---|
380 |
|
---|
381 | // show last entry
|
---|
382 | fGui->Layout();
|
---|
383 | fGui->SetVsbPosition(txt.RowCount()-1);
|
---|
384 |
|
---|
385 | // tell a main loop, that list box contents have changed
|
---|
386 | fGui->SetBit(kHasChanged);
|
---|
387 |
|
---|
388 | // release mutex
|
---|
389 | UnLock();
|
---|
390 | }
|
---|
391 |
|
---|
392 | void MLog::Lock()
|
---|
393 | {
|
---|
394 | #ifdef _REENTRANT
|
---|
395 | fMuxGui->Lock();
|
---|
396 | #endif
|
---|
397 | }
|
---|
398 |
|
---|
399 | void MLog::UnLock()
|
---|
400 | {
|
---|
401 | #ifdef _REENTRANT
|
---|
402 | fMuxGui->UnLock();
|
---|
403 | #endif
|
---|
404 | }
|
---|
405 |
|
---|
406 | // --------------------------------------------------------------------------
|
---|
407 | //
|
---|
408 | // This is called to flush the buffer of the streaming devices
|
---|
409 | //
|
---|
410 | int MLog::sync()
|
---|
411 | {
|
---|
412 | Lock();
|
---|
413 | WriteBuffer();
|
---|
414 | UnLock();
|
---|
415 |
|
---|
416 | if (fDevice&eStdout)
|
---|
417 | {
|
---|
418 | if (!TestBit(eNoColors))
|
---|
419 | cout << kReset;
|
---|
420 | cout.flush();
|
---|
421 | }
|
---|
422 |
|
---|
423 | if (fDevice&eStderr)
|
---|
424 | cerr.flush();
|
---|
425 |
|
---|
426 | if (fDevice&eFile && fOut)
|
---|
427 | fOut->flush();
|
---|
428 |
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 | // --------------------------------------------------------------------------
|
---|
433 | //
|
---|
434 | // This function comes from streambuf and should
|
---|
435 | // output the buffer to the device (flush, endl)
|
---|
436 | // or handle a buffer overflow (too many chars)
|
---|
437 | // If a real overflow happens i contains the next
|
---|
438 | // chars which doesn't fit into the buffer anymore.
|
---|
439 | // If the buffer is not really filled i is EOF(-1).
|
---|
440 | //
|
---|
441 | int MLog::overflow(int i) // i=EOF means not a real overflow
|
---|
442 | {
|
---|
443 | //
|
---|
444 | // no output if
|
---|
445 | //
|
---|
446 | if (fOutputLevel <= fDebugLevel)
|
---|
447 | {
|
---|
448 | Lock();
|
---|
449 |
|
---|
450 | *fPPtr++ = (char)i;
|
---|
451 |
|
---|
452 | if (fPPtr == fEPtr)
|
---|
453 | WriteBuffer();
|
---|
454 |
|
---|
455 | UnLock();
|
---|
456 | }
|
---|
457 |
|
---|
458 | return 0;
|
---|
459 | }
|
---|
460 |
|
---|
461 | // --------------------------------------------------------------------------
|
---|
462 | //
|
---|
463 | // Create a new instance of an file output stream
|
---|
464 | // an set the corresponding flag
|
---|
465 | //
|
---|
466 | void MLog::AllocateFile(const char *fname)
|
---|
467 | {
|
---|
468 | // gcc 3.2:
|
---|
469 | char *txt = (char*)"logXXXXXX";
|
---|
470 | fOut = fname ? new ofstream(fname) : new ofstream(/*mkstemp(*/txt/*)*/);
|
---|
471 | fOutAllocated = kTRUE;
|
---|
472 | }
|
---|
473 |
|
---|
474 | // --------------------------------------------------------------------------
|
---|
475 | //
|
---|
476 | // if fout was allocated by this instance of MLooging
|
---|
477 | // delete it.
|
---|
478 | //
|
---|
479 | void MLog::DeallocateFile()
|
---|
480 | {
|
---|
481 | if (fOutAllocated)
|
---|
482 | delete fOut;
|
---|
483 | }
|
---|
484 |
|
---|
485 | // --------------------------------------------------------------------------
|
---|
486 | //
|
---|
487 | // if necessary delete the old in stance of the file
|
---|
488 | // output stream and create a new one
|
---|
489 | //
|
---|
490 | void MLog::ReallocateFile(const char *fname)
|
---|
491 | {
|
---|
492 | DeallocateFile();
|
---|
493 | AllocateFile(fname);
|
---|
494 | }
|
---|
495 |
|
---|
496 | // --------------------------------------------------------------------------
|
---|
497 | //
|
---|
498 | // This function checks if a device should get enabled or disabled.
|
---|
499 | //
|
---|
500 | void MLog::CheckFlag(Flags_t chk, int flag)
|
---|
501 | {
|
---|
502 | if (flag==-1)
|
---|
503 | return;
|
---|
504 |
|
---|
505 | flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
|
---|
506 | }
|
---|
507 |
|
---|
508 | // --------------------------------------------------------------------------
|
---|
509 | //
|
---|
510 | // Add a plugin to which the output should be redirected, eg. MLogHtml
|
---|
511 | // The user has to take care of its deletion. If the plugin is deleted
|
---|
512 | // (and the kMustCleanup bit was not reset accidentaly) the plugin
|
---|
513 | // is automatically removed from the list of active plugins.
|
---|
514 | //
|
---|
515 | void MLog::AddPlugin(MLogPlugin *plug)
|
---|
516 | {
|
---|
517 | fPlugins->Add(plug);
|
---|
518 | plug->SetBit(kMustCleanup);
|
---|
519 | }
|
---|