| 1 | /* ======================================================================== *\
|
|---|
| 2 | ! $Name: not supported by cvs2svn $:$Id: MLog.h,v 1.37 2007-02-13 11:06:53 tbretz Exp $
|
|---|
| 3 | \* ======================================================================== */
|
|---|
| 4 | #ifndef MARS_MLog
|
|---|
| 5 | #define MARS_MLog
|
|---|
| 6 |
|
|---|
| 7 | #ifndef ROOT_TObject
|
|---|
| 8 | #include <TObject.h>
|
|---|
| 9 | #endif
|
|---|
| 10 | #ifndef ROOT_TString
|
|---|
| 11 | #include <TString.h>
|
|---|
| 12 | #endif
|
|---|
| 13 | #ifndef ROOT_TSystem
|
|---|
| 14 | #include <TSystem.h>
|
|---|
| 15 | #endif
|
|---|
| 16 |
|
|---|
| 17 | #include <iostream> // base classes for MLog
|
|---|
| 18 |
|
|---|
| 19 | class MArgs;
|
|---|
| 20 |
|
|---|
| 21 | class TEnv;
|
|---|
| 22 | class TMutex;
|
|---|
| 23 | class TGTextView;
|
|---|
| 24 |
|
|---|
| 25 | class MLogPlugin;
|
|---|
| 26 |
|
|---|
| 27 | class MLog : public std::streambuf, public std::ostream, public TObject
|
|---|
| 28 | {
|
|---|
| 29 | public:
|
|---|
| 30 | typedef enum _flags {
|
|---|
| 31 | eStdout = 0x001,
|
|---|
| 32 | eStderr = 0x002,
|
|---|
| 33 | eFile = 0x004,
|
|---|
| 34 | eGui = 0x008,
|
|---|
| 35 | eNoColors = 0x400 //BIT(15)
|
|---|
| 36 | } Flags_t;
|
|---|
| 37 |
|
|---|
| 38 | enum ELogBits {
|
|---|
| 39 | kHasChanged = BIT(14) // if gui has changed
|
|---|
| 40 | };
|
|---|
| 41 |
|
|---|
| 42 | enum ELogType {
|
|---|
| 43 | kDefault,
|
|---|
| 44 | kColor,
|
|---|
| 45 | kBlackWhite
|
|---|
| 46 | };
|
|---|
| 47 |
|
|---|
| 48 | private:
|
|---|
| 49 | enum {
|
|---|
| 50 | kIsUnderlined = BIT(15)//, kIsAutoIntro = BIT(16)
|
|---|
| 51 | };
|
|---|
| 52 | static const char kESC;
|
|---|
| 53 | static const char *const kEsc;
|
|---|
| 54 | static const char *const kReset;
|
|---|
| 55 | static const char *const kRed;
|
|---|
| 56 | static const char *const kGreen;
|
|---|
| 57 | static const char *const kYellow;
|
|---|
| 58 | static const char *const kBlue;
|
|---|
| 59 | static const char *const kUnderline;
|
|---|
| 60 | static const char *const kBlink;
|
|---|
| 61 | static const char *const kBright;
|
|---|
| 62 | static const char *const kDark;
|
|---|
| 63 |
|
|---|
| 64 | static const int fgBufferSize = 160; // two standard lines
|
|---|
| 65 |
|
|---|
| 66 | char fBuffer; //!
|
|---|
| 67 | char fBase[fgBufferSize+1]; //! Buffer to store the data in
|
|---|
| 68 | char *fPPtr; //! Pointer to present position in buffer
|
|---|
| 69 | const char *fEPtr; //! Pointer to end of buffer
|
|---|
| 70 |
|
|---|
| 71 | UInt_t fOutputLevel; //! Present output level of the stream
|
|---|
| 72 | UInt_t fDebugLevel; //! Present global debug level
|
|---|
| 73 | UInt_t fDevice; //! Flag to indicate the present streams
|
|---|
| 74 |
|
|---|
| 75 | Bool_t fIsNull; //! Switch output completely off
|
|---|
| 76 |
|
|---|
| 77 | ofstream *fOut; //! possible file output stream
|
|---|
| 78 | Bool_t fOutAllocated; //! flag if fout is created by MLogging
|
|---|
| 79 | TGTextView *fGui; //! Text View output
|
|---|
| 80 |
|
|---|
| 81 | Bool_t fIsDirectGui; //! Pipe text directly to the GUI (for single threaded environments)
|
|---|
| 82 | TString **fGuiLines; //! Lines to pipe to gui
|
|---|
| 83 | Int_t fNumLines; //!
|
|---|
| 84 | TString fGuiLine; //!
|
|---|
| 85 |
|
|---|
| 86 | #ifdef _REENTRANT
|
|---|
| 87 | TMutex *fMuxGui; //! Mutex locking access of TGListBox
|
|---|
| 88 | TMutex *fMuxStream; //! Mutex locking access to streaming
|
|---|
| 89 | #endif
|
|---|
| 90 |
|
|---|
| 91 | TList *fPlugins;
|
|---|
| 92 |
|
|---|
| 93 | void Init();
|
|---|
| 94 |
|
|---|
| 95 | void WriteBuffer();
|
|---|
| 96 | int sync();
|
|---|
| 97 | int overflow(int i); // i=EOF means not a real overflow
|
|---|
| 98 |
|
|---|
| 99 | void AllocateFile(const char *f);
|
|---|
| 100 | void DeallocateFile();
|
|---|
| 101 | void ReallocateFile(const char *f);
|
|---|
| 102 | void CheckFlag(Flags_t chk, int flag);
|
|---|
| 103 | void Output(ostream &out, int len);
|
|---|
| 104 | void AddGuiLine(const TString& line);
|
|---|
| 105 |
|
|---|
| 106 | // User defined error handling (see TError.h)
|
|---|
| 107 | static bool ErrorHandlerIgnore(Int_t level);
|
|---|
| 108 | static void ErrorHandlerPrint(Int_t level, const char *location, const char *msg);
|
|---|
| 109 | static void ErrorHandlerCol(Int_t level, Bool_t abort, const char *location, const char *msg);
|
|---|
| 110 | static void ErrorHandlerAll(Int_t level, Bool_t abort, const char *location, const char *msg);
|
|---|
| 111 |
|
|---|
| 112 | public:
|
|---|
| 113 |
|
|---|
| 114 | MLog(int i=eStdout);
|
|---|
| 115 | MLog(ofstream &out);
|
|---|
| 116 | MLog(TGTextView &out);
|
|---|
| 117 | MLog(const char *fname, int flag=-1);
|
|---|
| 118 |
|
|---|
| 119 | MLog(MLog const& log) : std::ios(), std::streambuf(), ostream((std::streambuf*)&log), TObject()
|
|---|
| 120 | {
|
|---|
| 121 | fOutputLevel = log.fOutputLevel;
|
|---|
| 122 | fDebugLevel = log.fDebugLevel;
|
|---|
| 123 | fDevice = log.fDevice;
|
|---|
| 124 | }
|
|---|
| 125 | ~MLog();
|
|---|
| 126 |
|
|---|
| 127 | static TString Intro();
|
|---|
| 128 | static void RedirectErrorHandler(ELogType typ=kColor);
|
|---|
| 129 |
|
|---|
| 130 | bool LockUpdate(const char *msg);
|
|---|
| 131 | bool UnLockUpdate(const char *msg);
|
|---|
| 132 |
|
|---|
| 133 | bool Lock(const char *msg="");
|
|---|
| 134 | bool UnLock(const char *msg="");
|
|---|
| 135 |
|
|---|
| 136 | void EnableDirectGui() { fIsDirectGui = kTRUE; }
|
|---|
| 137 | void DisableDirectGui() { fIsDirectGui = kFALSE; }
|
|---|
| 138 | void UpdateGui();
|
|---|
| 139 |
|
|---|
| 140 | void Underline();
|
|---|
| 141 |
|
|---|
| 142 | void SetDebugLevel(int i) { fDebugLevel = i; }
|
|---|
| 143 | int GetDebugLevel() const { return fDebugLevel; }
|
|---|
| 144 | void SetOutputLevel(int i) { fOutputLevel = i; }
|
|---|
| 145 | void SetOutputDevice(int i) { fDevice = i; }
|
|---|
| 146 | void EnableOutputDevice(Flags_t f) { fDevice |= f; }
|
|---|
| 147 | void DisableOutputDevice(Flags_t f) { fDevice &= ~f; }
|
|---|
| 148 | void operator=(ofstream &sout) { SetOutputFile(sout); }
|
|---|
| 149 | void operator=(TGTextView *sout) { SetOutputGui(sout); }
|
|---|
| 150 | //void SetAutoIntro(Bool_t b=kTRUE) { b ? SetBit(kIsAutoIntro) : SetBit(kIsAutoIntro); }
|
|---|
| 151 |
|
|---|
| 152 | Bool_t IsNullOutput() const { return fIsNull; }
|
|---|
| 153 | Bool_t IsOutputDeviceEnabled(int i) const { return fDevice & i; }
|
|---|
| 154 |
|
|---|
| 155 | void SetOutputGui(TGTextView *sout, int flag=-1)
|
|---|
| 156 | {
|
|---|
| 157 | fGui = sout;
|
|---|
| 158 | CheckFlag(eGui, flag);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | void SetOutputFile(ofstream &sout, int flag=-1)
|
|---|
| 162 | {
|
|---|
| 163 | //
|
|---|
| 164 | // Set new output file by a given stream. The new output
|
|---|
| 165 | // file is not deleted automatically. If no flag is specified
|
|---|
| 166 | // the state of the file-device stream is unchanged.
|
|---|
| 167 | // if the a flag is specified the state is changed
|
|---|
| 168 | // in correspondance to the flag
|
|---|
| 169 | //
|
|---|
| 170 | DeallocateFile();
|
|---|
| 171 | fOut = &sout;
|
|---|
| 172 | CheckFlag(eFile, flag);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | void SetOutputFile(const char *f=NULL, int flag=-1)
|
|---|
| 176 | {
|
|---|
| 177 | //
|
|---|
| 178 | // Set new output file by name. The new output file must
|
|---|
| 179 | // not be deleted by the user. If no flag is specified
|
|---|
| 180 | // the state of the file-device stream is unchanged.
|
|---|
| 181 | // if the a flag is specified the state is changed
|
|---|
| 182 | // in correspondance to the flag
|
|---|
| 183 | //
|
|---|
| 184 | ReallocateFile(f);
|
|---|
| 185 | CheckFlag(eFile, flag);
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | ofstream &GetOutputFile()
|
|---|
| 189 | {
|
|---|
| 190 | //
|
|---|
| 191 | // Get the file output stream from MLogging
|
|---|
| 192 | // if no file output stream is existing yet it will be created.
|
|---|
| 193 | // For the creating a C-Temporary file name is used.
|
|---|
| 194 | // if the stream is created here the user must not delete it
|
|---|
| 195 | //
|
|---|
| 196 | // What a pitty, but it seems, that there is now way to ask
|
|---|
| 197 | // an ofstream for the corresponding file name. Elsewhise
|
|---|
| 198 | // I would implement a GetFileName-function, too.
|
|---|
| 199 | //
|
|---|
| 200 | if (!fOut)
|
|---|
| 201 | ReallocateFile(NULL);
|
|---|
| 202 | return *fOut;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | // FIXME: Switch off colors when on....
|
|---|
| 206 | void SetNullOutput(Bool_t n=kTRUE) { fIsNull = n; }
|
|---|
| 207 |
|
|---|
| 208 | void SetNoColors(Bool_t flag=kTRUE) { flag ? SetBit(eNoColors) : ResetBit(eNoColors); }
|
|---|
| 209 |
|
|---|
| 210 | void Setup(MArgs &arg);
|
|---|
| 211 | void Usage();
|
|---|
| 212 |
|
|---|
| 213 | void ReadEnv(const TEnv &env, TString prefix="", Bool_t print=kFALSE);
|
|---|
| 214 | void WriteEnv(TEnv &env, TString prefix="", Bool_t print=kFALSE) const;
|
|---|
| 215 |
|
|---|
| 216 | MLog &Separator(TString str="", int outlvl=0)
|
|---|
| 217 | {
|
|---|
| 218 | if (!str.IsNull())
|
|---|
| 219 | {
|
|---|
| 220 | const Int_t l = (78-str.Length())/2-3;
|
|---|
| 221 | str.Prepend("={ ");
|
|---|
| 222 | str.Prepend('-', l<1?1:l);
|
|---|
| 223 | str.Append(" }=");
|
|---|
| 224 | str.Append('-', l<1?1:l);
|
|---|
| 225 | }
|
|---|
| 226 | if (str.Length()<78)
|
|---|
| 227 | str.Append('-', 78-str.Length());
|
|---|
| 228 |
|
|---|
| 229 | const int save = fOutputLevel;
|
|---|
| 230 | SetOutputLevel(outlvl);
|
|---|
| 231 | (*this) << str << std::endl;
|
|---|
| 232 | fOutputLevel = save;
|
|---|
| 233 |
|
|---|
| 234 | return *this;
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | void AddPlugin(MLogPlugin *plug);
|
|---|
| 238 |
|
|---|
| 239 | ClassDef(MLog, 0) // This is what we call 'The logging system'
|
|---|
| 240 | };
|
|---|
| 241 |
|
|---|
| 242 | //
|
|---|
| 243 | // This is the definition of a global output stream, which by
|
|---|
| 244 | // default pipes all output to the stdout
|
|---|
| 245 | //
|
|---|
| 246 | R__EXTERN MLog gLog;
|
|---|
| 247 |
|
|---|
| 248 | #endif
|
|---|