]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/log.cpp
ambiguous ctor call resolved
[wxWidgets.git] / src / common / log.cpp
index 1ba86880833f2b0f050c3fb8615fd432bd63ef38..34391202ed6c860fe50dea0e325bc551c9b9d078 100644 (file)
 
 // wxWindows
 #ifndef WX_PRECOMP
-  #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>
+  #include "wx/window.h"
+#ifdef __WXMSW__
+  #include "wx/msw/private.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>
@@ -311,30 +316,43 @@ void wxLog::RemoveTraceMask(const wxString& str)
         ms_aTraceMasks.Remove((size_t)index);
 }
 
-void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
+void wxLog::TimeStamp(wxString *str)
 {
-    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 ) {
         case wxLOG_FatalError:
-            DoLogString(str << _("Fatal error: ") << szString, t);
+            DoLogString(wxString(_("Fatal error: ")) + szString, t);
             DoLogString(_("Program aborted."), t);
             Flush();
             abort();
             break;
 
         case wxLOG_Error:
-            DoLogString(str << _("Error: ") << szString, t);
+            DoLogString(wxString(_("Error: ")) + szString, t);
             break;
 
         case wxLOG_Warning:
-            DoLogString(str << _("Warning: ") << szString, t);
+            DoLogString(wxString(_("Warning: ")) + szString, t);
             break;
 
         case wxLOG_Info:
-        case wxLOG_Message:
             if ( GetVerbose() )
-                DoLogString(str + szString, t);
+        case wxLOG_Message:
+        default:    // log unknown log levels too
+                DoLogString(szString, t);
             // fall through
 
         case wxLOG_Status:
@@ -346,15 +364,11 @@ void wxLog::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
 #ifdef __WXDEBUG__
             DoLogString(szString, t);
 #endif
-
             break;
-
-        default:
-            wxFAIL_MSG(_("unknown log level in wxLog::DoLog"));
     }
 }
 
-void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t t)
+void wxLog::DoLogString(const wxChar *WXUNUSED(szString), time_t WXUNUSED(t))
 {
     wxFAIL_MSG(_T("DoLogString must be overriden if it's called."));
 }
@@ -376,10 +390,11 @@ wxLogStderr::wxLogStderr(FILE *fp)
         m_fp = fp;
 }
 
-void wxLogStderr::DoLogString(const wxChar *szString, time_t t)
+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);
@@ -404,9 +419,9 @@ wxLogStream::wxLogStream(ostream *ostr)
         m_ostr = ostr;
 }
 
-void wxLogStream::DoLogString(const wxChar *szString, time_t t)
+void wxLogStream::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
 {
-    (*m_ostr) << szString << endl << flush;
+    (*m_ostr) << wxConvCurrent->cWX2MB(szString) << endl << flush;
 }
 #endif // wxUSE_STD_IOSTREAM
 
@@ -416,19 +431,19 @@ void wxLogStream::DoLogString(const wxChar *szString, time_t 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)
@@ -492,6 +507,9 @@ void wxLogGui::Flush()
 
     // no undisplayed messages whatsoever
     Clear();
+    
+    // do it here again
+    m_bHasMessages = FALSE;
 }
 
 // the default behaviour is to discard all informational messages if there
@@ -510,6 +528,7 @@ void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
                 break;
 
         case wxLOG_Status:
+#if wxUSE_STATUSBAR
                 {
                     // find the top window and set it's status text if it has any
                     wxFrame *pFrame = gs_pFrame;
@@ -523,6 +542,7 @@ void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
                     if ( pFrame != NULL )
                         pFrame->SetStatusText(szString);
                 }
+#endif // wxUSE_STATUSBAR
                 break;
 
         case wxLOG_Trace:
@@ -532,12 +552,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
-                        fprintf(stderr, "%s: %s\n",
-                                level == wxLOG_Trace ? "Trace" : "Debug",
-                                (const char*)wxConv_libc.cWX2MB(szString));
+                        wxFprintf(stderr, _T("%s: %s\n"),
+                                  level == wxLOG_Trace ? _T("Trace")
+                                                       : _T("Debug"),
+                                  szString);
                         fflush(stderr);
                     #endif
                 }
@@ -556,7 +580,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
@@ -569,10 +592,8 @@ void wxLogGui::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
 
                 m_aMessages.Add(szString);
                 m_aTimes.Add((long)t);
+                m_bHasMessages = TRUE;
                 break;
-
-        default:
-                wxFAIL_MSG(_("unknown log level in wxLogGui::DoLog"));
     }
 }
 
@@ -592,7 +613,9 @@ public:
     // menu callbacks
     void OnClose(wxCommandEvent& event);
     void OnCloseWindow(wxCloseEvent& event);
+#if wxUSE_FILE
     void OnSave (wxCommandEvent& event);
+#endif // wxUSE_FILE
     void OnClear(wxCommandEvent& event);
 
     void OnIdle(wxIdleEvent&);
@@ -620,7 +643,9 @@ private:
 BEGIN_EVENT_TABLE(wxLogFrame, wxFrame)
     // wxLogWindow menu events
     EVT_MENU(Menu_Close, wxLogFrame::OnClose)
+#if wxUSE_FILE
     EVT_MENU(Menu_Save,  wxLogFrame::OnSave)
+#endif // wxUSE_FILE
     EVT_MENU(Menu_Clear, wxLogFrame::OnClear)
 
     EVT_CLOSE(wxLogFrame::OnCloseWindow)
@@ -640,15 +665,19 @@ wxLogFrame::wxLogFrame(wxFrame *pParent, wxLogWindow *log, const wxChar *szTitle
     // create menu
     wxMenuBar *pMenuBar = new wxMenuBar;
     wxMenu *pMenu = new wxMenu;
+#if wxUSE_FILE
     pMenu->Append(Menu_Save,  _("&Save..."), _("Save log contents to file"));
+#endif // wxUSE_FILE
     pMenu->Append(Menu_Clear, _("C&lear"), _("Clear the log contents"));
     pMenu->AppendSeparator();
     pMenu->Append(Menu_Close, _("&Close"), _("Close this window"));
     pMenuBar->Append(pMenu, _("&Log"));
     SetMenuBar(pMenuBar);
 
+#if wxUSE_STATUSBAR
     // status bar for menu prompts
     CreateStatusBar();
+#endif // wxUSE_STATUSBAR
 
     m_log->OnFrameCreate(this);
 }
@@ -663,6 +692,7 @@ void wxLogFrame::OnCloseWindow(wxCloseEvent& WXUNUSED(event))
     DoClose();
 }
 
+#if wxUSE_FILE
 void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
 {
     // get the file name
@@ -733,6 +763,7 @@ void wxLogFrame::OnSave(wxCommandEvent& WXUNUSED(event))
         wxLogStatus(this, _("Log saved to the file '%s'."), szFileName);
     }
 }
+#endif // wxUSE_FILE
 
 void wxLogFrame::OnClear(wxCommandEvent& WXUNUSED(event))
 {
@@ -747,9 +778,9 @@ wxLogFrame::~wxLogFrame()
 // wxLogWindow
 // -----------
 wxLogWindow::wxLogWindow(wxFrame *pParent,
-        const wxChar *szTitle,
-        bool bShow,
-        bool bDoPass)
+                         const wxChar *szTitle,
+                         bool bShow,
+                         bool bDoPass)
 {
     m_bPassMessages = bDoPass;
 
@@ -789,6 +820,7 @@ void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
             case wxLOG_Status:
                 // by default, these messages are ignored by wxLog, so process
                 // them ourselves
+                if ( !wxIsEmpty(szString) )
                 {
                     wxString str;
                     str << _("Status: ") << szString;
@@ -812,7 +844,7 @@ void wxLogWindow::DoLog(wxLogLevel level, const wxChar *szString, time_t t)
     m_bHasMessages = TRUE;
 }
 
-void wxLogWindow::DoLogString(const wxChar *szString, time_t t)
+void wxLogWindow::DoLogString(const wxChar *szString, time_t WXUNUSED(t))
 {
     // put the text into our window
     wxTextCtrl *pText = m_pLogFrame->TextCtrl();
@@ -823,8 +855,11 @@ void wxLogWindow::DoLogString(const wxChar *szString, time_t 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
 }
@@ -864,6 +899,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;
 
@@ -963,7 +1001,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);
@@ -996,7 +1034,7 @@ void Trap()
 }
 
 // this function is called when an assert fails
-void wxOnAssert(const char *szFile, int nLine, const wxChar *szMsg)
+void wxOnAssert(const wxChar *szFile, int nLine, const wxChar *szMsg)
 {
     // this variable can be set to true to suppress "assert failure" messages
     static bool s_bNoAsserts = FALSE;
@@ -1018,14 +1056,10 @@ void wxOnAssert(const char *szFile, int nLine, const wxChar *szMsg)
     // make life easier for people using VC++ IDE: clicking on the message
     // will take us immediately to the place of the failed assert
 #ifdef __VISUALC__
-    sprintf(szBuf, _T("%s(%d): assert failed"), szFile, nLine);
+    wxSprintf(szBuf, _T("%s(%d): assert failed"), szFile, nLine);
 #else  // !VC++
     // make the error message more clear for all the others
-#ifdef wxSprintf
     wxSprintf(szBuf, _T("Assert failed in file %s at line %d"), szFile, nLine);
-#else
-    wxSprintf(szBuf, _T("Assert failed in file %hs at line %d"), szFile, nLine);
-#endif
 #endif // VC/!VC
 
     if ( szMsg != NULL ) {