Changeset 1743


Ignore:
Timestamp:
02/06/03 11:35:56 (22 years ago)
Author:
tbretz
Message:
*** empty log message ***
Location:
trunk/MagicSoft/Mars/mbase
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Mars/mbase/MLog.cc

    r1524 r1743  
    4545#include <stdlib.h>     // mkstempe
    4646#include <fstream.h>
     47#include <pthread.h>
    4748#include <TGListBox.h>
    4849
     
    6869    setp(&fBuffer, &fBuffer+1);
    6970    *this << '\0';
     71
     72    //
     73    // Creat drawing semaphore
     74    //
     75    fMuxGui = new pthread_mutex_t;
     76    pthread_mutex_init((pthread_mutex_t*)fMuxGui, NULL);
    7077}
    7178
     
    7582// which is used for the output (i)
    7683//
    77 MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), fIsNull(kFALSE), fGuiLineId(0), fout(NULL), fOutAllocated(kFALSE), fgui(NULL)
     84MLog::MLog(int i) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(i), fIsNull(kFALSE), fGuiLineId(0), fout(NULL), fOutAllocated(kFALSE), fgui(NULL), fNumLines(0)
    7885{
    7986    Init();
     
    8592// ofstream as the default output device
    8693//
    87 MLog::MLog(ofstream &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fGuiLineId(0),  fout(&out), fOutAllocated(kFALSE), fgui(NULL)
     94MLog::MLog(ofstream &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fGuiLineId(0),  fout(&out), fOutAllocated(kFALSE), fgui(NULL), fNumLines(0)
    8895{
    8996    Init();
     
    95102// TGListBox as the default output device
    96103//
    97 MLog::MLog(TGListBox &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), fGuiLineId(0),  fout(NULL), fOutAllocated(kFALSE), fgui(&out)
     104MLog::MLog(TGListBox &out) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eGui), fGuiLineId(0),  fout(NULL), fOutAllocated(kFALSE), fgui(&out), fNumLines(0)
    98105{
    99106    Init();
     
    106113// or not.
    107114//
    108 MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fGuiLineId(0),  fgui(NULL)
     115MLog::MLog(const char *fname, int flag) : ostream(this), fPPtr(fBase), fEPtr(fBase+bsz), fOutputLevel(0), fDebugLevel((unsigned)-1), fDevice(eFile), fIsNull(kFALSE), fGuiLineId(0),  fgui(NULL), fNumLines(0)
    109116{
    110117    Init();
     
    112119    AllocateFile(fname);
    113120    CheckFlag(eFile, flag);
     121}
     122
     123// --------------------------------------------------------------------------
     124//
     125//  Destructor, destroying the gui mutex.
     126//
     127MLog::~MLog()
     128{
     129    DeallocateFile();
     130    pthread_mutex_destroy((pthread_mutex_t*)fMuxGui);
    114131}
    115132
     
    153170    if (fDevice&eGui && fgui)
    154171    {
     172        char **newstr = new char*[fNumLines+1];
     173
     174        for (int i=0; i<fNumLines; i++)
     175            newstr[i] = fGuiLines[i];
     176
     177        if (fNumLines>0)
     178            delete fGuiLines;
     179
    155180        char *dummy = new char[len];
    156181        memcpy(dummy, fBase, len-1);
    157         *(dummy+len-1)='\0';
    158         fgui->AddEntry(dummy, fGuiLineId);
    159         fgui->SetTopEntry(fGuiLineId++);
    160         fgui->SetBit(kHasChanged);
    161         delete dummy;
     182        dummy[len-1]='\0';
     183
     184        newstr[fNumLines++] = dummy;
     185
     186        fGuiLines = newstr;
    162187    }
    163188}
    164189
     190void MLog::UpdateGui()
     191{
     192    if (fNumLines==0)
     193        return;
     194
     195    Lock();
     196
     197//    cout << "/---------------------------------------" << endl;
     198
     199    for (int i=0; i<fNumLines; i++)
     200    {
     201        fgui->AddEntry(fGuiLines[i], fGuiLineId++);
     202//        cout << fGuiLines[i] << endl;
     203        delete fGuiLines[i];
     204    }
     205
     206    delete fGuiLines;
     207
     208//    cout << "\\---------------------------------------" << endl;
     209
     210    fNumLines=0;
     211
     212    fgui->SetTopEntry(fGuiLineId-1);
     213    fgui->SetBit(kHasChanged);
     214
     215    UnLock();
     216}
     217
     218void MLog::Lock()
     219{
     220    pthread_mutex_lock((pthread_mutex_t*)fMuxGui);
     221}
     222
     223void MLog::UnLock()
     224{
     225    pthread_mutex_unlock((pthread_mutex_t*)fMuxGui);
     226}
     227
    165228// --------------------------------------------------------------------------
    166229//
     
    169232int MLog::sync()
    170233{
     234    Lock();
    171235    WriteBuffer();
     236    UnLock();
    172237
    173238    if (fDevice&eStdout)
     
    197262    // no output if
    198263    //
    199     if (fOutputLevel > fDebugLevel)
    200         return 0;
    201 
    202     *fPPtr++ = (char)i;
    203 
    204     if (fPPtr == fEPtr)
    205         WriteBuffer();
     264    if (fOutputLevel <= fDebugLevel)
     265    {
     266        Lock();
     267
     268        *fPPtr++ = (char)i;
     269
     270        if (fPPtr == fEPtr)
     271            WriteBuffer();
     272
     273        UnLock();
     274    }
    206275
    207276    return 0;
     
    215284void MLog::AllocateFile(const char *fname)
    216285{
    217     fout = fname ? new ofstream(fname) : new ofstream(mkstemp("logXXXXXX"));
     286    char *txt = (char*)"logXXXXXX";
     287    fout = fname ? new ofstream(fname) : new ofstream(mkstemp(txt));
    218288    fOutAllocated = kTRUE;
    219289}
  • trunk/MagicSoft/Mars/mbase/MLog.h

    r1666 r1743  
    4040    TGListBox *fgui;          //! Listbox output
    4141
     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
    4248    void Init();
    4349
     
    5864
    5965    MLog(MLog &log);
     66    ~MLog();
    6067
    61     ~MLog()
    62     {
    63         DeallocateFile();
    64     }
     68    void Lock();
     69    void UnLock();
     70
     71    void EnableDirectGui()  { fIsDirectGui = kTRUE; }
     72    void DisableDirectGui() { fIsDirectGui = kFALSE; }
     73    void UpdateGui();
    6574
    6675    void SetDebugLevel(int i)           { fDebugLevel  =  i;  }
    67     int GetDebugLevel() const           { return fDebugLevel; }
     76    int  GetDebugLevel() const          { return fDebugLevel; }
    6877    void SetOutputLevel(int i)          { fOutputLevel =  i;  }
    6978    void SetOutputDevice(int i)         { fDevice      =  i;  }
Note: See TracChangeset for help on using the changeset viewer.