1 | #ifndef MARS_MLog
|
---|
2 | #define MARS_MLog
|
---|
3 |
|
---|
4 | #ifndef ROOT_TObject
|
---|
5 | #include <TObject.h>
|
---|
6 | #endif
|
---|
7 | #ifndef ROOT_TString
|
---|
8 | #include <TString.h>
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | #ifndef __CINT__
|
---|
12 | #include <iostream.h> // base classes for MLog
|
---|
13 | #else
|
---|
14 | class streambuf;
|
---|
15 | class ostream;
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #define bsz 160 // two standard lines
|
---|
19 |
|
---|
20 | class TGTextView;
|
---|
21 |
|
---|
22 | class MLog : public streambuf, public ostream, public TObject
|
---|
23 | {
|
---|
24 | public:
|
---|
25 | typedef enum _flags {
|
---|
26 | eStdout = 0x001,
|
---|
27 | eStderr = 0x002,
|
---|
28 | eFile = 0x004,
|
---|
29 | eGui = 0x008,
|
---|
30 | eNoColors = 0x400 //BIT(15)
|
---|
31 | } Flags_t;
|
---|
32 |
|
---|
33 | enum ELogBits {
|
---|
34 | kHasChanged = BIT(14) // if gui has changed
|
---|
35 | };
|
---|
36 |
|
---|
37 | private:
|
---|
38 | static const char kESC;
|
---|
39 | static const char *const kEsc;
|
---|
40 | static const char *const kReset;
|
---|
41 | static const char *const kRed;
|
---|
42 | static const char *const kGreen;
|
---|
43 | static const char *const kYellow;
|
---|
44 | static const char *const kUnderline;
|
---|
45 | static const char *const kBlink;
|
---|
46 | static const char *const kBright;
|
---|
47 | static const char *const kDark;
|
---|
48 |
|
---|
49 | char fBuffer; //!
|
---|
50 | char fBase[bsz+1]; //! Buffer to store the data in
|
---|
51 | char *fPPtr; //! Pointer to present position in buffer
|
---|
52 | const char *fEPtr; //! Pointer to end of buffer
|
---|
53 |
|
---|
54 | UInt_t fOutputLevel; //! Present output level of the stream
|
---|
55 | UInt_t fDebugLevel; //! Present global debug level
|
---|
56 | UInt_t fDevice; //! Flag to indicate the present streams
|
---|
57 |
|
---|
58 | Bool_t fIsNull; //! Switch output completely off
|
---|
59 |
|
---|
60 | ofstream *fout; //! possible file output stream
|
---|
61 | Bool_t fOutAllocated; //! flag if fout is created by MLogging
|
---|
62 | TGTextView *fgui; //! Text View output
|
---|
63 |
|
---|
64 | Bool_t fIsDirectGui; //! Pipe text directly to the GUI (for single threaded environments)
|
---|
65 | TString **fGuiLines; //! Lines to pipe to gui
|
---|
66 | Int_t fNumLines; //!
|
---|
67 | TString fGuiLine; //!
|
---|
68 |
|
---|
69 | #ifdef _REENTRANT
|
---|
70 | void *fMuxGui; //! Mutex locking access of TGListBox
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | void Init();
|
---|
74 |
|
---|
75 | void WriteBuffer();
|
---|
76 | int sync();
|
---|
77 | int overflow(int i); // i=EOF means not a real overflow
|
---|
78 |
|
---|
79 | void AllocateFile(const char *f);
|
---|
80 | void DeallocateFile();
|
---|
81 | void ReallocateFile(const char *f);
|
---|
82 | void CheckFlag(Flags_t chk, int flag);
|
---|
83 | void Output(ostream &out, int len);
|
---|
84 |
|
---|
85 | public:
|
---|
86 | MLog(int i=eStdout);
|
---|
87 | MLog(ofstream &out);
|
---|
88 | MLog(TGTextView &out);
|
---|
89 | MLog(const char *fname, int flag=-1);
|
---|
90 |
|
---|
91 | MLog(MLog &log);
|
---|
92 | ~MLog();
|
---|
93 |
|
---|
94 | void Lock();
|
---|
95 | void UnLock();
|
---|
96 |
|
---|
97 | void EnableDirectGui() { fIsDirectGui = kTRUE; }
|
---|
98 | void DisableDirectGui() { fIsDirectGui = kFALSE; }
|
---|
99 | void UpdateGui();
|
---|
100 |
|
---|
101 | void Underline();
|
---|
102 |
|
---|
103 | void SetDebugLevel(int i) { fDebugLevel = i; }
|
---|
104 | int GetDebugLevel() const { return fDebugLevel; }
|
---|
105 | void SetOutputLevel(int i) { fOutputLevel = i; }
|
---|
106 | void SetOutputDevice(int i) { fDevice = i; }
|
---|
107 | void EnableOutputDevice(Flags_t f) { fDevice |= f; }
|
---|
108 | void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
|
---|
109 | void operator=(ofstream &out) { SetOutputFile(out); }
|
---|
110 | void operator=(TGTextView *out) { SetOutputGui(out); }
|
---|
111 |
|
---|
112 | Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
|
---|
113 |
|
---|
114 | void SetOutputGui(TGTextView *out, int flag=-1)
|
---|
115 | {
|
---|
116 | fgui = out;
|
---|
117 | CheckFlag(eGui, flag);
|
---|
118 | }
|
---|
119 |
|
---|
120 | void SetOutputFile(ofstream &out, int flag=-1)
|
---|
121 | {
|
---|
122 | //
|
---|
123 | // Set new output file by a given stream. The new output
|
---|
124 | // file is not deleted automatically. If no flag is specified
|
---|
125 | // the state of the file-device stream is unchanged.
|
---|
126 | // if the a flag is specified the state is changed
|
---|
127 | // in correspondance to the flag
|
---|
128 | //
|
---|
129 | DeallocateFile();
|
---|
130 | fout = &out;
|
---|
131 | CheckFlag(eFile, flag);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void SetOutputFile(const char *f=NULL, int flag=-1)
|
---|
135 | {
|
---|
136 | //
|
---|
137 | // Set new output file by name. The new output file must
|
---|
138 | // not be deleted by the user. If no flag is specified
|
---|
139 | // the state of the file-device stream is unchanged.
|
---|
140 | // if the a flag is specified the state is changed
|
---|
141 | // in correspondance to the flag
|
---|
142 | //
|
---|
143 | ReallocateFile(f);
|
---|
144 | CheckFlag(eFile, flag);
|
---|
145 | }
|
---|
146 |
|
---|
147 | ofstream &GetOutputFile()
|
---|
148 | {
|
---|
149 | //
|
---|
150 | // Get the file output stream from MLogging
|
---|
151 | // if no file output stream is existing yet it will be created.
|
---|
152 | // For the creating a C-Temporary file name is used.
|
---|
153 | // if the stream is created here the user must not delete it
|
---|
154 | //
|
---|
155 | // What a pitty, but it seems, that there is now way to ask
|
---|
156 | // an ofstream for the corresponding file name. Elsewhise
|
---|
157 | // I would implement a GetFileName-function, too.
|
---|
158 | //
|
---|
159 | if (!fout)
|
---|
160 | ReallocateFile(NULL);
|
---|
161 | return *fout;
|
---|
162 | }
|
---|
163 |
|
---|
164 | void SetNullOutput(Bool_t n=kTRUE) { fIsNull = n; }
|
---|
165 |
|
---|
166 | void SetNoColors(Bool_t flag=kTRUE) { flag ? SetBit(eNoColors) : ResetBit(eNoColors); }
|
---|
167 |
|
---|
168 | ClassDef(MLog, 0) // This is what we call 'The logging system'
|
---|
169 | };
|
---|
170 |
|
---|
171 | #endif
|
---|