| 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 | Bool_t fIsNull; //! Switch output completely off
|
|---|
| 35 |
|
|---|
| 36 | Int_t fGuiLineId;
|
|---|
| 37 |
|
|---|
| 38 | ofstream *fout; //! possible file output stream
|
|---|
| 39 | Bool_t fOutAllocated; //! flag if fout is created by MLogging
|
|---|
| 40 | TGListBox *fgui; //! Listbox output
|
|---|
| 41 |
|
|---|
| 42 | void Init();
|
|---|
| 43 |
|
|---|
| 44 | void WriteBuffer();
|
|---|
| 45 | int sync();
|
|---|
| 46 | int overflow(int i); // i=EOF means not a real overflow
|
|---|
| 47 |
|
|---|
| 48 | void AllocateFile(const char *f);
|
|---|
| 49 | void DeallocateFile();
|
|---|
| 50 | void ReallocateFile(const char *f);
|
|---|
| 51 | void CheckFlag(Flags_t chk, int flag);
|
|---|
| 52 |
|
|---|
| 53 | public:
|
|---|
| 54 | MLog(int i=eStdout);
|
|---|
| 55 | MLog(ofstream &out);
|
|---|
| 56 | MLog(TGListBox &out);
|
|---|
| 57 | MLog(const char *fname, int flag=-1);
|
|---|
| 58 |
|
|---|
| 59 | MLog(MLog &log);
|
|---|
| 60 |
|
|---|
| 61 | ~MLog()
|
|---|
| 62 | {
|
|---|
| 63 | DeallocateFile();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | void SetDebugLevel(int i) { fDebugLevel = i; }
|
|---|
| 67 | void SetOutputLevel(int i) { fOutputLevel = i; }
|
|---|
| 68 | void SetOutputDevice(int i) { fDevice = i; }
|
|---|
| 69 | void EnableOutputDevice(Flags_t f) { fDevice |= f; }
|
|---|
| 70 | void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
|
|---|
| 71 | void operator=(ofstream &out) { SetOutputFile(out); }
|
|---|
| 72 | void operator=(TGListBox *out) { SetOutputGui(out); }
|
|---|
| 73 |
|
|---|
| 74 | Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
|
|---|
| 75 |
|
|---|
| 76 | void SetOutputGui(TGListBox *out, int flag=-1)
|
|---|
| 77 | {
|
|---|
| 78 | fgui = out;
|
|---|
| 79 | CheckFlag(eGui, flag);
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | void SetOutputFile(ofstream &out, int flag=-1)
|
|---|
| 83 | {
|
|---|
| 84 | //
|
|---|
| 85 | // Set new output file by a given stream. The new output
|
|---|
| 86 | // file is not deleted automatically. If no flag is specified
|
|---|
| 87 | // the state of the file-device stream is unchanged.
|
|---|
| 88 | // if the a flag is specified the state is changed
|
|---|
| 89 | // in correspondance to the flag
|
|---|
| 90 | //
|
|---|
| 91 | DeallocateFile();
|
|---|
| 92 | fout = &out;
|
|---|
| 93 | CheckFlag(eFile, flag);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void SetOutputFile(const char *f=NULL, int flag=-1)
|
|---|
| 97 | {
|
|---|
| 98 | //
|
|---|
| 99 | // Set new output file by name. The new output file must
|
|---|
| 100 | // not be deleted by the user. If no flag is specified
|
|---|
| 101 | // the state of the file-device stream is unchanged.
|
|---|
| 102 | // if the a flag is specified the state is changed
|
|---|
| 103 | // in correspondance to the flag
|
|---|
| 104 | //
|
|---|
| 105 | ReallocateFile(f);
|
|---|
| 106 | CheckFlag(eFile, flag);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | ofstream &GetOutputFile()
|
|---|
| 110 | {
|
|---|
| 111 | //
|
|---|
| 112 | // Get the file output stream from MLogging
|
|---|
| 113 | // if no file output stream is existing yet it will be created.
|
|---|
| 114 | // For the creating a C-Temporary file name is used.
|
|---|
| 115 | // if the stream is created here the user must not delete it
|
|---|
| 116 | //
|
|---|
| 117 | // What a pitty, but it seems, that there is now way to ask
|
|---|
| 118 | // an ofstream for the corresponding file name. Elsewhise
|
|---|
| 119 | // I would implement a GetFileName-function, too.
|
|---|
| 120 | //
|
|---|
| 121 | if (!fout)
|
|---|
| 122 | ReallocateFile(NULL);
|
|---|
| 123 | return *fout;
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 | void SetNullOutput(Bool_t n=kTRUE) { fIsNull = n; }
|
|---|
| 127 |
|
|---|
| 128 | ClassDef(MLog, 0) // This is what we call 'The logging system'
|
|---|
| 129 | };
|
|---|
| 130 |
|
|---|
| 131 | #endif
|
|---|