]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/log.cpp
Commented setsockopt as it doesn't work anywhere. I will use signals for timeout.
[wxWidgets.git] / src / common / log.cpp
index 7c350059690140ad58939e296c8b8933b9fd0d62..b29f583649afc0aa2c334349249e351a8c2e4db9 100644 (file)
 // wxWindows
 #ifndef WX_PRECOMP
   #include "wx/window.h"
+#ifdef __WXMSW__
   #include "wx/msw/private.h"
-  #include  <wx/event.h>
-  #include  <wx/app.h>
-  #include  <wx/string.h>
-  #include  <wx/intl.h>
-  #include  <wx/menu.h>
-  #include  <wx/frame.h>
-  #include  <wx/msgdlg.h>
-  #include  <wx/filedlg.h>
-  #include  <wx/textctrl.h>
+#endif
+  #include  "wx/event.h"
+  #include  "wx/app.h"
+  #include  "wx/string.h"
+  #include  "wx/intl.h"
+  #include  "wx/menu.h"
+  #include  "wx/frame.h"
+  #include  "wx/msgdlg.h"
+  #include  "wx/filedlg.h"
+  #include  "wx/textctrl.h"
 #endif //WX_PRECOMP
 
-#include  <wx/file.h>
-#include  <wx/textfile.h>
-#include  <wx/utils.h>
-#include  <wx/log.h>
+#include  "wx/file.h"
+#include  "wx/textfile.h"
+#include  "wx/utils.h"
+#include  "wx/wxchar.h"
+#include  "wx/log.h"
 
 // other standard headers
 #include  <errno.h>
@@ -313,6 +316,20 @@ void wxLog::RemoveTraceMask(const wxString& str)
         ms_aTraceMasks.Remove((size_t)index);
 }
 
+void wxLog::TimeStamp(wxString *str)
+{
+    if ( ms_timestamp )
+    {
+        wxChar buf[256];
+        time_t timeNow;
+        (void)time(&timeNow);
+        wxStrftime(buf, WXSIZEOF(buf), ms_timestamp, localtime(&timeNow));
+
+        str->Empty();
+        *str << buf << _T(": ");
+    }
+}
+
 void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
 {
     switch ( level ) {
@@ -375,8 +392,9 @@ wxLogStderr::wxLogStderr(FILE *fp)
 
 void wxLogStderr::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
 {
-    wxString str(szString);
-    str << _T('\n');
+    wxString str;
+    TimeStamp(&str);
+    str << szString << _T('\n');
 
     fputs(str.mb_str(), m_fp);
     fflush(m_fp);
@@ -403,7 +421,7 @@ wxLogStream::wxLogStream(ostream *ostr)
 
 void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
 {
-    (*m_ostr) << wxConv_libc.cWX2MB(szString) << endl << flush;
+    (*m_ostr) << wxConvCurrent->cWX2MB(szString) << endl << flush;
 }
 #endif // wxUSE_STD_IOSTREAM
 
@@ -413,19 +431,19 @@ void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
 // wxLogTextCtrl implementation
 // ----------------------------------------------------------------------------
 
-#if wxUSE_STD_IOSTREAM
 wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
-#if !defined(NO_TEXT_WINDOW_STREAM)
-: wxLogStream(new ostream(pTextCtrl))
-#endif
 {
+    m_pTextCtrl = pTextCtrl;
 }
 
-wxLogTextCtrl::~wxLogTextCtrl()
+void wxLogTextCtrl::DoLogString(const wxChar *szString, time_t t)
 {
-    delete m_ostr;
+    wxString msg;
+    TimeStamp(&msg);
+    msg << szString << _T('\n');
+
+    m_pTextCtrl->AppendText(msg);
 }
-#endif // wxUSE_STD_IOSTREAM
 
 // ----------------------------------------------------------------------------
 // wxLogGui implementation (FIXME MT-unsafe)
@@ -531,12 +549,16 @@ void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
                     #ifdef __WXMSW__
                         // don't prepend debug/trace here: it goes to the
                         // debug window anyhow, but do put a timestamp
-                        OutputDebugString(wxString(szString) + _T("\n\r"));
+                        wxString str;
+                        TimeStamp(&str);
+                        str << szString << _T("\n\r");
+                        OutputDebugString(str);
                     #else
                         // send them to stderr
                         wxFprintf(stderr, _T("%s: %s\n"),
-                                level == wxLOG_Trace ? _T("Trace") : _T("Debug"),
-                                szString);
+                                  level == wxLOG_Trace ? _T("Trace")
+                                                       : _T("Debug"),
+                                  szString);
                         fflush(stderr);
                     #endif
                 }
@@ -555,7 +577,6 @@ void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
                 if ( !m_bErrors ) {
                     m_aMessages.Empty();
                     m_aTimes.Empty();
-                    m_bHasMessages = TRUE;
                     m_bErrors = TRUE;
                 }
                 // fall through
@@ -568,6 +589,7 @@ void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
 
                 m_aMessages.Add(szString);
                 m_aTimes.Add((long)t);
+                m_bHasMessages = TRUE;
                 break;
     }
 }
@@ -830,8 +852,11 @@ void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
     pText->SetSelection(nLen, nLen);
 #endif // Windows
 
-    pText->WriteText(szString);
-    pText->WriteText(_T("\n")); // "\n" ok here (_not_ "\r\n")
+    wxString msg;
+    TimeStamp(&msg);
+    msg << szString << _T('\n');
+
+    pText->AppendText(msg);
 
     // TODO ensure that the line can be seen
 }
@@ -871,6 +896,9 @@ wxLogWindow::~wxLogWindow()
 wxLog          *wxLog::ms_pLogger      = (wxLog *)NULL;
 bool            wxLog::ms_doLog        = TRUE;
 bool            wxLog::ms_bAutoCreate  = TRUE;
+
+const wxChar   *wxLog::ms_timestamp    = _T("%X");  // time only, no date
+
 wxTraceMask     wxLog::ms_ulTraceMask  = (wxTraceMask)0;
 wxArrayString   wxLog::ms_aTraceMasks;
 
@@ -970,7 +998,7 @@ const wxChar *wxSysErrorMsg(unsigned long nErrCode)
 #else   // Unix
 #if wxUSE_UNICODE
     static wxChar s_szBuf[LOG_BUFFER_SIZE / 2];
-    wxConv_libc.MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
+    wxConvCurrent->MB2WC(s_szBuf, strerror(nErrCode), WXSIZEOF(s_szBuf) -1);
     return s_szBuf;
 #else
     return strerror(nErrCode);