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