From 9ef3052c912e6e1fb35d589d14d38d65b7eb70d9 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 26 May 1998 15:11:46 +0000 Subject: [PATCH] 1) log leves are now global wxLOG_ prefixed constants and not wxConfig::enum members 2) another variant of wxLogTrace() takes the trace level as argument: this allows to filter trace messages from different sources (see wxTraceXXX constants) 3) wxLogWindow class added: opens a debug frame which collects all log messages git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@34 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/log.h | 143 +++++++++---- src/common/log.cpp | 495 +++++++++++++++++++++++++++++++++------------ 2 files changed, 462 insertions(+), 176 deletions(-) diff --git a/include/wx/log.h b/include/wx/log.h index 7a3fd73d9e..a484063245 100644 --- a/include/wx/log.h +++ b/include/wx/log.h @@ -13,39 +13,54 @@ #define __LOGH__ #ifdef __GNUG__ -#pragma interface "log.h" + #pragma interface #endif //GNU C++ +// ---------------------------------------------------------------------------- +// constants +// ---------------------------------------------------------------------------- + +// different standard log levels (you may also define your own) +enum +{ + wxLOG_FatalError, // program can't continue, abort immediately + wxLOG_Error, // a serious error, user must be informed about it + wxLOG_Warning, // user is normally informed about it but may be ignored + wxLOG_Message, // normal message (i.e. normal output of a non GUI app) + wxLOG_Info, // informational message (a.k.a. 'Verbose') + wxLOG_Status, // informational: might go to the status line of GUI app + wxLOG_Debug, // never shown to the user, disabled in release mode + wxLOG_Trace, // trace messages are also only enabled in debug mode + wxLOG_Progress, // used for progress indicator (not yet) + wxLOG_User = 100 // user defined levels start here +}; + +// meaning of different bits of the trace mask (which allows selectively +// enable/disable some trace messages) +#define wxTraceMemAlloc 0x0001 // trace memory allocation (new/delete) +#define wxTraceMessages 0x0002 // trace window messages/X callbacks +#define wxTraceResAlloc 0x0004 // trace GDI resource allocation +#define wxTraceRefCount 0x0008 // trace various ref counting operations + +#ifdef __WINDOWS__ + #define wxTraceOleCalls 0x0100 // OLE interface calls +#endif + +typedef unsigned long wxTraceMask; +typedef unsigned long wxLogLevel; + // ---------------------------------------------------------------------------- // derive from this class to redirect (or suppress, or ...) log messages // normally, only a single instance of this class exists but it's not enforced -// -// ## would ne nice to add a time stamp to log messages // ---------------------------------------------------------------------------- class WXDLLEXPORT wxLog { public: - enum Level - { - FatalError, // program can't continue, abort immediately - Error, // a serious error, user must be informed about it - Warning, // warning: user is normally informed about but may ignore it - Message, // normal message (i.e. normal output of a non GUI app) - Info, // informational message (a.k.a. 'Verbose') - Status, // informational: might go to the status line of GUI app - Debug, // never shown to the user, disabled in release mode - Trace, // trace messages are also only enabled in debug mode - Progress, // used for progress indicator (not yet) - User1, // user defined levels (use with wxLogGeneric(wxLog::User1,...) - User2, // - User3, // - }; - // ctor wxLog(); // sink function - static void OnLog(Level level, const char *szString) + static void OnLog(wxLogLevel level, const char *szString) { if ( ms_pLogger != 0 ) ms_pLogger->DoLog(level, szString); } // message buffering @@ -53,7 +68,9 @@ public: // (FILE and iostream logs don't need it, but wxGuiLog does to avoid // showing 17 modal dialogs one after another) virtual void Flush(); - // call Flush() only if this function returns true + // call to Flush() may be optimized: call it only if this function + // returns true (although Flush() also returns immediately if there + // is no messages, this functions is more efficient because inline) bool HasPendingMessages() const { return m_bHasMessages; } // only one sink is active at each moment @@ -65,16 +82,22 @@ public: // functions controlling the default wxLog behaviour // verbose mode is activated by standard command-line '-verbose' option - static void SetVerbose(bool bVerbose = TRUE) { ms_bVerbose = bVerbose; } + void SetVerbose(bool bVerbose = TRUE) { m_bVerbose = bVerbose; } // sets the format for timestamp prepended by wxLog::DoLog(): it's // passed to strftime() function, see it's documentation for details. - // the string is not copied! - static void SetTimeStampFormat(const char *szTimeFormat) - { ms_szTimeFormat = szTimeFormat; } + // no time stamp at all if szTF is NULL or empty + // NB: the string is not copied, so it's lifetime must be long enough! + void SetTimeStampFormat(const char *szTF) { m_szTimeFormat = szTF; } + // trace mask (see wxTraceXXX constants for details) + void SetTraceMask(wxTraceMask ulMask) { m_ulTraceMask = ulMask; } // accessors // gets the verbose status - static bool GetVerbose() { return ms_bVerbose; } + bool GetVerbose() const { return m_bVerbose; } + // get current time format + const char *GetTimeStampFormat() const { return m_szTimeFormat; } + // get trace mask + wxTraceMask GetTraceMask() const { return m_ulTraceMask; } // make dtor virtual for all derived classes virtual ~wxLog() { } @@ -82,20 +105,21 @@ public: protected: bool m_bHasMessages; - // static variables - // ---------------- - static bool ms_bVerbose; // FALSE => ignore LogInfo messages - static const char *ms_szTimeFormat; // format for strftime() + bool m_bVerbose; // FALSE => ignore LogInfo messages + const char *m_szTimeFormat; // format for strftime() + wxTraceMask m_ulTraceMask; // controls wxLogTrace behaviour -private: // the logging functions that can be overriden // default DoLog() prepends the time stamp and a prefix corresponding // to the message to szString and then passes it to DoLogString() - virtual void DoLog(Level level, const char *szString); + virtual void DoLog(wxLogLevel level, const char *szString); // default DoLogString does nothing but is not pure virtual because if // you override DoLog() you might not need it at all virtual void DoLogString(const char *szString); +private: + // static variables + // ---------------- static wxLog *ms_pLogger; // currently active log sink static bool ms_bInitialized; // any log targets created? }; @@ -133,8 +157,8 @@ protected: ostream *m_ostr; }; -/* // log everything to a text window (GUI only of course) +class wxTextCtrl; class WXDLLEXPORT wxLogTextCtrl : public wxLogStream { public: @@ -142,7 +166,6 @@ public: wxLogTextCtrl(wxTextCtrl *pTextCtrl); ~wxLogTextCtrl(); }; -*/ // ---------------------------------------------------------------------------- // GUI log target, the default one for wxWindows programs @@ -157,12 +180,37 @@ public: virtual void Flush(); protected: - virtual void DoLog(Level level, const char *szString); + virtual void DoLog(wxLogLevel level, const char *szString); wxArrayString m_aMessages; bool m_bErrors; }; +// ---------------------------------------------------------------------------- +// (background) log window: this class forwards all log messages to the log +// target which was active when it was instantiated, but also collects them +// to the log window. This window has it's own menu which allows the user to +// close it, clear the log contents or save it to the file. +// ---------------------------------------------------------------------------- +class wxLogFrame; +class WXDLLEXPORT wxLogWindow : public wxLog +{ +public: + wxLogWindow(const wxTString& strTitle); + ~wxLogWindow(); + + // show/hide the log window + void Show(bool bShow = TRUE); + +protected: + virtual void DoLog(wxLogLevel level, const char *szString); + virtual void DoLogString(const char *szString); + +private: + wxLog *m_pOldLog; // previous log target + wxLogFrame *m_pLogFrame; // the log frame +}; + // ---------------------------------------------------------------------------- // /dev/null log target: suppress logging until this object goes out of scope // ---------------------------------------------------------------------------- @@ -209,7 +257,7 @@ private: // an array, function or reference :-( // the most generic log function -void WXDLLEXPORT wxLogGeneric(wxLog::Level level, wxTString strFormat, ...); +void WXDLLEXPORT wxLogGeneric(wxLogLevel level, wxTString strFormat, ...); #define DECLARE_LOG_FUNCTION(level) \ extern void WXDLLEXPORT wxLog##level(wxTString strFormat, ...) @@ -231,13 +279,22 @@ DECLARE_LOG_FUNCTION(SysError); // that don't set the errno (like registry APIs in Win32)) void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...); -// debug functions don't translate their arguments -#undef DECLARE_LOG_FUNCTION -#define DECLARE_LOG_FUNCTION(level) \ - extern void WXDLLEXPORT wxLog##level(const char *szFormat, ...) +// debug functions do nothing in release mode +#ifdef __DEBUG__ + // NB: debug functions don't translate their arguments + extern void WXDLLEXPORT wxLogDebug(const char *szFormat, ...); + + // first king of LogTrace is uncoditional: it doesn't check the level, + // while the second one does nothing if all of level bits are not set + // in wxLog::GetActive()->GetTraceMask(). + extern void WXDLLEXPORT wxLogTrace(const char *szFormat, ...); + extern void WXDLLEXPORT wxLogTrace(wxTraceMask mask, + const char *szFormat, ...); +#else //!debug + #define wxLogDebug + #define wxLogTrace +#endif -DECLARE_LOG_FUNCTION(Debug); -DECLARE_LOG_FUNCTION(Trace); // are we in 'verbose' mode? // (note that it's often handy to change this var manually from the @@ -266,7 +323,7 @@ const char* WXDLLEXPORT wxSysErrorMsg(unsigned long nErrCode = 0); wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \ __FILE__, __LINE__, api, \ rc, wxSysErrorMsg(rc)) - #define wxLogLastError(api) wxLogApiError(api, ::GetLastError()) + #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode()) #else //!debug #define wxLogApiError(api, rc) #define wxLogLastError(api) diff --git a/src/common/log.cpp b/src/common/log.cpp index 05917b8bdd..d523a74f5d 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -29,12 +29,17 @@ // wxWindows #ifndef WX_PRECOMP - #include #include + #include + #include + #include -#endif + #include + #include +#endif //WX_PRECOMP -#include +#include +#include #include // other standard headers @@ -42,13 +47,10 @@ #include #include -// C++ headers -#include - // _WINDOWS_ is defined when windows.h is included, // __WINDOWS__ is defined for MS Windows compilation #if defined(__WINDOWS__) && !defined(_WINDOWS_) -#include + #include #endif //windows.h // ---------------------------------------------------------------------------- @@ -58,7 +60,7 @@ // define this to enable wrapping of log messages //#define LOG_PRETTY_WRAP -#ifdef LOG_PRETTY_WRAP +#ifdef LOG_PRETTY_WRAP static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); #endif @@ -81,16 +83,16 @@ static char s_szBuf[LOG_BUFFER_SIZE]; // generic log function -void wxLogGeneric(wxLog::Level level, wxTString strFormat, ...) +void wxLogGeneric(wxLogLevel level, wxTString strFormat, ...) { - if ( wxLog::GetActiveTarget() != NULL ) { - va_list argptr; - va_start(argptr, strFormat); - vsprintf(s_szBuf, strFormat, argptr); - va_end(argptr); - - wxLog::OnLog(level, s_szBuf); - } + if ( wxLog::GetActiveTarget() != NULL ) { + va_list argptr; + va_start(argptr, strFormat); + vsprintf(s_szBuf, strFormat, argptr); + va_end(argptr); + + wxLog::OnLog(level, s_szBuf); + } } #define IMPLEMENT_LOG_FUNCTION(level) \ @@ -102,7 +104,7 @@ void wxLogGeneric(wxLog::Level level, wxTString strFormat, ...) vsprintf(s_szBuf, strFormat, argptr); \ va_end(argptr); \ \ - wxLog::OnLog(wxLog::level, s_szBuf); \ + wxLog::OnLog(wxLOG_##level, s_szBuf); \ } \ } @@ -113,9 +115,23 @@ IMPLEMENT_LOG_FUNCTION(Message) IMPLEMENT_LOG_FUNCTION(Info) IMPLEMENT_LOG_FUNCTION(Status) -// debug functions don't use wxTString -#undef IMPLEMENT_LOG_FUNCTION -#define IMPLEMENT_LOG_FUNCTION(level) \ +// same as info, but only if 'verbose' mode is on +void wxLogVerbose(wxTString strFormat, ...) +{ + wxLog *pLog = wxLog::GetActiveTarget(); + if ( pLog != NULL && pLog->GetVerbose() ) { + va_list argptr; + va_start(argptr, strFormat); + vsprintf(s_szBuf, strFormat, argptr); + va_end(argptr); + + wxLog::OnLog(wxLOG_Info, s_szBuf); + } +} + +// debug functions +#ifdef __DEBUG__ +#define IMPLEMENT_LOG_DEBUG_FUNCTION(level) \ void wxLog##level(const char *szFormat, ...) \ { \ if ( wxLog::GetActiveTarget() != NULL ) { \ @@ -124,55 +140,65 @@ IMPLEMENT_LOG_FUNCTION(Status) vsprintf(s_szBuf, szFormat, argptr); \ va_end(argptr); \ \ - wxLog::OnLog(wxLog::level, s_szBuf); \ + wxLog::OnLog(wxLOG_##level, s_szBuf); \ } \ } -IMPLEMENT_LOG_FUNCTION(Debug) -IMPLEMENT_LOG_FUNCTION(Trace) + void wxLogTrace(wxTraceMask mask, const char *szFormat, ...) + { + wxLog *pLog = wxLog::GetActiveTarget(); -void wxLogVerbose(wxTString strFormat, ...) -{ - if ( wxLog::GetVerbose() && wxLog::GetActiveTarget() != NULL ) { - va_list argptr; - va_start(argptr, strFormat); - vsprintf(s_szBuf, strFormat, argptr); - va_end(argptr); - - wxLog::OnLog(wxLog::Info, s_szBuf); - } -} - -void wxLogSysError(wxTString str, ...) + // we check that all of mask bits are set in the current mask, so + // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something + // if both bits are set. + if ( pLog != NULL && (pLog->GetTraceMask() & mask == mask) ) { + va_list argptr; + va_start(argptr, szFormat); + vsprintf(s_szBuf, szFormat, argptr); + va_end(argptr); + + wxLog::OnLog(wxLOG_Trace, s_szBuf); + } + } + +#else // release + #define IMPLEMENT_LOG_DEBUG_FUNCTION(level) +#endif + +IMPLEMENT_LOG_DEBUG_FUNCTION(Debug) +IMPLEMENT_LOG_DEBUG_FUNCTION(Trace) + +// wxLogSysError: one uses the last error code, for other you must give it +// explicitly + +// common part of both wxLogSysError +void wxLogSysErrorHelper(long lErrCode) { - if ( wxLog::GetActiveTarget() != NULL ) { - va_list argptr; - va_start(argptr, str); - vsprintf(s_szBuf, str, argptr); - va_end(argptr); + char szErrMsg[LOG_BUFFER_SIZE / 2]; + sprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); + strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf)); - char szErrMsg[LOG_BUFFER_SIZE / 2]; - sprintf(szErrMsg, _(" (error %ld: %s)"), wxSysErrorCode(), wxSysErrorMsg()); - strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf)); + wxLog::OnLog(wxLOG_Error, s_szBuf); +} - wxLog::OnLog(wxLog::Error, s_szBuf); - } +void WXDLLEXPORT wxLogSysError(wxTString strFormat, ...) +{ + va_list argptr; + va_start(argptr, strFormat); + vsprintf(s_szBuf, strFormat, argptr); + va_end(argptr); + + wxLogSysErrorHelper(wxSysErrorCode()); } void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...) { - if ( wxLog::GetActiveTarget() != NULL ) { - va_list argptr; - va_start(argptr, strFormat); - vsprintf(s_szBuf, strFormat, argptr); - va_end(argptr); + va_list argptr; + va_start(argptr, strFormat); + vsprintf(s_szBuf, strFormat, argptr); + va_end(argptr); - char szErrMsg[LOG_BUFFER_SIZE / 2]; - sprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode)); - strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf)); - - wxLog::OnLog(wxLog::Error, s_szBuf); - } + wxLogSysErrorHelper(lErrCode); } // ---------------------------------------------------------------------------- @@ -182,19 +208,24 @@ void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...) wxLog::wxLog() { m_bHasMessages = FALSE; + m_bVerbose = FALSE; + m_szTimeFormat = "[%d/%b/%y %H:%M:%S] "; + m_ulTraceMask = (wxTraceMask)-1; // set all bits } wxLog *wxLog::GetActiveTarget() { if ( !ms_bInitialized ) { - // prevent infinite recursion if someone calls wxLogXXX() from wxApp + // prevent infinite recursion if someone calls wxLogXXX() from + // wxApp::CreateLogTarget() ms_bInitialized = TRUE; - // ask the application to create a log target for us if it exists - if ( wxTheApp != NULL ) + #ifdef WX_TEST_MINIMAL + ms_pLogger = new wxLogStderr; + #else + // ask the application to create a log target for us ms_pLogger = wxTheApp->CreateLogTarget(); - else - ms_pLogger = new wxLogStderr; + #endif // do nothing if it fails - what can we do? } @@ -215,53 +246,58 @@ wxLog *wxLog::SetActiveTarget(wxLog *pLogger) return pOldLogger; } -void wxLog::DoLog(Level level, const char *szString) +void wxLog::DoLog(wxLogLevel level, const char *szString) { - char szBuf[128]; - time_t timeNow; - struct tm *ptmNow; + wxString str; - time(&timeNow); - ptmNow = localtime(&timeNow); + // prepend a timestamp if not disabled + if ( !IsEmpty(m_szTimeFormat) ) { + char szBuf[128]; + time_t timeNow; + struct tm *ptmNow; - strftime(szBuf, WXSIZEOF(szBuf), ms_szTimeFormat, ptmNow); - wxString str = szBuf; + time(&timeNow); + ptmNow = localtime(&timeNow); + + strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow); + str = szBuf; + } switch ( level ) { - case FatalError: + case wxLOG_FatalError: DoLogString(str << _("Fatal error: ") << szString); DoLogString(_("Program aborted.")); Flush(); abort(); break; - case Error: + case wxLOG_Error: DoLogString(str << _("Error: ") << szString); break; - case Warning: + case wxLOG_Warning: DoLogString(str << _("Warning: ") << szString); break; - case Info: + case wxLOG_Info: if ( GetVerbose() ) - case Message: + case wxLOG_Message: DoLogString(str + szString); // fall through - case Status: + case wxLOG_Status: // nothing to do break; - case Trace: - case Debug: + case wxLOG_Trace: + case wxLOG_Debug: #ifdef __DEBUG__ #ifdef __WIN32__ // in addition to normal logging, also send the string to debugger // (don't prepend "Debug" here: it will go to debug window anyhow) ::OutputDebugString(str + szString + "\n\r"); #endif //Win32 - DoLogString(str << (level == Trace ? _("Trace") : _("Debug")) + DoLogString(str << (level == wxLOG_Trace ? "Trace" : "Debug") << ": " << szString); #endif @@ -272,7 +308,7 @@ void wxLog::DoLog(Level level, const char *szString) } } -void wxLog::DoLogString(const char *WXUNUSED(szString)) +void wxLog::DoLogString(const char *szString) { wxFAIL_MSG("DoLogString must be overrided if it's called."); } @@ -321,18 +357,20 @@ void wxLogStream::DoLogString(const char *szString) // ---------------------------------------------------------------------------- // wxLogTextCtrl implementation // ---------------------------------------------------------------------------- - -/* wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) +// @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf +#ifndef __GTK__ : wxLogStream(new ostream(pTextCtrl)) +#endif //GTK { } wxLogTextCtrl::~wxLogTextCtrl() { - delete m_ostr; + #ifndef __GTK__ + delete m_ostr; + #endif //GTK } -*/ // ---------------------------------------------------------------------------- // wxLogGui implementation @@ -385,19 +423,19 @@ void wxLogGui::Flush() // the default behaviour is to discard all informational messages if there // are any errors/warnings. -void wxLogGui::DoLog(Level level, const char *szString) +void wxLogGui::DoLog(wxLogLevel level, const char *szString) { switch ( level ) { - case Info: + case wxLOG_Info: if ( GetVerbose() ) - case Message: + case wxLOG_Message: if ( !m_bErrors ) { m_aMessages.Add(szString); m_bHasMessages = TRUE; } break; - case Status: + case wxLOG_Status: { // find the top window and set it's status text if it has any wxWindow *pWin = wxTheApp->GetTopWindow(); @@ -408,8 +446,8 @@ void wxLogGui::DoLog(Level level, const char *szString) } break; - case Trace: - case Debug: + case wxLOG_Trace: + case wxLOG_Debug: #ifdef __DEBUG__ #ifdef __WIN32__ OutputDebugString(szString); @@ -423,13 +461,13 @@ void wxLogGui::DoLog(Level level, const char *szString) #endif break; - case FatalError: + case wxLOG_FatalError: // show this one immediately wxMessageBox(szString, "Fatal error", wxICON_HAND); break; - case Error: - case Warning: + case wxLOG_Error: + case wxLOG_Warning: // discard earlier informational messages if this is the 1st error if ( !m_bErrors ) { m_aMessages.Empty(); @@ -445,6 +483,200 @@ void wxLogGui::DoLog(Level level, const char *szString) } } +// ---------------------------------------------------------------------------- +// wxLogWindow implementation +// ---------------------------------------------------------------------------- + +// log frame class +class wxLogFrame : public wxFrame +{ +public: + // ctor + wxLogFrame(const char *szTitle); + + // menu callbacks + void OnClose(wxCommandEvent& event); + void OnSave (wxCommandEvent& event); + void OnClear(wxCommandEvent& event); + + // accessors + wxTextCtrl *TextCtrl() const { return m_pTextCtrl; } + +private: + enum + { + Menu_Close = 100, + Menu_Save, + Menu_Clear + }; + + wxTextCtrl *m_pTextCtrl; + + DECLARE_EVENT_TABLE() +}; + +BEGIN_EVENT_TABLE(wxLogFrame, wxFrame) + // wxLogWindow menu events + EVT_MENU(Menu_Close, wxLogFrame::OnClose) + EVT_MENU(Menu_Save, wxLogFrame::OnSave) + EVT_MENU(Menu_Clear, wxLogFrame::OnClear) + + EVT_CLOSE(wxLogFrame::OnClose) +END_EVENT_TABLE() + +wxLogFrame::wxLogFrame(const char *szTitle) + : wxFrame(NULL, -1, szTitle) +{ + // @@ kludge: wxSIMPLE_BORDER is simply to prevent wxWindows from creating + // a rich edit control instead of a normal one we want + m_pTextCtrl = new wxTextCtrl(this, -1, wxEmptyString, wxDefaultPosition, + wxDefaultSize, + wxSIMPLE_BORDER | + wxTE_MULTILINE | + wxHSCROLL | + wxTE_READONLY); + /* + m_pTextCtrl->SetEditable(FALSE); + m_pTextCtrl->SetRichEdit(FALSE); + */ + + // create menu + wxMenuBar *pMenuBar = new wxMenuBar; + wxMenu *pMenu = new wxMenu; + pMenu->Append(Menu_Save, "&Save..."); + pMenu->Append(Menu_Clear, "C&lear"); + pMenu->AppendSeparator(); + pMenu->Append(Menu_Close, "&Close"); + pMenuBar->Append(pMenu, "&Log"); + SetMenuBar(pMenuBar); + + // @@ what about status bar? needed (for menu prompts)? +} + +void wxLogFrame::OnClose(wxCommandEvent& event) +{ + // just hide the window + Show(FALSE); +} + +void wxLogFrame::OnSave(wxCommandEvent& event) +{ + // get the file name + // ----------------- + const char *szFileName = wxSaveFileSelector("log", "txt", "log.txt"); + if ( szFileName == NULL ) { + // cancelled + return; + } + + // open file + // --------- + wxFile file; + Bool bOk; + if ( wxFile::Exists(szFileName) ) { + Bool bAppend; + wxString strMsg; + strMsg.Printf(_("Append log to file '%s' " + "(choosing [No] will overwrite it)?"), szFileName); + switch ( wxMessageBox(strMsg, "Question", wxYES_NO | wxCANCEL) ) { + case wxYES: + bAppend = TRUE; + break; + + case wxNO: + bAppend = FALSE; + break; + + case wxCANCEL: + return; + + default: + wxFAIL_MSG("invalid message box return value"); + } + + if ( bAppend ) { + bOk = file.Open(szFileName, wxFile::write_append); + } + else { + bOk = file.Create(szFileName, TRUE /* overwrite */); + } + } + else { + bOk = file.Create(szFileName); + } + + // retrieve text and save it + // ------------------------- + + // @@@@ TODO: no GetNumberOfLines and GetLineText in wxGTK yet +#ifndef __GTK__ + int nLines = m_pTextCtrl->GetNumberOfLines(); + for ( int nLine = 0; bOk && nLine < nLines; nLine++ ) { + bOk = file.Write(m_pTextCtrl->GetLineText(nLine) + wxTextFile::GetEOL()); + } +#endif //GTK + + if ( bOk ) + bOk = file.Close(); + + if ( !bOk ) { + wxLogError("Can't save log contents to file."); + return; + } +} + +void wxLogFrame::OnClear(wxCommandEvent& event) +{ + m_pTextCtrl->Clear(); +} + +wxLogWindow::wxLogWindow(const wxTString& strTitle) +{ + m_pOldLog = wxLog::GetActiveTarget(); + m_pLogFrame = new wxLogFrame(strTitle); +} + +void wxLogWindow::Show(Bool bShow) +{ + m_pLogFrame->Show(bShow); +} + +void wxLogWindow::DoLog(wxLogLevel level, const char *szString) +{ + // first let the previous logger show it + if ( m_pOldLog != NULL ) { + // @@@ why can't we access protected wxLog method from here (we derive + // from wxLog)? gcc gives "DoLog is protected in this context", what + // does this mean? Anyhow, the cast is harmless and let's us do what + // we want. + ((wxLogWindow *)m_pOldLog)->DoLog(level, szString); + } + + // and this will format it nicely and call our DoLogString() + wxLog::DoLog(level, szString); +} + +void wxLogWindow::DoLogString(const char *szString) +{ + // put the text into our window + wxTextCtrl *pText = m_pLogFrame->TextCtrl(); + + // remove selection (WriteText is in fact ReplaceSelection) + long nLen = pText->GetLastPosition(); + pText->SetSelection(nLen, nLen); + + pText->WriteText(szString); + pText->WriteText("\n"); // "\n" ok here (_not_ "\r\n") + + // ensure that the line can be seen + // @@@ TODO +} + +wxLogWindow::~wxLogWindow() +{ + m_pLogFrame->Close(TRUE); +} + #endif //WX_TEST_MINIMAL // ============================================================================ @@ -456,8 +688,6 @@ void wxLogGui::DoLog(Level level, const char *szString) // ---------------------------------------------------------------------------- wxLog *wxLog::ms_pLogger = NULL; bool wxLog::ms_bInitialized = FALSE; -bool wxLog::ms_bVerbose = FALSE; -const char *wxLog::ms_szTimeFormat = "[%d/%b/%y %H:%M:%S] "; // ---------------------------------------------------------------------------- // stdout error logging helper @@ -467,8 +697,7 @@ const char *wxLog::ms_szTimeFormat = "[%d/%b/%y %H:%M:%S] "; // (looks more pretty on the terminal). Also adds newline at the end. // // @@ this is now disabled until I find a portable way of determining the -// terminal window size - +// terminal window size (ok, I found it but does anybody really cares?) #ifdef LOG_PRETTY_WRAP static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) { @@ -522,40 +751,40 @@ const char *wxSysErrorMsg(unsigned long nErrCode) if ( nErrCode == 0 ) nErrCode = wxSysErrorCode(); -#ifdef __WINDOWS__ -#ifdef __WIN32__ - static char s_szBuf[LOG_BUFFER_SIZE / 2]; - - // get error message from system - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, nErrCode, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)&lpMsgBuf, - 0, NULL); - - // copy it to our buffer and free memory - strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); - s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0'; - LocalFree(lpMsgBuf); - - // returned string is capitalized and ended with '\r\n' - bad - s_szBuf[0] = (char)tolower(s_szBuf[0]); - size_t len = strlen(s_szBuf); - if ( len > 0 ) { - // truncate string - if ( s_szBuf[len - 2] == '\r' ) - s_szBuf[len - 2] = '\0'; - } + #ifdef __WINDOWS__ + #ifdef __WIN32__ + static char s_szBuf[LOG_BUFFER_SIZE / 2]; + + // get error message from system + LPVOID lpMsgBuf; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, nErrCode, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPTSTR)&lpMsgBuf, + 0, NULL); + + // copy it to our buffer and free memory + strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); + s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0'; + LocalFree(lpMsgBuf); + + // returned string is capitalized and ended with '\r\n' - bad + s_szBuf[0] = (char)tolower(s_szBuf[0]); + size_t len = strlen(s_szBuf); + if ( len > 0 ) { + // truncate string + if ( s_szBuf[len - 2] == '\r' ) + s_szBuf[len - 2] = '\0'; + } - return s_szBuf; -#else - // TODO: Windows 3.1 - return NULL; -#endif -#else - return strerror(nErrCode); -#endif + return s_szBuf; + #else //Win16 + // TODO @@@@ + return NULL; + #endif // Win16/32 + #else // Unix + return strerror(nErrCode); + #endif // Win/Unix } // ---------------------------------------------------------------------------- @@ -600,11 +829,11 @@ void wxOnAssert(const char *szFile, int nLine, const char *szMsg) break; } } - #else + #else // !Windows // @@@@ don't know how to start the debugger under generic Unix s_bNoAsserts = TRUE; // suppress 'unused var' warning abort(); - #endif + #endif // Windows/!Windows } #endif //DEBUG -- 2.45.2