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
This commit is contained in:
Vadim Zeitlin
1998-05-26 15:11:46 +00:00
parent 9d8046f6fe
commit 9ef3052c91
2 changed files with 461 additions and 175 deletions

View File

@@ -13,39 +13,54 @@
#define __LOGH__ #define __LOGH__
#ifdef __GNUG__ #ifdef __GNUG__
#pragma interface "log.h" #pragma interface
#endif //GNU C++ #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 // 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 // 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 class WXDLLEXPORT wxLog
{ {
public: 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 // ctor
wxLog(); wxLog();
// sink function // 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); } { if ( ms_pLogger != 0 ) ms_pLogger->DoLog(level, szString); }
// message buffering // message buffering
@@ -53,7 +68,9 @@ public:
// (FILE and iostream logs don't need it, but wxGuiLog does to avoid // (FILE and iostream logs don't need it, but wxGuiLog does to avoid
// showing 17 modal dialogs one after another) // showing 17 modal dialogs one after another)
virtual void Flush(); 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; } bool HasPendingMessages() const { return m_bHasMessages; }
// only one sink is active at each moment // only one sink is active at each moment
@@ -65,16 +82,22 @@ public:
// functions controlling the default wxLog behaviour // functions controlling the default wxLog behaviour
// verbose mode is activated by standard command-line '-verbose' option // 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 // sets the format for timestamp prepended by wxLog::DoLog(): it's
// passed to strftime() function, see it's documentation for details. // passed to strftime() function, see it's documentation for details.
// the string is not copied! // no time stamp at all if szTF is NULL or empty
static void SetTimeStampFormat(const char *szTimeFormat) // NB: the string is not copied, so it's lifetime must be long enough!
{ ms_szTimeFormat = szTimeFormat; } void SetTimeStampFormat(const char *szTF) { m_szTimeFormat = szTF; }
// trace mask (see wxTraceXXX constants for details)
void SetTraceMask(wxTraceMask ulMask) { m_ulTraceMask = ulMask; }
// accessors // accessors
// gets the verbose status // 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 // make dtor virtual for all derived classes
virtual ~wxLog() { } virtual ~wxLog() { }
@@ -82,20 +105,21 @@ public:
protected: protected:
bool m_bHasMessages; bool m_bHasMessages;
// static variables bool m_bVerbose; // FALSE => ignore LogInfo messages
// ---------------- const char *m_szTimeFormat; // format for strftime()
static bool ms_bVerbose; // FALSE => ignore LogInfo messages wxTraceMask m_ulTraceMask; // controls wxLogTrace behaviour
static const char *ms_szTimeFormat; // format for strftime()
private:
// the logging functions that can be overriden // the logging functions that can be overriden
// default DoLog() prepends the time stamp and a prefix corresponding // default DoLog() prepends the time stamp and a prefix corresponding
// to the message to szString and then passes it to DoLogString() // 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 // default DoLogString does nothing but is not pure virtual because if
// you override DoLog() you might not need it at all // you override DoLog() you might not need it at all
virtual void DoLogString(const char *szString); virtual void DoLogString(const char *szString);
private:
// static variables
// ----------------
static wxLog *ms_pLogger; // currently active log sink static wxLog *ms_pLogger; // currently active log sink
static bool ms_bInitialized; // any log targets created? static bool ms_bInitialized; // any log targets created?
}; };
@@ -133,8 +157,8 @@ protected:
ostream *m_ostr; ostream *m_ostr;
}; };
/*
// log everything to a text window (GUI only of course) // log everything to a text window (GUI only of course)
class wxTextCtrl;
class WXDLLEXPORT wxLogTextCtrl : public wxLogStream class WXDLLEXPORT wxLogTextCtrl : public wxLogStream
{ {
public: public:
@@ -142,7 +166,6 @@ public:
wxLogTextCtrl(wxTextCtrl *pTextCtrl); wxLogTextCtrl(wxTextCtrl *pTextCtrl);
~wxLogTextCtrl(); ~wxLogTextCtrl();
}; };
*/
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// GUI log target, the default one for wxWindows programs // GUI log target, the default one for wxWindows programs
@@ -157,12 +180,37 @@ public:
virtual void Flush(); virtual void Flush();
protected: protected:
virtual void DoLog(Level level, const char *szString); virtual void DoLog(wxLogLevel level, const char *szString);
wxArrayString m_aMessages; wxArrayString m_aMessages;
bool m_bErrors; 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 // /dev/null log target: suppress logging until this object goes out of scope
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -209,7 +257,7 @@ private:
// an array, function or reference :-( // an array, function or reference :-(
// the most generic log function // 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) \ #define DECLARE_LOG_FUNCTION(level) \
extern void WXDLLEXPORT wxLog##level(wxTString strFormat, ...) 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)) // that don't set the errno (like registry APIs in Win32))
void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...); void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...);
// debug functions don't translate their arguments // debug functions do nothing in release mode
#undef DECLARE_LOG_FUNCTION #ifdef __DEBUG__
#define DECLARE_LOG_FUNCTION(level) \ // NB: debug functions don't translate their arguments
extern void WXDLLEXPORT wxLog##level(const char *szFormat, ...) 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? // are we in 'verbose' mode?
// (note that it's often handy to change this var manually from the // (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).", \ wxLogDebug("At %s(%d) '%s' failed with error %lx (%s).", \
__FILE__, __LINE__, api, \ __FILE__, __LINE__, api, \
rc, wxSysErrorMsg(rc)) rc, wxSysErrorMsg(rc))
#define wxLogLastError(api) wxLogApiError(api, ::GetLastError()) #define wxLogLastError(api) wxLogApiError(api, wxSysErrorCode())
#else //!debug #else //!debug
#define wxLogApiError(api, rc) #define wxLogApiError(api, rc)
#define wxLogLastError(api) #define wxLogLastError(api)

View File

@@ -29,12 +29,17 @@
// wxWindows // wxWindows
#ifndef WX_PRECOMP #ifndef WX_PRECOMP
#include <wx/string.h>
#include <wx/app.h> #include <wx/app.h>
#include <wx/generic/msgdlgg.h> #include <wx/string.h>
#endif #include <wx/intl.h>
#include <wx/intl.h> #include <wx/generic/msgdlgg.h>
#include <wx/filedlg.h>
#include <wx/textctrl.h>
#endif //WX_PRECOMP
#include <wx/file.h>
#include <wx/textfile.h>
#include <wx/log.h> #include <wx/log.h>
// other standard headers // other standard headers
@@ -42,13 +47,10 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
// C++ headers
#include <iostream.h>
// _WINDOWS_ is defined when windows.h is included, // _WINDOWS_ is defined when windows.h is included,
// __WINDOWS__ is defined for MS Windows compilation // __WINDOWS__ is defined for MS Windows compilation
#if defined(__WINDOWS__) && !defined(_WINDOWS_) #if defined(__WINDOWS__) && !defined(_WINDOWS_)
#include <windows.h> #include <windows.h>
#endif //windows.h #endif //windows.h
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -58,7 +60,7 @@
// define this to enable wrapping of log messages // define this to enable wrapping of log messages
//#define LOG_PRETTY_WRAP //#define LOG_PRETTY_WRAP
#ifdef LOG_PRETTY_WRAP #ifdef LOG_PRETTY_WRAP
static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz); static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz);
#endif #endif
@@ -81,16 +83,16 @@
static char s_szBuf[LOG_BUFFER_SIZE]; static char s_szBuf[LOG_BUFFER_SIZE];
// generic log function // generic log function
void wxLogGeneric(wxLog::Level level, wxTString strFormat, ...) void wxLogGeneric(wxLogLevel level, wxTString strFormat, ...)
{ {
if ( wxLog::GetActiveTarget() != NULL ) { if ( wxLog::GetActiveTarget() != NULL ) {
va_list argptr; va_list argptr;
va_start(argptr, strFormat); va_start(argptr, strFormat);
vsprintf(s_szBuf, strFormat, argptr); vsprintf(s_szBuf, strFormat, argptr);
va_end(argptr); va_end(argptr);
wxLog::OnLog(level, s_szBuf); wxLog::OnLog(level, s_szBuf);
} }
} }
#define IMPLEMENT_LOG_FUNCTION(level) \ #define IMPLEMENT_LOG_FUNCTION(level) \
@@ -102,7 +104,7 @@ void wxLogGeneric(wxLog::Level level, wxTString strFormat, ...)
vsprintf(s_szBuf, strFormat, argptr); \ vsprintf(s_szBuf, strFormat, argptr); \
va_end(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(Info)
IMPLEMENT_LOG_FUNCTION(Status) IMPLEMENT_LOG_FUNCTION(Status)
// debug functions don't use wxTString // same as info, but only if 'verbose' mode is on
#undef IMPLEMENT_LOG_FUNCTION void wxLogVerbose(wxTString strFormat, ...)
#define IMPLEMENT_LOG_FUNCTION(level) \ {
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, ...) \ void wxLog##level(const char *szFormat, ...) \
{ \ { \
if ( wxLog::GetActiveTarget() != NULL ) { \ if ( wxLog::GetActiveTarget() != NULL ) { \
@@ -124,55 +140,65 @@ IMPLEMENT_LOG_FUNCTION(Status)
vsprintf(s_szBuf, szFormat, argptr); \ vsprintf(s_szBuf, szFormat, argptr); \
va_end(argptr); \ va_end(argptr); \
\ \
wxLog::OnLog(wxLog::level, s_szBuf); \ wxLog::OnLog(wxLOG_##level, s_szBuf); \
} \ } \
} }
IMPLEMENT_LOG_FUNCTION(Debug) void wxLogTrace(wxTraceMask mask, const char *szFormat, ...)
IMPLEMENT_LOG_FUNCTION(Trace) {
wxLog *pLog = wxLog::GetActiveTarget();
void wxLogVerbose(wxTString strFormat, ...) // we check that all of mask bits are set in the current mask, so
{ // that wxLogTrace(wxTraceRefCount | wxTraceOle) will only do something
if ( wxLog::GetVerbose() && wxLog::GetActiveTarget() != NULL ) { // if both bits are set.
va_list argptr; if ( pLog != NULL && (pLog->GetTraceMask() & mask == mask) ) {
va_start(argptr, strFormat); va_list argptr;
vsprintf(s_szBuf, strFormat, argptr); va_start(argptr, szFormat);
va_end(argptr); vsprintf(s_szBuf, szFormat, argptr);
va_end(argptr);
wxLog::OnLog(wxLog::Info, s_szBuf);
}
}
void wxLogSysError(wxTString str, ...)
{
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]; wxLog::OnLog(wxLOG_Trace, s_szBuf);
sprintf(szErrMsg, _(" (error %ld: %s)"), wxSysErrorCode(), wxSysErrorMsg()); }
strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf));
wxLog::OnLog(wxLog::Error, 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)
{
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);
}
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, ...) void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...)
{ {
if ( wxLog::GetActiveTarget() != NULL ) { va_list argptr;
va_list argptr; va_start(argptr, strFormat);
va_start(argptr, strFormat); vsprintf(s_szBuf, strFormat, argptr);
vsprintf(s_szBuf, strFormat, argptr); va_end(argptr);
va_end(argptr);
char szErrMsg[LOG_BUFFER_SIZE / 2]; wxLogSysErrorHelper(lErrCode);
sprintf(szErrMsg, _(" (error %ld: %s)"), lErrCode, wxSysErrorMsg(lErrCode));
strncat(s_szBuf, szErrMsg, WXSIZEOF(s_szBuf) - strlen(s_szBuf));
wxLog::OnLog(wxLog::Error, s_szBuf);
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -182,19 +208,24 @@ void WXDLLEXPORT wxLogSysError(long lErrCode, wxTString strFormat, ...)
wxLog::wxLog() wxLog::wxLog()
{ {
m_bHasMessages = FALSE; m_bHasMessages = FALSE;
m_bVerbose = FALSE;
m_szTimeFormat = "[%d/%b/%y %H:%M:%S] ";
m_ulTraceMask = (wxTraceMask)-1; // set all bits
} }
wxLog *wxLog::GetActiveTarget() wxLog *wxLog::GetActiveTarget()
{ {
if ( !ms_bInitialized ) { 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; ms_bInitialized = TRUE;
// ask the application to create a log target for us if it exists #ifdef WX_TEST_MINIMAL
if ( wxTheApp != NULL ) ms_pLogger = new wxLogStderr;
#else
// ask the application to create a log target for us
ms_pLogger = wxTheApp->CreateLogTarget(); ms_pLogger = wxTheApp->CreateLogTarget();
else #endif
ms_pLogger = new wxLogStderr;
// do nothing if it fails - what can we do? // do nothing if it fails - what can we do?
} }
@@ -215,53 +246,58 @@ wxLog *wxLog::SetActiveTarget(wxLog *pLogger)
return pOldLogger; return pOldLogger;
} }
void wxLog::DoLog(Level level, const char *szString) void wxLog::DoLog(wxLogLevel level, const char *szString)
{ {
char szBuf[128]; wxString str;
time_t timeNow;
struct tm *ptmNow;
time(&timeNow); // prepend a timestamp if not disabled
ptmNow = localtime(&timeNow); if ( !IsEmpty(m_szTimeFormat) ) {
char szBuf[128];
time_t timeNow;
struct tm *ptmNow;
strftime(szBuf, WXSIZEOF(szBuf), ms_szTimeFormat, ptmNow); time(&timeNow);
wxString str = szBuf; ptmNow = localtime(&timeNow);
strftime(szBuf, WXSIZEOF(szBuf), m_szTimeFormat, ptmNow);
str = szBuf;
}
switch ( level ) { switch ( level ) {
case FatalError: case wxLOG_FatalError:
DoLogString(str << _("Fatal error: ") << szString); DoLogString(str << _("Fatal error: ") << szString);
DoLogString(_("Program aborted.")); DoLogString(_("Program aborted."));
Flush(); Flush();
abort(); abort();
break; break;
case Error: case wxLOG_Error:
DoLogString(str << _("Error: ") << szString); DoLogString(str << _("Error: ") << szString);
break; break;
case Warning: case wxLOG_Warning:
DoLogString(str << _("Warning: ") << szString); DoLogString(str << _("Warning: ") << szString);
break; break;
case Info: case wxLOG_Info:
if ( GetVerbose() ) if ( GetVerbose() )
case Message: case wxLOG_Message:
DoLogString(str + szString); DoLogString(str + szString);
// fall through // fall through
case Status: case wxLOG_Status:
// nothing to do // nothing to do
break; break;
case Trace: case wxLOG_Trace:
case Debug: case wxLOG_Debug:
#ifdef __DEBUG__ #ifdef __DEBUG__
#ifdef __WIN32__ #ifdef __WIN32__
// in addition to normal logging, also send the string to debugger // in addition to normal logging, also send the string to debugger
// (don't prepend "Debug" here: it will go to debug window anyhow) // (don't prepend "Debug" here: it will go to debug window anyhow)
::OutputDebugString(str + szString + "\n\r"); ::OutputDebugString(str + szString + "\n\r");
#endif //Win32 #endif //Win32
DoLogString(str << (level == Trace ? _("Trace") : _("Debug")) DoLogString(str << (level == wxLOG_Trace ? "Trace" : "Debug")
<< ": " << szString); << ": " << szString);
#endif #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."); wxFAIL_MSG("DoLogString must be overrided if it's called.");
} }
@@ -321,18 +357,20 @@ void wxLogStream::DoLogString(const char *szString)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxLogTextCtrl implementation // wxLogTextCtrl implementation
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
/*
wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl) wxLogTextCtrl::wxLogTextCtrl(wxTextCtrl *pTextCtrl)
// @@@ TODO: in wxGTK wxTextCtrl doesn't derive from streambuf
#ifndef __GTK__
: wxLogStream(new ostream(pTextCtrl)) : wxLogStream(new ostream(pTextCtrl))
#endif //GTK
{ {
} }
wxLogTextCtrl::~wxLogTextCtrl() wxLogTextCtrl::~wxLogTextCtrl()
{ {
delete m_ostr; #ifndef __GTK__
delete m_ostr;
#endif //GTK
} }
*/
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxLogGui implementation // wxLogGui implementation
@@ -385,19 +423,19 @@ void wxLogGui::Flush()
// the default behaviour is to discard all informational messages if there // the default behaviour is to discard all informational messages if there
// are any errors/warnings. // are any errors/warnings.
void wxLogGui::DoLog(Level level, const char *szString) void wxLogGui::DoLog(wxLogLevel level, const char *szString)
{ {
switch ( level ) { switch ( level ) {
case Info: case wxLOG_Info:
if ( GetVerbose() ) if ( GetVerbose() )
case Message: case wxLOG_Message:
if ( !m_bErrors ) { if ( !m_bErrors ) {
m_aMessages.Add(szString); m_aMessages.Add(szString);
m_bHasMessages = TRUE; m_bHasMessages = TRUE;
} }
break; break;
case Status: case wxLOG_Status:
{ {
// find the top window and set it's status text if it has any // find the top window and set it's status text if it has any
wxWindow *pWin = wxTheApp->GetTopWindow(); wxWindow *pWin = wxTheApp->GetTopWindow();
@@ -408,8 +446,8 @@ void wxLogGui::DoLog(Level level, const char *szString)
} }
break; break;
case Trace: case wxLOG_Trace:
case Debug: case wxLOG_Debug:
#ifdef __DEBUG__ #ifdef __DEBUG__
#ifdef __WIN32__ #ifdef __WIN32__
OutputDebugString(szString); OutputDebugString(szString);
@@ -423,13 +461,13 @@ void wxLogGui::DoLog(Level level, const char *szString)
#endif #endif
break; break;
case FatalError: case wxLOG_FatalError:
// show this one immediately // show this one immediately
wxMessageBox(szString, "Fatal error", wxICON_HAND); wxMessageBox(szString, "Fatal error", wxICON_HAND);
break; break;
case Error: case wxLOG_Error:
case Warning: case wxLOG_Warning:
// discard earlier informational messages if this is the 1st error // discard earlier informational messages if this is the 1st error
if ( !m_bErrors ) { if ( !m_bErrors ) {
m_aMessages.Empty(); 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 #endif //WX_TEST_MINIMAL
// ============================================================================ // ============================================================================
@@ -456,8 +688,6 @@ void wxLogGui::DoLog(Level level, const char *szString)
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxLog *wxLog::ms_pLogger = NULL; wxLog *wxLog::ms_pLogger = NULL;
bool wxLog::ms_bInitialized = FALSE; 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 // 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. // (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 // @@ 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 #ifdef LOG_PRETTY_WRAP
static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz) static void wxLogWrap(FILE *f, const char *pszPrefix, const char *psz)
{ {
@@ -522,40 +751,40 @@ const char *wxSysErrorMsg(unsigned long nErrCode)
if ( nErrCode == 0 ) if ( nErrCode == 0 )
nErrCode = wxSysErrorCode(); nErrCode = wxSysErrorCode();
#ifdef __WINDOWS__ #ifdef __WINDOWS__
#ifdef __WIN32__ #ifdef __WIN32__
static char s_szBuf[LOG_BUFFER_SIZE / 2]; static char s_szBuf[LOG_BUFFER_SIZE / 2];
// get error message from system // get error message from system
LPVOID lpMsgBuf; LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, nErrCode, NULL, nErrCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, (LPTSTR)&lpMsgBuf,
0, NULL); 0, NULL);
// copy it to our buffer and free memory // copy it to our buffer and free memory
strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1); strncpy(s_szBuf, (const char *)lpMsgBuf, WXSIZEOF(s_szBuf) - 1);
s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0'; s_szBuf[WXSIZEOF(s_szBuf) - 1] = '\0';
LocalFree(lpMsgBuf); LocalFree(lpMsgBuf);
// returned string is capitalized and ended with '\r\n' - bad // returned string is capitalized and ended with '\r\n' - bad
s_szBuf[0] = (char)tolower(s_szBuf[0]); s_szBuf[0] = (char)tolower(s_szBuf[0]);
size_t len = strlen(s_szBuf); size_t len = strlen(s_szBuf);
if ( len > 0 ) { if ( len > 0 ) {
// truncate string // truncate string
if ( s_szBuf[len - 2] == '\r' ) if ( s_szBuf[len - 2] == '\r' )
s_szBuf[len - 2] = '\0'; s_szBuf[len - 2] = '\0';
} }
return s_szBuf; return s_szBuf;
#else #else //Win16
// TODO: Windows 3.1 // TODO @@@@
return NULL; return NULL;
#endif #endif // Win16/32
#else #else // Unix
return strerror(nErrCode); return strerror(nErrCode);
#endif #endif // Win/Unix
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@@ -600,11 +829,11 @@ void wxOnAssert(const char *szFile, int nLine, const char *szMsg)
break; break;
} }
} }
#else #else // !Windows
// @@@@ don't know how to start the debugger under generic Unix // @@@@ don't know how to start the debugger under generic Unix
s_bNoAsserts = TRUE; // suppress 'unused var' warning s_bNoAsserts = TRUE; // suppress 'unused var' warning
abort(); abort();
#endif #endif // Windows/!Windows
} }
#endif //DEBUG #endif //DEBUG