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

Last change on this file since 2275 was 2232, checked in by tbretz, 21 years ago
*** empty log message ***
File size: 13.7 KB
Line 
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@uni-sw.gwdg.de>
19!
20! Copyright: MAGIC Software Development, 2000-2001
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#ifdef _REENTRANT
103#include <pthread.h>
104#endif
105#include <TGTextView.h>
106
107ClassImp(MLog);
108
109using namespace std;
110
111// root 3.02:
112// check for TObjectWarning, TObject::Info, gErrorIgnoreLevel
113
114const char MLog::kESC = '\033'; // (char)27
115const char *const MLog::kEsc = "\033[";
116const char *const MLog::kReset = "\033[0m";
117const char *const MLog::kRed = "\033[31m";
118const char *const MLog::kGreen = "\033[32m";
119#ifdef HAVE_DARKBACKGROUND
120const char *const MLog::kYellow = "\033[33m\033[1m";
121#else
122const char *const MLog::kYellow = "\033[33m";
123#endif
124const char *const MLog::kBlue = "\033[34m";
125const char *const MLog::kUnderline = "\033[4m";;
126const char *const MLog::kBlink = "\033[5m";;
127const char *const MLog::kBright = "\033[1m";;
128const char *const MLog::kDark = "\033[2m";;
129
130//
131// This is the definition of the global log facility
132//
133MLog gLog;
134
135// --------------------------------------------------------------------------
136//
137// this strange usage of an unbufferd buffer is a workaround
138// to make it work on Alpha and Linux!
139//
140void MLog::Init()
141{
142 setp(&fBuffer, &fBuffer+1);
143 *this << '\0';
144
145#ifdef _REENTRANT
146 //
147 // Creat drawing semaphore
148 //
149 fMuxGui = new pthread_mutex_t;
150 pthread_mutex_init((pthread_mutex_t*)fMuxGui, NULL);
151#endif
152}
153
154// --------------------------------------------------------------------------
155//
156// default constructor which initializes the streamer and sets the device
157// which is used for the output (i)
158//
159MLog::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)
160{
161 Init();
162}
163
164// --------------------------------------------------------------------------
165//
166// default constructor which initializes the streamer and sets the given
167// ofstream as the default output device
168//
169MLog::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)
170{
171 Init();
172}
173
174// --------------------------------------------------------------------------
175//
176// default constructor which initializes the streamer and sets the given
177// TGTextView as the default output device
178//
179MLog::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)
180{
181 Init();
182}
183
184// --------------------------------------------------------------------------
185//
186// default constructor which initializes the streamer and opens a file with
187// the given name. Dependend on the flag the file is set as output device
188// or not.
189//
190MLog::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)
191{
192 Init();
193
194 AllocateFile(fname);
195 CheckFlag(eFile, flag);
196}
197
198// --------------------------------------------------------------------------
199//
200// Destructor, destroying the gui mutex.
201//
202MLog::~MLog()
203{
204 DeallocateFile();
205#ifdef _REENTRANT
206 pthread_mutex_destroy((pthread_mutex_t*)fMuxGui);
207#endif
208}
209
210// --------------------------------------------------------------------------
211//
212// copyt constructor
213//
214/*
215MLog::MLog(MLog const& log)
216{
217// fOutputLevel = log.fOutputLevel;
218// fDebugLevel = log.fDebugLevel;
219// fDevice = log.fDevice;
220}
221*/
222
223void MLog::Underline()
224{
225 SetBit(kIsUnderlined);
226
227 if (TestBit(eNoColors))
228 return;
229
230 if (fDevice&eStdout)
231 cout << kUnderline;
232
233 if (fDevice&eStderr)
234 cerr << kUnderline;
235}
236
237void MLog::Output(ostream &out, int len)
238{
239 if (!TestBit(eNoColors))
240 switch (fOutputLevel)
241 {
242 // do not output reset. Otherwise we reset underline in 0-mode
243 // case 1: out << MLog::kReset; break; // all
244 case 0: break; // all = background color
245 case 1: out << MLog::kRed; break; // err
246 case 2: out << MLog::kYellow; break; // warn
247 case 3: out << MLog::kGreen; break; // inf
248 default: out << MLog::kBlue; break; // all others (dbg)
249 }
250
251 if (len>0)
252 {
253 // Check for EOL
254 const Int_t endline = fBase[len-1]=='\n' ? 1 : 0;
255 // output text to screen (without trailing '\n')
256 out << TString(fBase, len-endline);
257 // reset colors if working with colors
258 if (!TestBit(eNoColors))
259 out << kReset;
260 // output EOL of check found EOL
261 if (endline)
262 {
263 out << '\n';
264 // Check whether text was underlined
265 if (TestBit(kIsUnderlined) && TestBit(eNoColors))
266 {
267 out << setw(len-1) << setfill('-') << "" << "\n";
268 ResetBit(kIsUnderlined);
269 }
270 }
271 }
272 out.flush();
273}
274
275void MLog::AddGuiLine(const TString &line)
276{
277 // add a new TString* to the array of gui lines
278 TString **newstr = new TString*[fNumLines+1];
279 memcpy(newstr, fGuiLines, fNumLines*sizeof(TString*));
280 if (fNumLines>0)
281 delete fGuiLines;
282 fGuiLines = newstr;
283
284 // add Gui line as last line of array
285 fGuiLines[fNumLines++] = new TString(line);
286}
287
288// --------------------------------------------------------------------------
289//
290// This is the function which writes the stream physically to a device.
291// If you want to add a new device this must be done here.
292//
293void MLog::WriteBuffer()
294{
295 //
296 // restart writing to the buffer at its first char
297 //
298 const int len = fPPtr - fBase;
299
300 fPPtr = fBase;
301
302 if (fIsNull)
303 return;
304
305 if (fDevice&eStdout)
306 Output(cout, len);
307
308 if (fDevice&eStderr)
309 Output(cerr, len);
310
311 if (fDevice&eFile && fout)
312 fout->write(fBase, len);
313
314 if (fDevice&eGui && fgui)
315 {
316 // check whether the current text was flushed or endl'ed
317 const Int_t endline = fBase[len-1]=='\n' ? 1 : 0;
318
319 // for the gui remove trailing characters ('\n' or '\0')
320 fBase[len-endline]='\0';
321
322 // add new text to line storage
323 fGuiLine += fBase;
324
325 if (endline)
326 {
327 AddGuiLine(fGuiLine);
328 fGuiLine = "";
329
330 // Check whether text should be underlined
331 if (endline && TestBit(kIsUnderlined))
332 {
333 AddGuiLine("");
334 fGuiLines[fNumLines-1]->Append('-', fGuiLines[fNumLines-2]->Length());
335 ResetBit(kIsUnderlined);
336 }
337 }
338 }
339}
340
341void MLog::UpdateGui()
342{
343 if (fNumLines==0)
344 return;
345
346 // lock mutex
347 Lock();
348
349 TGText &txt=*fgui->GetText();
350
351 // copy lines to TGListBox
352 for (int i=0; i<fNumLines; i++)
353 {
354 // Replace all tabs by 7 white spaces
355 fGuiLines[i]->ReplaceAll("\t", " ");
356 txt.InsText(TGLongPosition(0, txt.RowCount()), *fGuiLines[i]);
357 delete fGuiLines[i];
358 }
359 delete fGuiLines;
360
361 fNumLines=0;
362
363 // cut text box top 1000 lines
364 // while (txt.RowCount()>1000)
365 // txt.DelLine(1);
366
367 // show last entry
368 fgui->Layout();
369 fgui->SetVsbPosition(txt.RowCount()-1);
370
371 // tell a main loop, that list box contents have changed
372 fgui->SetBit(kHasChanged);
373
374 // release mutex
375 UnLock();
376}
377
378void MLog::Lock()
379{
380#ifdef _REENTRANT
381 pthread_mutex_lock((pthread_mutex_t*)fMuxGui);
382#endif
383}
384
385void MLog::UnLock()
386{
387#ifdef _REENTRANT
388 pthread_mutex_unlock((pthread_mutex_t*)fMuxGui);
389#endif
390}
391
392// --------------------------------------------------------------------------
393//
394// This is called to flush the buffer of the streaming devices
395//
396int MLog::sync()
397{
398 Lock();
399 WriteBuffer();
400 UnLock();
401
402 if (fDevice&eStdout)
403 {
404 if (!TestBit(eNoColors))
405 cout << kReset;
406 cout.flush();
407 }
408
409 if (fDevice&eStderr)
410 cerr.flush();
411
412 if (fDevice&eFile && fout)
413 fout->flush();
414
415 return 0;
416}
417
418// --------------------------------------------------------------------------
419//
420// This function comes from streambuf and should
421// output the buffer to the device (flush, endl)
422// or handle a buffer overflow (too many chars)
423// If a real overflow happens i contains the next
424// chars which doesn't fit into the buffer anymore.
425// If the buffer is not really filled i is EOF(-1).
426//
427int MLog::overflow(int i) // i=EOF means not a real overflow
428{
429 //
430 // no output if
431 //
432 if (fOutputLevel <= fDebugLevel)
433 {
434 Lock();
435
436 *fPPtr++ = (char)i;
437
438 if (fPPtr == fEPtr)
439 WriteBuffer();
440
441 UnLock();
442 }
443
444 return 0;
445}
446
447// --------------------------------------------------------------------------
448//
449// Create a new instance of an file output stream
450// an set the corresponding flag
451//
452void MLog::AllocateFile(const char *fname)
453{
454 // gcc 3.2:
455 char *txt = (char*)"logXXXXXX";
456 fout = fname ? new ofstream(fname) : new ofstream(/*mkstemp(*/txt/*)*/);
457 fOutAllocated = kTRUE;
458}
459
460// --------------------------------------------------------------------------
461//
462// if fout was allocated by this instance of MLooging
463// delete it.
464//
465void MLog::DeallocateFile()
466{
467 if (fOutAllocated)
468 delete fout;
469}
470
471// --------------------------------------------------------------------------
472//
473// if necessary delete the old in stance of the file
474// output stream and create a new one
475//
476void MLog::ReallocateFile(const char *fname)
477{
478 DeallocateFile();
479 AllocateFile(fname);
480}
481
482// --------------------------------------------------------------------------
483//
484// This function checks if a device should get enabled or disabled.
485//
486void MLog::CheckFlag(Flags_t chk, int flag)
487{
488 if (flag==-1)
489 return;
490
491 flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
492}
Note: See TracBrowser for help on using the repository browser.