source: trunk/MagicSoft/Mars/mbase/MLog.cc@ 8137

Last change on this file since 8137 was 8137, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 25.4 KB
Line 
1/* ======================================================================== *\
2! $Name: not supported by cvs2svn $:$Id: MLog.cc,v 1.56 2006-10-19 21:00:23 tbretz Exp $
3! --------------------------------------------------------------------------
4!
5! *
6! * This file is part of MARS, the MAGIC Analysis and Reconstruction
7! * Software. It is distributed to you in the hope that it can be a useful
8! * and timesaving tool in analysing Data of imaging Cerenkov telescopes.
9! * It is distributed WITHOUT ANY WARRANTY.
10! *
11! * Permission to use, copy, modify and distribute this software and its
12! * documentation for any purpose is hereby granted without fee,
13! * provided that the above copyright notice appear in all copies and
14! * that both that copyright notice and this permission notice appear
15! * in supporting documentation. It is provided "as is" without express
16! * or implied warranty.
17! *
18!
19!
20! Author(s): Thomas Bretz, 12/2000 <mailto:tbretz@astro.uni-wuerzburg.de>
21!
22! Copyright: MAGIC Software Development, 2000-2006
23!
24!
25\* ======================================================================== */
26
27//////////////////////////////////////////////////////////////////////////////
28//
29// MLog
30//
31// This is what we call the logging-system.
32//
33// It is derived from the C++ streaming classes and can handle our
34// logging. The log output can be redirected to stdout, stderr, any other
35// stream or a root window.
36//
37// There is a global log-instance which you can use like cout, id is gLog.
38// A log-instance of your choice (gLog by default) is destributed to all
39// Task which are used in an eventloop, so that you can redirect the output
40// of one eventloop to where you want..
41//
42// The MLog stream has the advantage, that it can be used like the common
43// C++ streams (for example cout). It can redirect the stream to different
44// outputs (console, file, GUI) if necessary at the same time.
45//
46// It supports different debug levels. The debug level of the current
47// stream contents is set by SetDebugLevel, the output level of the
48// current stream can be set by SetOutputLevel.
49//
50// The header file MLogManip.h contains so called manipulators (like flush
51// or setw from iomanip.h) which can manipulate these levels from within
52// stream, for example:
53// gLog << debug(3) << "Hallo World " << endl;
54// sets the debug level of the following stream to 3
55//
56// edev(), ddev() can be used to enable/disable an output device from
57// within the stream. The enumerations are defined in MLog::_flags
58//
59// Commonly used abbreviations are also defined:
60// dbginf Prints source file name and line number. Used for output
61// which people may like to look up in the code
62// all Is streamed to the output in any case. Used for outputs
63// which are requested by the user (eg TObject::Print)
64// err Should be used for fatal errors which stops the current
65// processing, eg:
66// gLog << err << "ERROR: TObject::Copy - Stopped" << endl;
67// warn Warning means an error occured, but it is not clear whether
68// this results further procesing or not.
69// inf Informs the user about what's going on. Mostly usefull for
70// debugging, but in general not necessary at all.
71// dbg Use this for your private purpose to mark something as debug
72// output. This is _not_ ment to be persistent!
73//
74// If your console is capable of ANSI colors the stream is displayed
75// in several colors:
76// all: default
77// err: red
78// warn: yellow/brown
79// inf: green
80// dbg: blue (and all other levels)
81//
82// If you have a dark background on your console you might want to set
83// an environment variable, eg:
84// export MARSDEFINES=-DHAVE_DARKBACKGROUND
85// and recompile MLog.
86//
87// If your console can display it also 'underline' can be used. This
88// underlines a text till the next 'endl', eg:
89// gLog << underline << "This is important!" << endl;
90//
91// To switch off ANSI support call: SetNoColors()
92//
93// gLog is a global stream defined like cout or cerr
94//
95//////////////////////////////////////////////////////////////////////////////
96#include "MLog.h"
97
98#include <stdlib.h> // mkstemp
99
100#include <fstream>
101#include <iomanip>
102
103#include <TROOT.h> // gROOT->GetListOfCleanups()
104#include <TSystem.h>
105
106#ifdef _REENTRANT
107#include <TMutex.h>
108#endif
109#include <TGTextView.h>
110
111#include <TEnv.h> // gEnv (ErrorHandler)
112#include <TError.h> // TError (SetErrorHandler)
113
114#include "MArgs.h"
115#include "MTime.h"
116#include "MParContainer.h"
117
118#include "MLogHtml.h"
119#include "MLogManip.h" // inf,warn,err (MLog::ErrorHandler)
120
121ClassImp(MLog);
122
123using namespace std;
124
125#undef DEBUG
126//#define DEBUG
127
128
129// root 3.02:
130// check for TObjectWarning, TObject::Info, gErrorIgnoreLevel
131
132const char MLog::kESC = '\033'; // (char)27
133const char *const MLog::kEsc = "\033[";
134const char *const MLog::kReset = "\033[0m";
135const char *const MLog::kRed = "\033[31m";
136const char *const MLog::kGreen = "\033[32m";
137#ifdef HAVE_DARKBACKGROUND
138const char *const MLog::kYellow = "\033[33m\033[1m";
139#else
140const char *const MLog::kYellow = "\033[33m";
141#endif
142const char *const MLog::kBlue = "\033[34m";
143const char *const MLog::kUnderline = "\033[4m";
144const char *const MLog::kBlink = "\033[5m";
145const char *const MLog::kBright = "\033[1m";
146const char *const MLog::kDark = "\033[2m";
147
148//
149// This is the definition of the global log facility
150//
151MLog gLog;
152
153// --------------------------------------------------------------------------
154//
155// this strange usage of an unbufferd buffer is a workaround
156// to make it work on Alpha and Linux!
157//
158void MLog::Init()
159{
160 //
161 // Creat drawing semaphore
162 //
163#ifdef _REENTRANT
164 fMuxGui = new TMutex;
165 fMuxStream = new TMutex;
166#endif
167
168 fPlugins = new TList;
169 gROOT->GetListOfCleanups()->Add(fPlugins);
170 fPlugins->SetBit(kMustCleanup);
171
172 setp(&fBuffer, &fBuffer+1);
173 *this << '\0';
174}
175
176// --------------------------------------------------------------------------
177//
178// default constructor which initializes the streamer and sets the device
179// which is used for the output (i)
180//
181MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), fIsNull(kFALSE), fOut(NULL), fOutAllocated(kFALSE), fGui(NULL), fNumLines(0)
182{
183 Init();
184}
185
186// --------------------------------------------------------------------------
187//
188// default constructor which initializes the streamer and sets the given
189// ofstream as the default output device
190//
191MLog::MLog(ofstream &sout) : ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fOut(&sout), fOutAllocated(kFALSE), fGui(NULL), fNumLines(0)
192{
193 Init();
194}
195
196// --------------------------------------------------------------------------
197//
198// default constructor which initializes the streamer and sets the given
199// TGTextView as the default output device
200//
201MLog::MLog(TGTextView &sout) : ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), fOut(NULL), fOutAllocated(kFALSE), fGui(&sout), fNumLines(0)
202{
203 Init();
204}
205
206// --------------------------------------------------------------------------
207//
208// default constructor which initializes the streamer and opens a file with
209// the given name. Dependend on the flag the file is set as output device
210// or not.
211//
212MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+fgBufferSize), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fGui(NULL), fNumLines(0)
213{
214 Init();
215
216 AllocateFile(fname);
217 CheckFlag(eFile, flag);
218}
219
220// --------------------------------------------------------------------------
221//
222// Destructor, destroying the gui mutex.
223//
224MLog::~MLog()
225{
226 DeallocateFile();
227
228#ifdef DEBUG
229 TIter Next(fPlugins);
230 TObject *o=0;
231 while ((o=Next()))
232 {
233 cout << "Delete: " << o->GetName() << std::flush;
234 cout << " [" << o->ClassName() << "]" << endl;
235 delete o;
236 }
237
238 cout << "Delete: fPlugins " << fPlugins << "..." << std::flush;
239#endif
240
241 delete fPlugins;
242#ifdef DEBUG
243 cout << "done." << endl;
244#endif
245
246#ifdef _REENTRANT
247 delete fMuxStream;
248 delete fMuxGui;
249#endif
250}
251
252// --------------------------------------------------------------------------
253//
254// copyt constructor
255//
256/*
257MLog::MLog(MLog const& log)
258{
259// fOutputLevel = log.fOutputLevel;
260// fDebugLevel = log.fDebugLevel;
261// fDevice = log.fDevice;
262}
263*/
264
265void MLog::Underline()
266{
267 if (fIsNull)
268 return;
269
270 SetBit(kIsUnderlined);
271
272 fPlugins->R__FOR_EACH(MLogPlugin, Underline)();
273
274 if (TestBit(eNoColors))
275 return;
276
277 if (fDevice&eStdout)
278 cout << kUnderline;
279
280 if (fDevice&eStderr)
281 cerr << kUnderline;
282}
283
284void MLog::Output(ostream &sout, int len)
285{
286 if (!TestBit(eNoColors))
287 switch (fOutputLevel)
288 {
289 // do not output reset. Otherwise we reset underline in 0-mode
290 // case 1: out << MLog::kReset; break; // all
291 case 0: break; // all = background color
292 case 1: sout << MLog::kRed; break; // err
293 case 2: sout << MLog::kYellow; break; // warn
294 case 3: sout << MLog::kGreen; break; // inf
295 default: sout << MLog::kBlue; break; // all others (dbg)
296 }
297
298 if (len>0)
299 {
300 // Check for EOL
301 const Int_t endline = fBase[len-1]=='\n' ? 1 : 0;
302 // output text to screen (without trailing '\n')
303 sout << TString(fBase, len-endline);
304 // reset colors if working with colors
305 if (!TestBit(eNoColors))
306 sout << kReset;
307 // output EOL of check found EOL
308 if (endline)
309 {
310 sout << '\n';
311 // Check whether text was underlined
312 if (TestBit(kIsUnderlined) && TestBit(eNoColors))
313 {
314 sout << setw(len-1) << setfill('-') << "" << "\n";
315 ResetBit(kIsUnderlined);
316 }
317 }
318 }
319 sout.flush();
320}
321
322void MLog::AddGuiLine(const TString &line)
323{
324 // add a new TString* to the array of gui lines
325 TString **newstr = new TString*[fNumLines+1];
326 memcpy(newstr, fGuiLines, fNumLines*sizeof(TString*));
327 if (fNumLines>0)
328 delete fGuiLines;
329 fGuiLines = newstr;
330
331 // add Gui line as last line of array
332 fGuiLines[fNumLines++] = new TString(line);
333}
334
335// --------------------------------------------------------------------------
336//
337// This is the function which writes the stream physically to a device.
338// If you want to add a new device this must be done here.
339//
340void MLog::WriteBuffer()
341{
342 //
343 // restart writing to the buffer at its first char
344 //
345 const int len = fPPtr - fBase;
346
347 fPPtr = fBase;
348
349 if (fIsNull)
350 return;
351
352 if (fDevice&eStdout)
353 Output(cout, len);
354
355 if (fDevice&eStderr)
356 Output(cerr, len);
357
358 if (fDevice&eFile && fOut)
359 fOut->write(fBase, len);
360
361 fPlugins->R__FOR_EACH(MLogPlugin, SetColor)(fOutputLevel);
362 fPlugins->R__FOR_EACH(MLogPlugin, WriteBuffer)(fBase, len);
363
364 if (fDevice&eGui && fGui)
365 {
366 // check whether the current text was flushed or endl'ed
367 const Int_t endline = fBase[len-1]=='\n' ? 1 : 0;
368
369 // for the gui remove trailing characters ('\n' or '\0')
370 fBase[len-endline]='\0';
371
372 // add new text to line storage
373 fGuiLine += fBase;
374
375 if (endline)
376 {
377 AddGuiLine(fGuiLine);
378 fGuiLine = "";
379
380 // Check whether text should be underlined
381 if (endline && TestBit(kIsUnderlined))
382 {
383 AddGuiLine("");
384 fGuiLines[fNumLines-1]->Append('-', fGuiLines[fNumLines-2]->Length());
385 ResetBit(kIsUnderlined);
386 }
387 }
388 }
389}
390
391void MLog::UpdateGui()
392{
393 if (fNumLines==0)
394 return;
395
396 // lock mutex
397 if (!LockUpdate("UpdateGui"))
398 {
399 Warning("UpdateGui", "Execution skipped");
400 return;
401 }
402
403 TGText &txt=*fGui->GetText();
404
405 // copy lines to TGListBox
406 for (int i=0; i<fNumLines; i++)
407 {
408 // Replace all tabs by 7 white spaces
409 fGuiLines[i]->ReplaceAll("\t", " ");
410 txt.InsText(TGLongPosition(0, txt.RowCount()), *fGuiLines[i]);
411 delete fGuiLines[i];
412 }
413 delete fGuiLines;
414
415 fNumLines=0;
416
417 // cut text box top 1000 lines
418 // while (txt.RowCount()>1000)
419 // txt.DelLine(1);
420
421 // show last entry
422 fGui->Layout();
423 fGui->SetVsbPosition(txt.RowCount()-1);
424
425 // tell a main loop, that list box contents have changed
426 fGui->SetBit(kHasChanged);
427
428 // release mutex
429 UnLockUpdate("UpdateGui");
430}
431
432bool MLog::LockUpdate(const char *msg)
433{
434#ifdef _REENTRANT
435 if (fMuxGui->Lock()==13)
436 {
437 Info("LockUpdate", "%s - mutex is already locked by this thread\n", msg);
438 return false;
439 }
440#endif
441 return true;
442}
443
444bool MLog::UnLockUpdate(const char *msg)
445{
446#ifdef _REENTRANT
447 if (fMuxGui->UnLock()==13)
448 {
449 Info("UnLockUpdate", "%s - tried to unlock mutex locked by other thread\n", msg);
450 return false;
451 }
452#endif
453 return true;
454}
455
456bool MLog::Lock(const char *msg)
457{
458#ifdef _REENTRANT
459 if (fMuxStream->Lock()==13)
460 {
461 Error("Lock", "%s - mutex is already locked by this thread\n", msg);
462 return false;
463 }
464// while (fMuxStream->Lock()==13)
465// usleep(1);
466// {
467// Error("Lock", "%s - mutex is already locked by this thread\n", msg);
468// return false;
469// }
470#endif
471 return true;
472}
473
474bool MLog::UnLock(const char *msg)
475{
476#ifdef _REENTRANT
477 if (fMuxStream->UnLock()==13)
478 {
479 Error("UnLock", "%s - tried to unlock mutex locked by other thread\n", msg);
480 return false;
481 }
482#endif
483 return true;
484}
485
486// --------------------------------------------------------------------------
487//
488// This is called to flush the buffer of the streaming devices
489//
490int MLog::sync()
491{
492 if (!LockUpdate("sync"))
493 usleep(1);
494 WriteBuffer();
495 UnLockUpdate("sync");
496
497 if (fDevice&eStdout)
498 {
499 if (!fIsNull && !TestBit(eNoColors))
500 cout << kReset;
501 cout.flush();
502 }
503
504 if (fDevice&eStderr)
505 cerr.flush();
506
507 if (fDevice&eFile && fOut)
508 fOut->flush();
509
510 return 0;
511}
512
513// --------------------------------------------------------------------------
514//
515// This function comes from streambuf and should
516// output the buffer to the device (flush, endl)
517// or handle a buffer overflow (too many chars)
518// If a real overflow happens i contains the next
519// chars which doesn't fit into the buffer anymore.
520// If the buffer is not really filled i is EOF(-1).
521//
522int MLog::overflow(int i) // i=EOF means not a real overflow
523{
524 //
525 // no output if
526 //
527 if (fOutputLevel <= fDebugLevel)
528 {
529 if (!LockUpdate("overflow"))
530 usleep(1);
531
532 *fPPtr++ = (char)i;
533
534 if (fPPtr == fEPtr)
535 WriteBuffer();
536
537 UnLockUpdate("overflow");
538 }
539
540 return 0;
541}
542
543// --------------------------------------------------------------------------
544//
545// Print usage information setup in Setup()
546//
547void MLog::Usage()
548{
549 // 1 2 3 4 5 6 7 8
550 // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
551 *this << " -v# Verbosity level # [default=2]" << endl;
552 *this << " -a, --no-colors Do not use Ansii color codes" << endl;
553 *this << " --log[=file] Write log-out to ascii-file [default: prgname.log]" << endl;
554 *this << " --html[=file] Write log-out to html-file [default: prgname.html]" << endl;
555 *this << " --debug[=n] Enable root debugging [default: gDebug=1]" << endl;
556 *this << " --null Null output (supresses all output)" << endl;
557}
558
559// --------------------------------------------------------------------------
560//
561// Setup MLog and global debug output from command line arguments.
562//
563void MLog::Setup(MArgs &arg)
564{
565 // FXIME: This is not really at a place where it belongs to!
566 gDebug = arg.HasOption("--debug=") ? arg.GetIntAndRemove("--debug=") : 0;
567 if (gDebug==0 && arg.HasOnlyAndRemove("--debug"))
568 gDebug=1;
569
570 TString f1 = arg.GetStringAndRemove("--log=", "");
571 if (f1.IsNull() && arg.HasOnlyAndRemove("--log"))
572 f1 = Form("%s.log", arg.GetName());
573 if (!f1.IsNull())
574 {
575 SetOutputFile(f1);
576 EnableOutputDevice(eFile);
577 }
578
579 TString f2 = arg.GetStringAndRemove("--html=", "");
580 if (f2.IsNull() && arg.HasOnlyAndRemove("--html"))
581 f2 = Form("%s.html", arg.GetName());
582 if (!f2.IsNull())
583 {
584 MLogHtml *html = new MLogHtml(f2);
585 html->SetBit(kCanDelete);
586 AddPlugin(html);
587 }
588
589 const Bool_t null = arg.HasOnlyAndRemove("--null");
590 if (null)
591 SetNullOutput();
592
593 if (arg.HasOnlyAndRemove("--no-colors") || arg.HasOnlyAndRemove("-a"))
594 SetNoColors();
595
596 SetDebugLevel(arg.GetIntAndRemove("-v", 2));
597}
598
599// --------------------------------------------------------------------------
600//
601// Read the setup from a TEnv:
602// MLog.VerbosityLevel: 0, 1, 2, 3, 4
603// MLog.DebugLevel: 0, 1, 2, 3, 4
604// MLog.NoColors
605//
606// Depending on your setup it might be correct to use something like:
607// Job1.MLog.VerbosityLevel: 1
608// Job1.DebugLevel: 2
609// Job1.MLog.NoColors
610//
611void MLog::ReadEnv(const TEnv &env, TString prefix, Bool_t print)
612{
613 MParContainer mlog("MLog");
614
615 if (mlog.IsEnvDefined(env, prefix+"MLog", "VerbosityLevel", print))
616 SetDebugLevel(mlog.GetEnvValue(env, prefix+"MLog", "VerbosityLevel", 2));
617 else
618 if (mlog.IsEnvDefined(env, "MLog", "VerbosityLevel", print))
619 SetDebugLevel(mlog.GetEnvValue(env, "MLog", "VerbosityLevel", 2));
620
621 if (mlog.IsEnvDefined(env, prefix+"MLog", "DebugLevel", print))
622 gDebug = mlog.GetEnvValue(env, prefix+"MLog", "DebugLevel", 0);
623 else
624 if (mlog.IsEnvDefined(env, "MLog", "DebugLevel", print))
625 gDebug = mlog.GetEnvValue(env, "MLog", "DebugLevel", 0);
626
627 if (mlog.IsEnvDefined(env, prefix+"MLog", "NoColors", print))
628 SetNoColors(mlog.GetEnvValue(env, prefix+"MLog", "NoColors", kFALSE));
629 else
630 if (mlog.IsEnvDefined(env, "MLog", "NoColors", print))
631 SetNoColors(mlog.GetEnvValue(env, "MLog", "NoColors", kFALSE));
632}
633
634// --------------------------------------------------------------------------
635//
636// Read the setup from a TEnv:
637// MLog.VerbosityLevel: 0, 1, 2, 3, 4
638// MLog.DebugLevel: 0, 1, 2, 3, 4
639// MLog.NoColors
640//
641// Depending on your setup it might be correct to use something like:
642// Job1.MLog.VerbosityLevel: 1
643// Job1.DebugLevel: 2
644// Job1.MLog.NoColors
645//
646void MLog::WriteEnv(TEnv &, TString prefix, Bool_t) const
647{
648 if (!prefix.IsNull())
649 prefix += ".";
650 prefix += "MLog";
651
652 cout << "MLog::WriteEnv: not yet implemented!" << endl;
653}
654
655// --------------------------------------------------------------------------
656//
657// Create a new instance of an file output stream
658// an set the corresponding flag
659//
660void MLog::AllocateFile(const char *fname)
661{
662 // gcc 3.2:
663 char *txt = (char*)"logXXXXXX";
664
665 TString n(fname ? fname : txt);
666 gSystem->ExpandPathName(n);
667
668 fOut = new ofstream(n.Data());
669
670 // switch off buffering
671 fOut->rdbuf()->pubsetbuf(0,0);
672
673 fOutAllocated = kTRUE;
674}
675
676// --------------------------------------------------------------------------
677//
678// if fout was allocated by this instance of MLooging
679// delete it.
680//
681void MLog::DeallocateFile()
682{
683 if (fOutAllocated)
684 delete fOut;
685}
686
687// --------------------------------------------------------------------------
688//
689// if necessary delete the old in stance of the file
690// output stream and create a new one
691//
692void MLog::ReallocateFile(const char *fname)
693{
694 DeallocateFile();
695 AllocateFile(fname);
696}
697
698// --------------------------------------------------------------------------
699//
700// This function checks if a device should get enabled or disabled.
701//
702void MLog::CheckFlag(Flags_t chk, int flag)
703{
704 if (flag==-1)
705 return;
706
707 flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
708}
709
710// --------------------------------------------------------------------------
711//
712// Add a plugin to which the output should be redirected, eg. MLogHtml
713// The user has to take care of its deletion. If the plugin is deleted
714// (and the kMustCleanup bit was not reset accidentaly) the plugin
715// is automatically removed from the list of active plugins.
716//
717// If MLog should take the ownership call plug->SetBit(kCanDelete);
718//
719void MLog::AddPlugin(MLogPlugin *plug)
720{
721 fPlugins->Add(plug);
722
723 // Make sure that it is recursively deleted from all objects in ListOfCleanups
724 plug->SetBit(kMustCleanup);
725}
726
727// --------------------------------------------------------------------------
728//
729// Returns "yyyy-mm-dd user@host gROOT->GetName()[pid]"
730//
731TString MLog::Intro()
732{
733 UserGroup_t *user = gSystem->GetUserInfo();
734
735 TString rc;
736 rc += MTime(-1).GetSqlDateTime();
737 rc += " ";
738 rc += user->fUser;
739 rc += "@";
740 rc += gSystem->HostName();
741 rc += " ";
742 rc += gROOT->GetName();
743 rc += "[";
744 rc += gSystem->GetPid();
745 rc += "] ";
746
747 delete user;
748
749 return rc;
750}
751
752// --------------------------------------------------------------------------
753//
754// Check whether errors at this level should be ignored.
755//
756bool MLog::ErrorHandlerIgnore(Int_t level)
757{
758 // The default error handler function. It prints the message on stderr and
759 // if abort is set it aborts the application.
760 if (gErrorIgnoreLevel == kUnset) {
761 R__LOCKGUARD2(gErrorMutex);
762
763 gErrorIgnoreLevel = 0;
764 if (gEnv) {
765 TString lvl = gEnv->GetValue("Root.ErrorIgnoreLevel", "Info");
766 if (!lvl.CompareTo("Info",TString::kIgnoreCase))
767 gErrorIgnoreLevel = kInfo;
768 else if (!lvl.CompareTo("Warning",TString::kIgnoreCase))
769 gErrorIgnoreLevel = kWarning;
770 else if (!lvl.CompareTo("Error",TString::kIgnoreCase))
771 gErrorIgnoreLevel = kError;
772 else if (!lvl.CompareTo("Break",TString::kIgnoreCase))
773 gErrorIgnoreLevel = kBreak;
774 else if (!lvl.CompareTo("SysError",TString::kIgnoreCase))
775 gErrorIgnoreLevel = kSysError;
776 else if (!lvl.CompareTo("Fatal",TString::kIgnoreCase))
777 gErrorIgnoreLevel = kFatal;
778 }
779 }
780
781 return level < gErrorIgnoreLevel;
782}
783
784// --------------------------------------------------------------------------
785//
786// Output the root error message to the log-stream.
787//
788void MLog::ErrorHandlerPrint(Int_t level, const char *location, const char *msg)
789{
790 R__LOCKGUARD2(gErrorMutex);
791
792 if (level >= kError)
793 gLog << "ROOT:Error";
794 else
795 if (level >= kSysError)
796 gLog << "SysError";
797 else
798 if (level >= kBreak)
799 gLog << "\n *** Break ***";
800 else
801 if (level >= kFatal)
802 gLog << "Fatal";
803 else
804 if (level >= kWarning)
805 gLog << "ROOT:Warning";
806 else
807 if (level >= kInfo)
808 gLog << "ROOT:Info";
809
810 if (level >= kBreak && level < kSysError)
811 gLog << ": " << msg << std::endl;
812 else
813 if (!location || strlen(location) == 0)
814 gLog << ": " << msg << std::endl;
815 else
816 gLog << " in <" << location << ">: " << msg << std::endl;
817}
818
819// --------------------------------------------------------------------------
820//
821// A new error handler using gLog instead of stderr as output.
822// It is mainly a copy of root's DefaultErrorHandler
823// (see TError.h and TError.cxx)
824//
825void MLog::ErrorHandlerCol(Int_t level, Bool_t abort, const char *location, const char *msg)
826{
827 if (ErrorHandlerIgnore(level))
828 return;
829
830 gLog << std::flush;
831
832 if (level >= kInfo)
833 gLog << inf;
834 if (level >= kWarning)
835 gLog << warn;
836 if (level >= kError)
837 gLog << err;
838
839 ErrorHandlerPrint(level, location, msg);
840
841 gLog << std::flush;
842 if (!abort)
843 return;
844
845 gLog << err << "aborting" << std::endl;
846 if (gSystem) {
847 gSystem->StackTrace();
848 gSystem->Abort();
849 } else
850 ::abort();
851}
852
853// --------------------------------------------------------------------------
854//
855// A new error handler using gLog instead of stderr as output.
856// It is mainly a copy of root's DefaultErrorHandler
857// (see TError.h and TError.cxx)
858//
859void MLog::ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg)
860{
861 if (ErrorHandlerIgnore(level))
862 return;
863
864 gLog << std::flush << all;
865
866 ErrorHandlerPrint(level, location, msg);
867
868 gLog << std::flush;
869 if (!abort)
870 return;
871
872 gLog << err << "aborting" << std::endl;
873 if (gSystem) {
874 gSystem->StackTrace();
875 gSystem->Abort();
876 } else
877 ::abort();
878}
879
880// --------------------------------------------------------------------------
881//
882// Redirect the root ErrorHandler (see TError.h) output to gLog.
883//
884// The diffrent types are:
885// kColor: Use gLog colors
886// kBlackWhite: Use all-qualifier (as in gLog << all << endl;)
887// kDefault: Set back to root's default error handler
888// (redirect output to stderr)
889//
890void MLog::RedirectErrorHandler(ELogType typ)
891{
892 switch (typ)
893 {
894 case kColor:
895 SetErrorHandler(MLog::ErrorHandlerCol);
896 break;
897 case kBlackWhite:
898 SetErrorHandler(MLog::ErrorHandlerAll);
899 break;
900 case kDefault:
901 SetErrorHandler(DefaultErrorHandler);
902 }
903}
Note: See TracBrowser for help on using the repository browser.