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

Last change on this file since 1121 was 1080, checked in by tbretz, 23 years ago
*** empty log message ***
File size: 7.4 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//////////////////////////////////////////////////////////////////////////////
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 //
191 // no output if
192 //
193 if (fOutputLevel > fDebugLevel)
194 return 0;
195
196 *fPPtr++ = (char)i;
197
198 if (fPPtr == fEPtr)
199 WriteBuffer();
200
201 return 0;
202}
203
204// --------------------------------------------------------------------------
205//
206// Create a new instance of an file output stream
207// an set the corresponding flag
208//
209void MLog::AllocateFile(const char *fname)
210{
211 fout = fname ? new ofstream(fname) : new ofstream(mkstemp("logXXXXXX"));
212 fOutAllocated = kTRUE;
213}
214
215// --------------------------------------------------------------------------
216//
217// if fout was allocated by this instance of MLooging
218// delete it.
219//
220void MLog::DeallocateFile()
221{
222 if (fOutAllocated)
223 delete fout;
224}
225
226// --------------------------------------------------------------------------
227//
228// if necessary delete the old in stance of the file
229// output stream and create a new one
230//
231void MLog::ReallocateFile(const char *fname)
232{
233 DeallocateFile();
234 AllocateFile(fname);
235}
236
237// --------------------------------------------------------------------------
238//
239// This function checks if a device should get enabled or disabled.
240//
241void MLog::CheckFlag(Flags_t chk, int flag)
242{
243 if (flag==-1)
244 return;
245
246 flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
247}
Note: See TracBrowser for help on using the repository browser.