1 | #include "MLog.h"
|
---|
2 |
|
---|
3 | #include <fstream.h>
|
---|
4 | #include <TGListBox.h>
|
---|
5 |
|
---|
6 | #include "MLogManip.h"
|
---|
7 |
|
---|
8 | ClassImp(MLog)
|
---|
9 |
|
---|
10 | //
|
---|
11 | // This is the definition of the global log facility
|
---|
12 | //
|
---|
13 | MLog gLog;
|
---|
14 |
|
---|
15 | void MLog::Init()
|
---|
16 | {
|
---|
17 | //
|
---|
18 | // this strange usage of an unbufferd buffer is a workaround
|
---|
19 | // to make it work on Alpha and Linux!
|
---|
20 | //
|
---|
21 | setp(&fBuffer, &fBuffer+1);
|
---|
22 | *this << '\0';
|
---|
23 | }
|
---|
24 |
|
---|
25 | MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), fout(NULL), fOutAllocated(kFALSE), fgui(NULL)
|
---|
26 | {
|
---|
27 | Init();
|
---|
28 | }
|
---|
29 |
|
---|
30 | MLog::MLog(ofstream &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fout(&out), fOutAllocated(kFALSE), fgui(NULL)
|
---|
31 | {
|
---|
32 | Init();
|
---|
33 | }
|
---|
34 |
|
---|
35 | MLog::MLog(TGListBox &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), fout(NULL), fOutAllocated(kFALSE), fgui(&out)
|
---|
36 | {
|
---|
37 | Init();
|
---|
38 | }
|
---|
39 |
|
---|
40 | MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fgui(NULL)
|
---|
41 | {
|
---|
42 | Init();
|
---|
43 |
|
---|
44 | AllocateFile(fname);
|
---|
45 | CheckFlag(eFile, flag);
|
---|
46 | }
|
---|
47 |
|
---|
48 | void MLog::Write()
|
---|
49 | {
|
---|
50 | const int len = fPPtr - fBase;
|
---|
51 |
|
---|
52 | if (fDevice&eStdout)
|
---|
53 | cout.write(fBase, len);
|
---|
54 |
|
---|
55 | if (fDevice&eStderr)
|
---|
56 | cerr.write(fBase, len);
|
---|
57 |
|
---|
58 | if (fDevice&eFile && fout)
|
---|
59 | fout->write(fBase, len);
|
---|
60 |
|
---|
61 | if (fDevice&eGui && fgui)
|
---|
62 | {
|
---|
63 | char dummy[bsz+1];
|
---|
64 | memcpy(dummy, fBase, bsz);
|
---|
65 | *(dummy+bsz)='\0';
|
---|
66 | fgui->AddEntry(dummy, -1);
|
---|
67 | }
|
---|
68 |
|
---|
69 | //
|
---|
70 | // restart writing to the buffer at its first char
|
---|
71 | //
|
---|
72 | fPPtr = fBase;
|
---|
73 | }
|
---|
74 |
|
---|
75 | int MLog::sync()
|
---|
76 | {
|
---|
77 | // cout << "*sync*";
|
---|
78 | Write();
|
---|
79 |
|
---|
80 | if (fDevice&eStdout)
|
---|
81 | cout.flush();
|
---|
82 |
|
---|
83 | if (fDevice&eStderr)
|
---|
84 | cerr.flush();
|
---|
85 |
|
---|
86 | if (fDevice&eFile && fout)
|
---|
87 | fout->flush();
|
---|
88 |
|
---|
89 | return 0;
|
---|
90 | }
|
---|
91 |
|
---|
92 | int MLog::overflow(int i) // i=EOF means not a real overflow
|
---|
93 | {
|
---|
94 | //
|
---|
95 | // This function comes from streambuf and should
|
---|
96 | // output the buffer to the device (flush, endl)
|
---|
97 | // or handle a buffer overflow (too many chars)
|
---|
98 | // If a real overflow happens i contains the next
|
---|
99 | // chars which doesn't fit into the buffer anymore.
|
---|
100 | // If the buffer is not really filled i is EOF(-1).
|
---|
101 | //
|
---|
102 | if (fOutputLevel < fDebugLevel)
|
---|
103 | {
|
---|
104 | *fPPtr++ = (char)i;
|
---|
105 |
|
---|
106 | if (fPPtr == fEPtr)
|
---|
107 | Write();
|
---|
108 | }
|
---|
109 |
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | void MLog::AllocateFile(const char *fname)
|
---|
114 | {
|
---|
115 | //
|
---|
116 | // Create a new instance of an file output stream
|
---|
117 | // an set the corresponding flag
|
---|
118 | //
|
---|
119 | fout = new ofstream(fname);
|
---|
120 | fOutAllocated = kTRUE;
|
---|
121 | }
|
---|
122 |
|
---|
123 | void MLog::DeallocateFile()
|
---|
124 | {
|
---|
125 | //
|
---|
126 | // if fout was allocated by this instance of MLooging
|
---|
127 | // delete it.
|
---|
128 | //
|
---|
129 | if (fOutAllocated)
|
---|
130 | delete fout;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void MLog::ReallocateFile(const char *fname)
|
---|
134 | {
|
---|
135 | //
|
---|
136 | // if necessary delete the old in stance of the file
|
---|
137 | // output stream and create a new one
|
---|
138 | //
|
---|
139 | DeallocateFile();
|
---|
140 | AllocateFile(fname);
|
---|
141 | }
|
---|
142 |
|
---|
143 | void MLog::CheckFlag(Flags_t chk, int flag)
|
---|
144 | {
|
---|
145 | if (flag==-1)
|
---|
146 | return;
|
---|
147 |
|
---|
148 | flag ? EnableOutputDevice(chk) : DisableOutputDevice(chk);
|
---|
149 | }
|
---|