]> git.saurik.com Git - wxWidgets.git/commitdiff
added wxProtocolLog class for logging network requests/responses (closes #7464)
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 7 Mar 2009 13:58:39 +0000 (13:58 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 7 Mar 2009 13:58:39 +0000 (13:58 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59404 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

17 files changed:
Makefile.in
build/bakefiles/files.bkl
build/msw/wx_net.dsp
build/msw/wx_vc7_net.vcproj
build/msw/wx_vc8_net.vcproj
build/msw/wx_vc9_net.vcproj
docs/changes.txt
include/wx/protocol/log.h [new file with mode: 0644]
include/wx/protocol/protocol.h
interface/wx/protocol/log.h [new file with mode: 0644]
interface/wx/protocol/protocol.h
samples/console/console.cpp
src/common/ftp.cpp
src/common/protocol.cpp
wxGTK.spec
wxMotif.spec
wxX11.spec

index ae4db8a245ffca06a9056258f6db84288f0f48af..280a2cb067979d3519e4df3f5838c60d53b14049 100644 (file)
@@ -451,6 +451,7 @@ ALL_BASE_HEADERS =  \
        wx/protocol/file.h \
        wx/protocol/ftp.h \
        wx/protocol/http.h \
+       wx/protocol/log.h \
        wx/protocol/protocol.h \
        wx/sckaddr.h \
        wx/sckipc.h \
@@ -647,6 +648,7 @@ ALL_PORTS_BASE_HEADERS =  \
        wx/protocol/file.h \
        wx/protocol/ftp.h \
        wx/protocol/http.h \
+       wx/protocol/log.h \
        wx/protocol/protocol.h \
        wx/sckaddr.h \
        wx/sckipc.h \
index bb458ea25090e3c0889958ad540596246ebb874d..3aa2b73060e5c8bd5ca5161594613948ddc5a910 100644 (file)
@@ -580,6 +580,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
     wx/protocol/file.h
     wx/protocol/ftp.h
     wx/protocol/http.h
+    wx/protocol/log.h
     wx/protocol/protocol.h
     wx/sckaddr.h
     wx/sckipc.h
index 37dd4498c20e87f922dc4cc448c6feba15a0cd21..e2f61cf4e8a028ebbeecb32e668f447c5de21cb5 100644 (file)
@@ -598,6 +598,10 @@ SOURCE=..\..\include\wx\protocol\http.h
 # End Source File\r
 # Begin Source File\r
 \r
+SOURCE=..\..\include\wx\protocol\log.h\r
+# End Source File\r
+# Begin Source File\r
+\r
 SOURCE=..\..\include\wx\protocol\protocol.h\r
 # End Source File\r
 # Begin Source File\r
index 417d2463246ebfbd2fade6ceaeb75214141dc002..72d7730d07536e5f7d879eaa3439bcf0ea0823f4 100644 (file)
                        <File\r
                                RelativePath="..\..\include\wx\protocol\http.h">\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\include\wx\protocol\log.h">\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\protocol\protocol.h">\r
                        </File>\r
index 9f9af1b089c590473681d1d43b1a03da2a261ac3..90339ae10c6a1dc14f13b82fd227d6e7ca98499b 100644 (file)
                                RelativePath="..\..\include\wx\protocol\http.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\include\wx\protocol\log.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\protocol\protocol.h"\r
                                >\r
index 115f9a0328282bd76fa63d79f49b38c11a08fe11..25eefa32b7652c5a495754ad67018c579c3e4404 100644 (file)
                                RelativePath="..\..\include\wx\protocol\http.h"\r
                                >\r
                        </File>\r
+                       <File\r
+                               RelativePath="..\..\include\wx\protocol\log.h"\r
+                               >\r
+                       </File>\r
                        <File\r
                                RelativePath="..\..\include\wx\protocol\protocol.h"\r
                                >\r
index 3ca763a24bf6f6dbac0762ab4b0512878c63eb4e..9940b7bff45a9eb8a17e71b163d37335146da921 100644 (file)
@@ -370,6 +370,7 @@ All:
 - Added wxStrnlen() for safe computation of string length.
 - Added wxImage::Clear() (troelsk).
 - Added wxLog::Log().
+- Added wxProtocolLog and use it in wxFTP.
 - Added wxXmlResource::GetResourceNode().
 
 All (Unix):
diff --git a/include/wx/protocol/log.h b/include/wx/protocol/log.h
new file mode 100644 (file)
index 0000000..d11be42
--- /dev/null
@@ -0,0 +1,55 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        wx/protocol/log.h
+// Purpose:     wxProtocolLog class for logging network exchanges
+// Author:      Troelsk, Vadim Zeitlin
+// Created:     2009-03-06
+// RCS-ID:      $Id$
+// Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+#ifndef _WX_PROTOCOL_LOG_H_
+#define _WX_PROTOCOL_LOG_H_
+
+#include "wx/string.h"
+
+// ----------------------------------------------------------------------------
+// wxProtocolLog: simple class for logging network requests and responses
+// ----------------------------------------------------------------------------
+
+class WXDLLIMPEXP_NET wxProtocolLog
+{
+public:
+    // Create object doing the logging using wxLogTrace() with the specified
+    // trace mask.
+    wxProtocolLog(const wxString& traceMask)
+        : m_traceMask(traceMask)
+    {
+    }
+
+    // Virtual dtor for the base class
+    virtual ~wxProtocolLog() { }
+
+    // Called by wxProtocol-derived classes to actually log something
+    virtual void LogRequest(const wxString& str)
+    {
+        DoLogString("==> " + str);
+    }
+
+    virtual void LogResponse(const wxString& str)
+    {
+        DoLogString("<== " + str);
+    }
+
+protected:
+    // Can be overridden by the derived classes.
+    virtual void DoLogString(const wxString& str);
+
+private:
+    const wxString m_traceMask;
+
+    wxDECLARE_NO_COPY_CLASS(wxProtocolLog);
+};
+
+#endif // _WX_PROTOCOL_LOG_H_
+
index f26d22ba8910f2bf752b557ad1f12ec231c8a6b1..aaf3a63bf9ab6c255554019b4b26019a5afee7ba 100644 (file)
@@ -24,6 +24,8 @@
     #include "wx/socket.h"
 #endif
 
+class WXDLLIMPEXP_FWD_NET wxProtocolLog;
+
 // ----------------------------------------------------------------------------
 // constants
 // ----------------------------------------------------------------------------
@@ -55,6 +57,7 @@ class WXDLLIMPEXP_NET wxProtocol
 {
 public:
     wxProtocol();
+    virtual ~wxProtocol();
 
 #if wxUSE_SOCKETS
     bool Reconnect();
@@ -89,6 +92,30 @@ public:
         { SetDefaultTimeout(seconds); }
 
 
+    // logging support: each wxProtocol object may have the associated logger
+    // (by default there is none) which is used to log network requests and
+    // responses
+
+    // set the logger, deleting the old one and taking ownership of this one
+    void SetLog(wxProtocolLog *log);
+
+    // return the current logger, may be NULL
+    wxProtocolLog *GetLog() const { return m_log; }
+
+    // detach the existing logger without deleting it, the caller is
+    // responsible for deleting the returned pointer if it's non-NULL
+    wxProtocolLog *DetachLog()
+    {
+        wxProtocolLog * const log = m_log;
+        m_log = NULL;
+        return log;
+    }
+
+    // these functions forward to the same functions with the same names in
+    // wxProtocolLog if we have a valid logger and do nothing otherwise
+    void LogRequest(const wxString& str);
+    void LogResponse(const wxString& str);
+
 protected:
     // the timeout associated with the protocol:
     wxUint32        m_uiDefaultTimeout;
@@ -100,6 +127,8 @@ protected:
     wxProtocolError m_lastError;
 
 private:
+    wxProtocolLog *m_log;
+
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxProtocol)
 };
 
diff --git a/interface/wx/protocol/log.h b/interface/wx/protocol/log.h
new file mode 100644 (file)
index 0000000..7570a8d
--- /dev/null
@@ -0,0 +1,61 @@
+///////////////////////////////////////////////////////////////////////////////
+// Name:        wx/protocol/log.h
+// Purpose:     interface of wxProtocolLog
+// Author:      Vadim Zeitlin
+// Created:     2009-03-06
+// RCS-ID:      $Id$
+// Copyright:   (c) 2009 Vadim Zeitlin <vadim@wxwidgets.org>
+// Licence:     wxWindows licence
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+    Class allowing to log network operations performed by wxProtocol.
+
+    @library{wxnet}
+    @category{net}
+
+    @see wxProtocol
+*/
+class wxProtocolLog
+{
+public:
+    /**
+        Create object doing the logging using wxLogTrace() with the specified
+        trace mask.
+
+        If you override DoLogString() in your class the @a traceMask may be
+        left empty but it must have a valid value if you rely on the default
+        DoLogString() implementation.
+     */
+    wxProtocolLog(const wxString& traceMask);
+
+    /**
+        Called by wxProtocol-derived objects to log strings sent to the server.
+
+        Default implementation prepends a client-to-server marker to @a str and
+        calls DoLogString().
+     */
+    virtual void LogRequest(const wxString& str);
+
+    /**
+        Called by wxProtocol-derived objects to log strings received from the
+        server.
+
+        Default implementation prepends a server-to-client marker to @a str and
+        calls DoLogString().
+     */
+    virtual void LogResponse(const wxString& str);
+
+protected:
+    /**
+        Log the given string.
+
+        This function is called from LogRequest() and LogResponse() and by
+        default uses wxLogTrace() with the trace mask specified in the
+        constructor but can be overridden to do something different by the
+        derived classes.
+     */
+    virtual void DoLogString(const wxString& str);
+};
+
+
index ea076402de785f701f7f4b61d32c5d3b21fceb01..dc1586ac52883e4280eb13b0c26a569ceee9d1e7 100644 (file)
@@ -1,5 +1,5 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        protocol/protocol.h
+// Name:        wx/protocol/protocol.h
 // Purpose:     interface of wxProtocol
 // Author:      wxWidgets team
 // RCS-ID:      $Id$
@@ -107,5 +107,50 @@ public:
         @see wxSocketBase::SetTimeout
     */
     void SetDefaultTimeout(wxUint32 Value);
+
+    /**
+        @name Logging support.
+
+        Each wxProtocol object may have the associated logger (by default there
+        is none) which is used to log network requests and responses.
+
+        @see wxProtocolLog
+    */
+    //@{
+
+    /**
+        Set the logger, deleting the old one and taking ownership of this one.
+
+        @param log
+            New logger allocated on the heap or @NULL.
+     */
+    void SetLog(wxProtocolLog *log);
+
+    /**
+        Return the current logger, may be @NULL.
+     */
+    wxProtocolLog *GetLog() const { return m_log; }
+
+    /**
+        Detach the existing logger without deleting it.
+        
+        The caller is responsible for deleting the returned pointer if it's
+        non-@c NULL.
+     */
+    wxProtocolLog *DetachLog();
+
+    /**
+        Call wxProtocolLog::LogRequest() if we have a valid logger or do
+        nothing otherwise.
+     */
+    void LogRequest(const wxString& str);
+
+    /**
+        Call wxProtocolLog::LogResponse() if we have a valid logger or do
+        nothing otherwise.
+     */
+    void LogResponse(const wxString& str);
+
+    //@}
 };
 
index 8a9a0705c6eeceeea06dd7d531f884233ad62a81..a1af04971cc8935c4b02e24d35eb85ef7b40bb0c 100644 (file)
@@ -2455,6 +2455,7 @@ static void TestSocketClient()
 #ifdef TEST_FTP
 
 #include "wx/protocol/ftp.h"
+#include "wx/protocol/log.h"
 
 #define FTP_ANONYMOUS
 
@@ -4403,6 +4404,7 @@ int main(int argc, char **argv)
     // wxFTP cannot be a static variable as its ctor needs to access
     // wxWidgets internals after it has been initialized
     ftp = new wxFTP;
+    ftp->SetLog(new wxProtocolLog(FTP_TRACE_MASK));
 
     if ( TestFtpConnect() )
     {
index e2854eeb6b8840c762c786a331a4bc7379b9193c..9b1cd2513b62964d71b2a02b112eeadf84ccaf04 100644 (file)
@@ -257,7 +257,7 @@ char wxFTP::SendCommand(const wxString& command)
         cmd = command;
     }
 
-    wxLogTrace(FTP_TRACE_MASK, _T("==> %s"), cmd.c_str());
+    LogRequest(cmd);
 #endif // __WXDEBUG__
 
     m_lastError = wxPROTO_NOERR;
@@ -301,6 +301,8 @@ char wxFTP::GetResult()
             return 0;
         }
 
+        LogResponse(line);
+
         if ( !m_lastResult.empty() )
         {
             // separate from last line
@@ -317,11 +319,6 @@ char wxFTP::GetResult()
             {
                 badReply = true;
             }
-            else
-            {
-                wxLogTrace(FTP_TRACE_MASK, _T("<== %s %s"),
-                           code.c_str(), line.c_str());
-            }
         }
         else // line has at least 4 chars
         {
@@ -331,8 +328,6 @@ char wxFTP::GetResult()
             if ( firstLine )
             {
                 code = wxString(line, LEN_CODE);
-                wxLogTrace(FTP_TRACE_MASK, _T("<== %s %s"),
-                           code.c_str(), line.c_str() + LEN_CODE + 1);
 
                 switch ( chMarker )
                 {
@@ -357,15 +352,6 @@ char wxFTP::GetResult()
                     {
                         endOfReply = true;
                     }
-
-                    wxLogTrace(FTP_TRACE_MASK, _T("<== %s %s"),
-                               code.c_str(), line.c_str() + LEN_CODE + 1);
-                }
-                else
-                {
-                    // just part of reply
-                    wxLogTrace(FTP_TRACE_MASK, _T("<== %s %s"),
-                               code.c_str(), line.c_str());
                 }
             }
         }
@@ -1008,8 +994,7 @@ int wxFTP::GetFileSize(const wxString& fileName)
                                           &filesize) != 9 )
                             {
                                 // Hmm... Invalid response
-                                wxLogTrace(FTP_TRACE_MASK,
-                                           _T("Invalid LIST response"));
+                                wxLogDebug(wxT("Invalid LIST response"));
                             }
                         }
                         else // Windows-style response (?)
@@ -1019,8 +1004,7 @@ int wxFTP::GetFileSize(const wxString& fileName)
                                           &filesize) != 4 )
                             {
                                 // something bad happened..?
-                                wxLogTrace(FTP_TRACE_MASK,
-                                           _T("Invalid or unknown LIST response"));
+                                wxLogDebug(wxT("Invalid or unknown LIST response"));
                             }
                         }
                     }
index 4a06cf857b9ee1591ac23f58deb0fd6b22fc0e16..2dfb4b40ab04eac1b0653f478772c96aa7e49bfa 100644 (file)
@@ -19,6 +19,7 @@
 #if wxUSE_PROTOCOL
 
 #include "wx/protocol/protocol.h"
+#include "wx/protocol/log.h"
 
 #ifndef WX_PRECOMP
     #include "wx/module.h"
@@ -64,6 +65,7 @@ wxProtocol::wxProtocol()
 #endif
 {
     m_lastError = wxPROTO_NOERR;
+    m_log = NULL;
     SetDefaultTimeout(60);      // default timeout is 60 seconds
 }
 
@@ -95,6 +97,10 @@ void wxProtocol::SetDefaultTimeout(wxUint32 Value)
 #endif
 }
 
+wxProtocol::~wxProtocol()
+{
+    delete m_log;
+}
 
 // ----------------------------------------------------------------------------
 // Read a line from socket
@@ -179,4 +185,31 @@ wxProtocolError wxProtocol::ReadLine(wxString& result)
 
 #endif // wxUSE_SOCKETS
 
+// ----------------------------------------------------------------------------
+// logging
+// ----------------------------------------------------------------------------
+
+void wxProtocol::SetLog(wxProtocolLog *log)
+{
+    delete m_log;
+    m_log = log;
+}
+
+void wxProtocol::LogRequest(const wxString& str)
+{
+    if ( m_log )
+        m_log->LogRequest(str);
+}
+
+void wxProtocol::LogResponse(const wxString& str)
+{
+    if ( m_log )
+        m_log->LogResponse(str);
+}
+
+void wxProtocolLog::DoLogString(const wxString& WXUNUSED_UNLESS_DEBUG(str))
+{
+    wxLogTrace(m_traceMask, "%s", str);
+}
+
 #endif // wxUSE_PROTOCOL
index 7bd321f1ff3a3b2ddfdfe42be33f4e193c131ea4..aa3ca9669b7d98c07e6355f5075c868681081b2f 100644 (file)
@@ -360,6 +360,7 @@ wx/fs_inet.h
 wx/protocol/file.h
 wx/protocol/ftp.h
 wx/protocol/http.h
+wx/protocol/log.h
 wx/protocol/protocol.h
 wx/sckaddr.h
 wx/sckipc.h
index d1c674579bf407535fda9c3a76ba894e03c7519b..03ac42fe174f6b43f2c18fe513856d6bae0c7b22 100644 (file)
@@ -265,6 +265,7 @@ wx/fs_inet.h
 wx/protocol/file.h
 wx/protocol/ftp.h
 wx/protocol/http.h
+wx/protocol/log.h
 wx/protocol/protocol.h
 wx/sckaddr.h
 wx/sckipc.h
index 5a532f693fe6748599aa6618e2b67e55b57f8aa3..657ae1452939a4160bbe609084586c39d6fdaba3 100644 (file)
@@ -289,6 +289,7 @@ wx/fs_inet.h
 wx/protocol/file.h
 wx/protocol/ftp.h
 wx/protocol/http.h
+wx/protocol/log.h
 wx/protocol/protocol.h
 wx/sckaddr.h
 wx/sckipc.h