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

Last change on this file since 994 was 989, checked in by tbretz, 24 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[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 }
151
152 //
153 // restart writing to the buffer at its first char
154 //
155 fPPtr = fBase;
156}
157
158// --------------------------------------------------------------------------
159//
160// This is called to flush the buffer of the streaming devices
161//
162int MLog::sync()
163{
164 WriteBuffer();
165
166 if (fDevice&eStdout)
167 cout.flush();
168
169 if (fDevice&eStderr)
170 cerr.flush();
171
172 if (fDevice&eFile && fout)
173 fout->flush();
174
175 return 0;
176}
177
178// --------------------------------------------------------------------------
179//
180// This function comes from streambuf and should
181// output the buffer to the device (flush, endl)
182// or handle a buffer overflow (too many chars)
183// If a real overflow happens i contains the next
184// chars which doesn't fit into the buffer anymore.
185// If the buffer is not really filled i is EOF(-1).
186//
187int MLog::overflow(int i) // i=EOF means not a real overflow
188{
189 if (fOutputLevel >= fDebugLevel)
190 return 0;
191
192 *fPPtr++ = (char)i;
193
194 if (fPPtr == fEPtr)
195 WriteBuffer();
196
197 return 0;
198}
199
200// --------------------------------------------------------------------------
201//
202// Create a new instance of an file output stream
203// an set the corresponding flag
204//
205void MLog::AllocateFile(const char *fname)
206{
207 fout = fname ? new ofstream(fname) : new ofstream(mkstemp("logXXXXXX"));
208 fOutAllocated = kTRUE;
209}
210
211// --------------------------------------------------------------------------
212//
213// if fout was allocated by this instance of MLooging
214// delete it.
215//
216void MLog::DeallocateFile()
217{
218 if (fOutAllocated)
219 delete fout;
220}
221
222// --------------------------------------------------------------------------
223//
224// if necessary delete the old in stance of the file
225// output stream and create a new one
226//
227void MLog::ReallocateFile(const char *fname)
228{
229 DeallocateFile();
230 AllocateFile(fname);
231}
232
233// --------------------------------------------------------------------------
234//
235// This function checks if a device should get enabled or disabled.
236//
237void MLog::CheckFlag(Flags_t chk, int flag)
238{
239 if (flag==-1)
240 return;
241
242 flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
243}
Note: See TracBrowser for help on using the repository browser.