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 |
|
---|
50 | ClassImp(MLog);
|
---|
51 |
|
---|
52 | //
|
---|
53 | // This is the definition of the global log facility
|
---|
54 | //
|
---|
55 | MLog 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 | //
|
---|
62 | void 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 | //
|
---|
73 | MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), 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 | //
|
---|
83 | MLog::MLog(ofstream &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), 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 | //
|
---|
93 | MLog::MLog(TGListBox &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), 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 | //
|
---|
104 | MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fgui(NULL)
|
---|
105 | {
|
---|
106 | Init();
|
---|
107 |
|
---|
108 | AllocateFile(fname);
|
---|
109 | CheckFlag(eFile, flag);
|
---|
110 | }
|
---|
111 |
|
---|
112 | // --------------------------------------------------------------------------
|
---|
113 | //
|
---|
114 | // copyt constructor
|
---|
115 | //
|
---|
116 | MLog::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 | //
|
---|
128 | void 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[bsz+1];
|
---|
144 | memcpy(dummy, fBase, bsz);
|
---|
145 | *(dummy+bsz)='\0';
|
---|
146 | fgui->AddEntry(dummy, -1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | //
|
---|
150 | // restart writing to the buffer at its first char
|
---|
151 | //
|
---|
152 | fPPtr = fBase;
|
---|
153 | }
|
---|
154 |
|
---|
155 | // --------------------------------------------------------------------------
|
---|
156 | //
|
---|
157 | // This is called to flush the buffer of the streaming devices
|
---|
158 | //
|
---|
159 | int MLog::sync()
|
---|
160 | {
|
---|
161 | WriteBuffer();
|
---|
162 |
|
---|
163 | if (fDevice&eStdout)
|
---|
164 | cout.flush();
|
---|
165 |
|
---|
166 | if (fDevice&eStderr)
|
---|
167 | cerr.flush();
|
---|
168 |
|
---|
169 | if (fDevice&eFile && fout)
|
---|
170 | fout->flush();
|
---|
171 |
|
---|
172 | return 0;
|
---|
173 | }
|
---|
174 |
|
---|
175 | // --------------------------------------------------------------------------
|
---|
176 | //
|
---|
177 | // This function comes from streambuf and should
|
---|
178 | // output the buffer to the device (flush, endl)
|
---|
179 | // or handle a buffer overflow (too many chars)
|
---|
180 | // If a real overflow happens i contains the next
|
---|
181 | // chars which doesn't fit into the buffer anymore.
|
---|
182 | // If the buffer is not really filled i is EOF(-1).
|
---|
183 | //
|
---|
184 | int MLog::overflow(int i) // i=EOF means not a real overflow
|
---|
185 | {
|
---|
186 | if (fOutputLevel >= fDebugLevel)
|
---|
187 | return 0;
|
---|
188 |
|
---|
189 | *fPPtr++ = (char)i;
|
---|
190 |
|
---|
191 | if (fPPtr == fEPtr)
|
---|
192 | WriteBuffer();
|
---|
193 |
|
---|
194 | return 0;
|
---|
195 | }
|
---|
196 |
|
---|
197 | // --------------------------------------------------------------------------
|
---|
198 | //
|
---|
199 | // Create a new instance of an file output stream
|
---|
200 | // an set the corresponding flag
|
---|
201 | //
|
---|
202 | void MLog::AllocateFile(const char *fname)
|
---|
203 | {
|
---|
204 | fout = new ofstream(fname);
|
---|
205 | fOutAllocated = kTRUE;
|
---|
206 | }
|
---|
207 |
|
---|
208 | // --------------------------------------------------------------------------
|
---|
209 | //
|
---|
210 | // if fout was allocated by this instance of MLooging
|
---|
211 | // delete it.
|
---|
212 | //
|
---|
213 | void MLog::DeallocateFile()
|
---|
214 | {
|
---|
215 | if (fOutAllocated)
|
---|
216 | delete fout;
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // if necessary delete the old in stance of the file
|
---|
222 | // output stream and create a new one
|
---|
223 | //
|
---|
224 | void MLog::ReallocateFile(const char *fname)
|
---|
225 | {
|
---|
226 | DeallocateFile();
|
---|
227 | AllocateFile(fname);
|
---|
228 | }
|
---|
229 |
|
---|
230 | // --------------------------------------------------------------------------
|
---|
231 | //
|
---|
232 | // This function checks if a device should get enabled or disabled.
|
---|
233 | //
|
---|
234 | void MLog::CheckFlag(Flags_t chk, int flag)
|
---|
235 | {
|
---|
236 | if (flag==-1)
|
---|
237 | return;
|
---|
238 |
|
---|
239 | flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
|
---|
240 | }
|
---|