Index: /trunk/MagicSoft/Mars/mbase/MLog.cc
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MLog.cc	(revision 1742)
+++ /trunk/MagicSoft/Mars/mbase/MLog.cc	(revision 1743)
@@ -45,4 +45,5 @@
 #include <stdlib.h>     // mkstempe
 #include <fstream.h>
+#include <pthread.h>
 #include <TGListBox.h>
 
@@ -68,4 +69,10 @@
     setp(&fBuffer, &fBuffer+1);
     *this << '\0';
+
+    //
+    // Creat drawing semaphore
+    //
+    fMuxGui = new pthread_mutex_t;
+    pthread_mutex_init((pthread_mutex_t*)fMuxGui, NULL);
 }
 
@@ -75,5 +82,5 @@
 // which is used for the output (i)
 //
-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)
+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), fNumLines(0)
 {
     Init();
@@ -85,5 +92,5 @@
 // ofstream as the default output device
 //
-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)
+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), fNumLines(0)
 {
     Init();
@@ -95,5 +102,5 @@
 // TGListBox as the default output device
 //
-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)
+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), fNumLines(0)
 {
     Init();
@@ -106,5 +113,5 @@
 // or not.
 //
-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)
+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), fNumLines(0)
 {
     Init();
@@ -112,4 +119,14 @@
     AllocateFile(fname);
     CheckFlag(eFile, flag);
+}
+
+// --------------------------------------------------------------------------
+//
+//  Destructor, destroying the gui mutex.
+//
+MLog::~MLog()
+{
+    DeallocateFile();
+    pthread_mutex_destroy((pthread_mutex_t*)fMuxGui);
 }
 
@@ -153,14 +170,60 @@
     if (fDevice&eGui && fgui)
     {
+        char **newstr = new char*[fNumLines+1];
+
+        for (int i=0; i<fNumLines; i++)
+            newstr[i] = fGuiLines[i];
+
+        if (fNumLines>0)
+            delete fGuiLines;
+
         char *dummy = new char[len];
         memcpy(dummy, fBase, len-1);
-        *(dummy+len-1)='\0';
-        fgui->AddEntry(dummy, fGuiLineId);
-        fgui->SetTopEntry(fGuiLineId++);
-        fgui->SetBit(kHasChanged);
-        delete dummy;
+        dummy[len-1]='\0';
+
+        newstr[fNumLines++] = dummy;
+
+        fGuiLines = newstr;
     }
 }
 
+void MLog::UpdateGui()
+{
+    if (fNumLines==0)
+        return;
+
+    Lock();
+
+//    cout << "/---------------------------------------" << endl;
+
+    for (int i=0; i<fNumLines; i++)
+    {
+        fgui->AddEntry(fGuiLines[i], fGuiLineId++);
+//        cout << fGuiLines[i] << endl;
+        delete fGuiLines[i];
+    }
+
+    delete fGuiLines;
+
+//    cout << "\\---------------------------------------" << endl;
+
+    fNumLines=0;
+
+    fgui->SetTopEntry(fGuiLineId-1);
+    fgui->SetBit(kHasChanged);
+
+    UnLock();
+}
+
+void MLog::Lock()
+{
+    pthread_mutex_lock((pthread_mutex_t*)fMuxGui);
+}
+
+void MLog::UnLock()
+{
+    pthread_mutex_unlock((pthread_mutex_t*)fMuxGui);
+}
+
 // --------------------------------------------------------------------------
 //
@@ -169,5 +232,7 @@
 int MLog::sync()
 {
+    Lock();
     WriteBuffer();
+    UnLock();
 
     if (fDevice&eStdout)
@@ -197,11 +262,15 @@
     // no output if
     //
-    if (fOutputLevel > fDebugLevel)
-        return 0;
-
-    *fPPtr++ = (char)i;
-
-    if (fPPtr == fEPtr)
-        WriteBuffer();
+    if (fOutputLevel <= fDebugLevel)
+    {
+        Lock();
+
+        *fPPtr++ = (char)i;
+
+        if (fPPtr == fEPtr)
+            WriteBuffer();
+
+        UnLock();
+    }
 
     return 0;
@@ -215,5 +284,6 @@
 void MLog::AllocateFile(const char *fname)
 {
-    fout = fname ? new ofstream(fname) : new ofstream(mkstemp("logXXXXXX"));
+    char *txt = (char*)"logXXXXXX";
+    fout = fname ? new ofstream(fname) : new ofstream(mkstemp(txt));
     fOutAllocated = kTRUE;
 }
Index: /trunk/MagicSoft/Mars/mbase/MLog.h
===================================================================
--- /trunk/MagicSoft/Mars/mbase/MLog.h	(revision 1742)
+++ /trunk/MagicSoft/Mars/mbase/MLog.h	(revision 1743)
@@ -40,4 +40,10 @@
     TGListBox *fgui;          //! Listbox output
 
+    Bool_t fIsDirectGui;      //! Pipe text directly to the GUI (for single threaded environments)
+    char   **fGuiLines;      //! Lines to pipe to gui
+    Int_t     fNumLines;
+
+    void *fMuxGui;            //! Mutex locking access of TGListBox
+
     void Init();
 
@@ -58,12 +64,15 @@
 
     MLog(MLog &log);
+    ~MLog();
 
-    ~MLog()
-    {
-        DeallocateFile();
-    }
+    void Lock();
+    void UnLock();
+
+    void EnableDirectGui()  { fIsDirectGui = kTRUE; }
+    void DisableDirectGui() { fIsDirectGui = kFALSE; }
+    void UpdateGui();
 
     void SetDebugLevel(int i)           { fDebugLevel  =  i;  }
-    int GetDebugLevel() const           { return fDebugLevel; }
+    int  GetDebugLevel() const          { return fDebugLevel; }
     void SetOutputLevel(int i)          { fOutputLevel =  i;  }
     void SetOutputDevice(int i)         { fDevice      =  i;  }
