source: tags/Mars-V0.5/mbase/MLog.cc

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