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 | // This is the function which writes the stream physically to a device.
|
---|
115 | // If you want to add a new device this must be done here.
|
---|
116 | //
|
---|
117 | void MLog::WriteBuffer()
|
---|
118 | {
|
---|
119 | const int len = fPPtr - fBase;
|
---|
120 |
|
---|
121 | if (fDevice&eStdout)
|
---|
122 | cout.write(fBase, len);
|
---|
123 |
|
---|
124 | if (fDevice&eStderr)
|
---|
125 | cerr.write(fBase, len);
|
---|
126 |
|
---|
127 | if (fDevice&eFile && fout)
|
---|
128 | fout->write(fBase, len);
|
---|
129 |
|
---|
130 | if (fDevice&eGui && fgui)
|
---|
131 | {
|
---|
132 | char dummy[bsz+1];
|
---|
133 | memcpy(dummy, fBase, bsz);
|
---|
134 | *(dummy+bsz)='\0';
|
---|
135 | fgui->AddEntry(dummy, -1);
|
---|
136 | }
|
---|
137 |
|
---|
138 | //
|
---|
139 | // restart writing to the buffer at its first char
|
---|
140 | //
|
---|
141 | fPPtr = fBase;
|
---|
142 | }
|
---|
143 |
|
---|
144 | // --------------------------------------------------------------------------
|
---|
145 | //
|
---|
146 | // This is called to flush the buffer of the streaming devices
|
---|
147 | //
|
---|
148 | int MLog::sync()
|
---|
149 | {
|
---|
150 | WriteBuffer();
|
---|
151 |
|
---|
152 | if (fDevice&eStdout)
|
---|
153 | cout.flush();
|
---|
154 |
|
---|
155 | if (fDevice&eStderr)
|
---|
156 | cerr.flush();
|
---|
157 |
|
---|
158 | if (fDevice&eFile && fout)
|
---|
159 | fout->flush();
|
---|
160 |
|
---|
161 | return 0;
|
---|
162 | }
|
---|
163 |
|
---|
164 | // --------------------------------------------------------------------------
|
---|
165 | //
|
---|
166 | // This function comes from streambuf and should
|
---|
167 | // output the buffer to the device (flush, endl)
|
---|
168 | // or handle a buffer overflow (too many chars)
|
---|
169 | // If a real overflow happens i contains the next
|
---|
170 | // chars which doesn't fit into the buffer anymore.
|
---|
171 | // If the buffer is not really filled i is EOF(-1).
|
---|
172 | //
|
---|
173 | int MLog::overflow(int i) // i=EOF means not a real overflow
|
---|
174 | {
|
---|
175 | if (fOutputLevel >= fDebugLevel)
|
---|
176 | return 0;
|
---|
177 |
|
---|
178 | *fPPtr++ = (char)i;
|
---|
179 |
|
---|
180 | if (fPPtr == fEPtr)
|
---|
181 | WriteBuffer();
|
---|
182 |
|
---|
183 | return 0;
|
---|
184 | }
|
---|
185 |
|
---|
186 | // --------------------------------------------------------------------------
|
---|
187 | //
|
---|
188 | // Create a new instance of an file output stream
|
---|
189 | // an set the corresponding flag
|
---|
190 | //
|
---|
191 | void MLog::AllocateFile(const char *fname)
|
---|
192 | {
|
---|
193 | fout = new ofstream(fname);
|
---|
194 | fOutAllocated = kTRUE;
|
---|
195 | }
|
---|
196 |
|
---|
197 | // --------------------------------------------------------------------------
|
---|
198 | //
|
---|
199 | // if fout was allocated by this instance of MLooging
|
---|
200 | // delete it.
|
---|
201 | //
|
---|
202 | void MLog::DeallocateFile()
|
---|
203 | {
|
---|
204 | if (fOutAllocated)
|
---|
205 | delete fout;
|
---|
206 | }
|
---|
207 |
|
---|
208 | // --------------------------------------------------------------------------
|
---|
209 | //
|
---|
210 | // if necessary delete the old in stance of the file
|
---|
211 | // output stream and create a new one
|
---|
212 | //
|
---|
213 | void MLog::ReallocateFile(const char *fname)
|
---|
214 | {
|
---|
215 | DeallocateFile();
|
---|
216 | AllocateFile(fname);
|
---|
217 | }
|
---|
218 |
|
---|
219 | // --------------------------------------------------------------------------
|
---|
220 | //
|
---|
221 | // This function checks if a device should get enabled or disabled.
|
---|
222 | //
|
---|
223 | void MLog::CheckFlag(Flags_t chk, int flag)
|
---|
224 | {
|
---|
225 | if (flag==-1)
|
---|
226 | return;
|
---|
227 |
|
---|
228 | flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
|
---|
229 | }
|
---|