1 | #ifndef MARS_MLog
|
---|
2 | #define MARS_MLog
|
---|
3 |
|
---|
4 | #ifndef ROOT_TObject
|
---|
5 | #include <TObject.h>
|
---|
6 | #endif
|
---|
7 |
|
---|
8 | #include <iostream.h> // base classes for MLog
|
---|
9 |
|
---|
10 | #define bsz 160 // two standard lines
|
---|
11 |
|
---|
12 | enum ELogBits {
|
---|
13 | kHasChanged = BIT(14) // if gui has changed
|
---|
14 | };
|
---|
15 |
|
---|
16 |
|
---|
17 | class TGListBox;
|
---|
18 |
|
---|
19 | class MLog : public streambuf, public ostream, public TObject
|
---|
20 | {
|
---|
21 | public:
|
---|
22 | typedef enum _flags { eStdout = 0x1, eStderr = 0x2, eFile = 0x4, eGui = 0x8 } Flags_t;
|
---|
23 |
|
---|
24 | private:
|
---|
25 | char fBuffer; //!
|
---|
26 | char fBase[bsz]; //! Buffer to store the data in
|
---|
27 | char *fPPtr; //! Pointer to present position in buffer
|
---|
28 | const char *fEPtr; //! Pointer to end of buffer
|
---|
29 |
|
---|
30 | UInt_t fOutputLevel; //! Present output level of the stream
|
---|
31 | UInt_t fDebugLevel; //! Present global debug level
|
---|
32 | UInt_t fDevice; //! Flag to indicate the present streams
|
---|
33 |
|
---|
34 | Int_t fGuiLineId;
|
---|
35 |
|
---|
36 | ofstream *fout; //! possible file output stream
|
---|
37 | Bool_t fOutAllocated; //! flag if fout is created by MLogging
|
---|
38 | TGListBox *fgui; //! Listbox output
|
---|
39 |
|
---|
40 | void Init();
|
---|
41 |
|
---|
42 | void WriteBuffer();
|
---|
43 | int sync();
|
---|
44 | int overflow(int i); // i=EOF means not a real overflow
|
---|
45 |
|
---|
46 | void AllocateFile(const char *f);
|
---|
47 | void DeallocateFile();
|
---|
48 | void ReallocateFile(const char *f);
|
---|
49 | void CheckFlag(Flags_t chk, int flag);
|
---|
50 |
|
---|
51 | public:
|
---|
52 | MLog(int i=eStdout);
|
---|
53 | MLog(ofstream &out);
|
---|
54 | MLog(TGListBox &out);
|
---|
55 | MLog(const char *fname, int flag=-1);
|
---|
56 |
|
---|
57 | MLog(MLog &log);
|
---|
58 |
|
---|
59 | ~MLog()
|
---|
60 | {
|
---|
61 | DeallocateFile();
|
---|
62 | }
|
---|
63 |
|
---|
64 | void SetDebugLevel(int i) { fDebugLevel = i; }
|
---|
65 | void SetOutputLevel(int i) { fOutputLevel = i; }
|
---|
66 | void SetOutputDevice(int i) { fDevice = i; }
|
---|
67 | void EnableOutputDevice(Flags_t f) { fDevice |= f; }
|
---|
68 | void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
|
---|
69 | void operator=(ofstream &out) { SetOutputFile(out); }
|
---|
70 | void operator=(TGListBox *out) { SetOutputGui(out); }
|
---|
71 |
|
---|
72 | Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
|
---|
73 |
|
---|
74 | void SetOutputGui(TGListBox *out, int flag=-1)
|
---|
75 | {
|
---|
76 | fgui = out;
|
---|
77 | CheckFlag(eGui, flag);
|
---|
78 | }
|
---|
79 |
|
---|
80 | void SetOutputFile(ofstream &out, int flag=-1)
|
---|
81 | {
|
---|
82 | //
|
---|
83 | // Set new output file by a given stream. The new output
|
---|
84 | // file is not deleted automatically. If no flag is specified
|
---|
85 | // the state of the file-device stream is unchanged.
|
---|
86 | // if the a flag is specified the state is changed
|
---|
87 | // in correspondance to the flag
|
---|
88 | //
|
---|
89 | DeallocateFile();
|
---|
90 | fout = &out;
|
---|
91 | CheckFlag(eFile, flag);
|
---|
92 | }
|
---|
93 |
|
---|
94 | void SetOutputFile(const char *f=NULL, int flag=-1)
|
---|
95 | {
|
---|
96 | //
|
---|
97 | // Set new output file by name. The new output file must
|
---|
98 | // not be deleted by the user. If no flag is specified
|
---|
99 | // the state of the file-device stream is unchanged.
|
---|
100 | // if the a flag is specified the state is changed
|
---|
101 | // in correspondance to the flag
|
---|
102 | //
|
---|
103 | ReallocateFile(f);
|
---|
104 | CheckFlag(eFile, flag);
|
---|
105 | }
|
---|
106 |
|
---|
107 | ofstream &GetOutputFile()
|
---|
108 | {
|
---|
109 | //
|
---|
110 | // Get the file output stream from MLogging
|
---|
111 | // if no file output stream is existing yet it will be created.
|
---|
112 | // For the creating a C-Temporary file name is used.
|
---|
113 | // if the stream is created here the user must not delete it
|
---|
114 | //
|
---|
115 | // What a pitty, but it seems, that there is now way to ask
|
---|
116 | // an ofstream for the corresponding file name. Elsewhise
|
---|
117 | // I would implement a GetFileName-function, too.
|
---|
118 | //
|
---|
119 | if (!fout)
|
---|
120 | ReallocateFile(NULL);
|
---|
121 | return *fout;
|
---|
122 | }
|
---|
123 |
|
---|
124 | ClassDef(MLog, 0) // This is what we call 'The logging system'
|
---|
125 | };
|
---|
126 |
|
---|
127 | #endif
|
---|