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

Last change on this file since 959 was 936, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 7.2 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 (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//////////////////////////////////////////////////////////////////////////////
42
43#include "MLog.h"
44
45#include <fstream.h>
46#include <TGListBox.h>
47
48#include "MLogManip.h"
49
50ClassImp(MLog);
51
52//
53// This is the definition of the global log facility
54//
55MLog gLog;
56
57// --------------------------------------------------------------------------
58//
59// this strange usage of an unbufferd buffer is a workaround
60// to make it work on Alpha and Linux!
61//
62void MLog::Init()
63{
64 setp(&fBuffer, &fBuffer+1);
65 *this << '\0';
66}
67
68// --------------------------------------------------------------------------
69//
70// default constructor which initializes the streamer and sets the device
71// which is used for the output (i)
72//
73MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), fGuiLineId(0), fout(NULL), fOutAllocated(kFALSE), fgui(NULL)
74{
75 Init();
76}
77
78// --------------------------------------------------------------------------
79//
80// default constructor which initializes the streamer and sets the given
81// ofstream as the default output device
82//
83MLog::MLog(ofstream &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fGuiLineId(0), fout(&out), fOutAllocated(kFALSE), fgui(NULL)
84{
85 Init();
86}
87
88// --------------------------------------------------------------------------
89//
90// default constructor which initializes the streamer and sets the given
91// TGListBox as the default output device
92//
93MLog::MLog(TGListBox &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), fGuiLineId(0), fout(NULL), fOutAllocated(kFALSE), fgui(&out)
94{
95 Init();
96}
97
98// --------------------------------------------------------------------------
99//
100// default constructor which initializes the streamer and opens a file with
101// the given name. Dependend on the flag the file is set as output device
102// or not.
103//
104MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fGuiLineId(0), fgui(NULL)
105{
106 Init();
107
108 AllocateFile(fname);
109 CheckFlag(eFile, flag);
110}
111
112// --------------------------------------------------------------------------
113//
114// copyt constructor
115//
116MLog::MLog(MLog &log)
117{
118 fOutputLevel = log.fOutputLevel;
119 fDebugLevel = log.fDebugLevel;
120 fDevice = log.fDevice;
121}
122
123// --------------------------------------------------------------------------
124//
125// This is the function which writes the stream physically to a device.
126// If you want to add a new device this must be done here.
127//
128void MLog::WriteBuffer()
129{
130 const int len = fPPtr - fBase;
131
132 if (fDevice&eStdout)
133 cout.write(fBase, len);
134
135 if (fDevice&eStderr)
136 cerr.write(fBase, len);
137
138 if (fDevice&eFile && fout)
139 fout->write(fBase, len);
140
141 if (fDevice&eGui && fgui)
142 {
143 char dummy[len+1];
144 memcpy(dummy, fBase, len);
145 *(dummy+len)='\0';
146 fgui->AddEntry(dummy, fGuiLineId);
147 fgui->SetTopEntry(fGuiLineId++);
148 fgui->SetBit(kHasChanged);
149 }
150
151 //
152 // restart writing to the buffer at its first char
153 //
154 fPPtr = fBase;
155}
156
157// --------------------------------------------------------------------------
158//
159// This is called to flush the buffer of the streaming devices
160//
161int MLog::sync()
162{
163 WriteBuffer();
164
165 if (fDevice&eStdout)
166 cout.flush();
167
168 if (fDevice&eStderr)
169 cerr.flush();
170
171 if (fDevice&eFile && fout)
172 fout->flush();
173
174 return 0;
175}
176
177// --------------------------------------------------------------------------
178//
179// This function comes from streambuf and should
180// output the buffer to the device (flush, endl)
181// or handle a buffer overflow (too many chars)
182// If a real overflow happens i contains the next
183// chars which doesn't fit into the buffer anymore.
184// If the buffer is not really filled i is EOF(-1).
185//
186int MLog::overflow(int i) // i=EOF means not a real overflow
187{
188 if (fOutputLevel >= fDebugLevel)
189 return 0;
190
191 *fPPtr++ = (char)i;
192
193 if (fPPtr == fEPtr)
194 WriteBuffer();
195
196 return 0;
197}
198
199// --------------------------------------------------------------------------
200//
201// Create a new instance of an file output stream
202// an set the corresponding flag
203//
204void MLog::AllocateFile(const char *fname)
205{
206 fout = new ofstream(fname);
207 fOutAllocated = kTRUE;
208}
209
210// --------------------------------------------------------------------------
211//
212// if fout was allocated by this instance of MLooging
213// delete it.
214//
215void MLog::DeallocateFile()
216{
217 if (fOutAllocated)
218 delete fout;
219}
220
221// --------------------------------------------------------------------------
222//
223// if necessary delete the old in stance of the file
224// output stream and create a new one
225//
226void MLog::ReallocateFile(const char *fname)
227{
228 DeallocateFile();
229 AllocateFile(fname);
230}
231
232// --------------------------------------------------------------------------
233//
234// This function checks if a device should get enabled or disabled.
235//
236void MLog::CheckFlag(Flags_t chk, int flag)
237{
238 if (flag==-1)
239 return;
240
241 flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
242}
Note: See TracBrowser for help on using the repository browser.