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