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