OSDN Git Service

Implement user selection of traceable features at run-time.
authorKeith Marshall <keithmarshall@users.sourceforge.net>
Sun, 29 May 2011 20:53:37 +0000 (20:53 +0000)
committerKeith Marshall <keithmarshall@users.sourceforge.net>
Sun, 29 May 2011 20:53:37 +0000 (20:53 +0000)
ChangeLog
src/clistub.c
src/debug.h
src/pkginet.cpp
src/pkgopts.h
src/pkgunst.cpp
src/sysroot.cpp

index b2d512d..40316a1 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2011-05-29  Keith Marshall  <keithmarshall@users.sourceforge.net>
+
+       Implement user selection of traceable features at run-time.
+
+       * src.pkgopts.h (OPTION_VERBOSE_MAX): New macro; define it.
+
+       * src/debug.h (DEBUG_ENABLED): New macro; define it.
+       (DEBUG_REQUEST, DEBUG_REQUEST_FLAGS, DEBUG_TRACE_DYNAMIC): Likewise.
+
+       * src/clistub.c (main) [--trace]: Don't make it available unless...
+       [DEBUG_ENABLED(DEBUG_TRACE_DYNAMIC)]: ...this is set (at compile-time).
+       [--verbose]: Limit to OPTION_VERBOSE_MAX, using...
+       (atmost): ...this new macro; define it.
+       (options): Declare `-v' as both long and short form, pending bug fix
+       in MinGW's getopt_long_only() implementation.
+
+       * src/pkginet.cpp (pkgInternetAgent::OpenURL): Use DEBUG_REQUEST.
+       (pkgInternetLzmaStreamingAgent::TransferData): Likewise.
+       (pkgInternetStreamingAgent::TransferData): Likewise.
+       (pkgInternetStreamingAgent::Get): Likewise.
+
+       * src/pkgunst.cpp (pkgActionItem::SetAuthorities): Use DEBUG_REQUEST.
+       (pkg_rmdir, pkg_unlink): Likewise.
+
+       * src/sysroot.cpp (pkgXmlDocument::LoadSystemMap): Use DEBUG_REQUEST.
+
 2011-05-21  Keith Marshall  <keithmarshall@users.sourceforge.net>
 
        Implement protocol for processing global program options.
index a82f82d..38afe6a 100644 (file)
@@ -27,6 +27,8 @@
 #define _WIN32_WINNT 0x500
 #define WIN32_LEAN_AND_MEAN
 
+#include "debug.h"
+
 #include <windows.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -158,11 +160,14 @@ extern const char *version_identification;
 
 static const char *help_text =
 "Manage MinGW and MSYS installations (command line user interface).\n\n"
+
 "Usage:\n"
 "  mingw-get [OPTIONS] ACTION [package-spec ...]\n\n"
+
 "  mingw-get update\n"
 "  mingw-get [OPTIONS] {install | upgrade | remove} package-spec ...\n"
 "  mingw-get [OPTIONS] {show | list} [package-spec ...]\n\n"
+
 "Options:\n"
 "  --help, -h      Show this help text\n"
 "  --version, -V   Show version and licence information\n"
@@ -170,7 +175,11 @@ static const char *help_text =
 "                  progress reporting output; repeat up\n"
 "                  to three times for maximum verbosity\n"
 "  --verbose=N     Set verbosity level to N; (0 <= N <= 3)\n"
-#if DEBUGLEVEL & DEBUG_TRACE_DYNAMIC
+
+/* The "--trace" option is available only when dynamic tracing
+ * debugging support is compiled in; don't advertise it otherwise.
+ */
+#if DEBUG_ENABLED( DEBUG_TRACE_DYNAMIC )
 "  --trace=N       Enable tracing feature N; (debugging aid)\n"
 #endif
 "\n"
@@ -180,10 +189,12 @@ static const char *help_text =
 "  install         Install new packages\n"
 "  upgrade         Upgrade previously installed packages\n"
 "  remove          Remove previously installed packages\n\n"
+
 "Package Specifications:\n"
 "  [subsystem-]name[-component]:\n"
 "  msys-bash-doc   The 'doc' component of the bash package for MSYS\n"
 "  mingw32-gdb     All components of the gdb package for MinGW\n\n"
+
 "Use 'mingw-get list' to identify possible package names\n"
 "and the components associated with each.\n\n";
 
@@ -319,7 +330,7 @@ static int xatoi( const char *input )
   return result;
 }
 
-#define minval( a, b )  ((a) < (b)) ? (a) : (b)
+#define atmost( lim, val )             ((lim) < (val)) ? (lim) : (val)
 
 int main( int argc, char **argv )
 {
@@ -344,15 +355,30 @@ int main( int argc, char **argv )
     int optref;
     struct option options[] =
     {
-      /* Option Name      Argument Category    Store To   Return Value
+      /* FIXME: There is a bug in MinGW's library implementation of the
+       * getopt_long_only() function, which prevents it from correctly
+       * identifying "-v" when intended as a short form option, in the
+       * presence of "-verbose" vs. "-version" ambiguity.
+       *
+       * Until this bug has been fixed, we must explicitly declare "-v"
+       * both here, as a long form option, and as a short form option in
+       * the subsequent getopts_long_only() function call.
+       *
+       * Option Name      Argument Category    Store To   Return Value
        * --------------   ------------------   --------   --------------
        */
       { "v",              no_argument,         NULL,      'v'            },
       { "version",        no_argument,         NULL,      'V'            },
       { "help",           no_argument,         NULL,      'h'            },
-      { "trace",          required_argument,   &optref,   OPTION_TRACE   },
       { "verbose",        optional_argument,   NULL,      OPTION_VERBOSE },
 
+#     if DEBUG_ENABLED( DEBUG_TRACE_DYNAMIC )
+       /* The "--trace" option is supported only when dynamic tracing
+        * debugging support has been compiled in.
+        */
+      { "trace",          required_argument,   &optref,   OPTION_TRACE   },
+#     endif
+
       /* This list must be terminated by a null definition...
        */
       { NULL, 0, NULL, 0 }
@@ -366,7 +392,7 @@ int main( int argc, char **argv )
        */
       parsed_options.flags[opt].numeric = 0;
 
-    while( (opt = getopt_long_only( argc, argv, "Vh", options, &offset )) != -1 )
+    while( (opt = getopt_long_only( argc, argv, "vVh", options, &offset )) != -1 )
       /*
        * Parse any user specified options from the command line...
        */
@@ -394,9 +420,9 @@ int main( int argc, char **argv )
          {
            /* This is the case where an explicit level was specified...
             */
-           unsigned verbosity = xatoi( optarg );
-           parsed_options.flags[OPTION_FLAGS].numeric &= ~OPTION_VERBOSE;
-           parsed_options.flags[OPTION_FLAGS].numeric |= minval( verbosity, 3 );
+           parsed_options.flags[OPTION_FLAGS].numeric =
+             (parsed_options.flags[OPTION_FLAGS].numeric & ~OPTION_VERBOSE)
+             | atmost( OPTION_VERBOSE_MAX, xatoi( optarg ));
            break;
          }
 
index 68d0b96..e70f6ef 100644 (file)
    * to selectively enable compilation of (any specific class of)
    * debug specific code.
    */
+#  define DEBUG_ENABLED(FEATURE)    (DEBUGLEVEL & (FEATURE))
+
+#  define DEBUG_TRACE_DYNAMIC                  0x0001
+
+#  if DEBUG_ENABLED( DEBUG_TRACE_DYNAMIC )
+   /* When compiled with dynamic tracing enabled, we allow the user
+    * to choose which features are to be traced, by specification of
+    * the "--trace=FEATURES" command line option; however the features
+    * which may be selected are restricted to an explicit subset which
+    * has been specified at compile time.
+    */
+#   include "pkgopts.h"
+
+#   define DEBUG_REQUEST_FLAGS       pkgOptions()->GetValue( OPTION_DEBUGLEVEL )
+#   define DEBUG_REQUEST(OPTION)    (DEBUG_ENABLED( OPTION ) & DEBUG_REQUEST_FLAGS)
+
+#  else  /* ! DEBUG_TRACE_DYNAMIC */
+   /* Without dynamic tracing enabled, only those features selected
+    * at compile time may be traced, and all unconditionally.
+    */
+#   define DEBUG_REQUEST(OPTION)    (DEBUGLEVEL & (OPTION))
+#  endif
+
 #  define DEBUG_TRACE_INIT                     0x0010
 #  define DEBUG_TRACE_TRANSACTIONS             0x0020
 #  define DEBUG_SUPPRESS_INSTALLATION          0x0040
index 6bf1402..02455ec 100644 (file)
@@ -379,9 +379,9 @@ HINTERNET pkgInternetAgent::OpenURL( const char *URL )
            */
           dmh_notify( DMH_ERROR, "%s:cannot open URL\n", URL );
 
-        else DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_INTERNET_REQUESTS,
+        else DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INTERNET_REQUESTS ),
           dmh_printf( "%s\nConnecting ... failed(status=%u); retrying...\n",
-            URL, GetLastError() )
+              URL, GetLastError() )
           );
        }
        else
@@ -444,11 +444,10 @@ HINTERNET pkgInternetAgent::OpenURL( const char *URL )
                /* Other failure modes may not be so readily recoverable;
                 * with little hope of success, retry anyway.
                 */
-               DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_INTERNET_REQUESTS,
+               DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INTERNET_REQUESTS ),
                    dmh_printf( "%s: abnormal request status = %u; retrying...\n",
                        URL, ResourceStatus
-                     )
-                 );
+                 ));
                if( HttpSendRequest( ResourceHandle, NULL, 0, 0, 0 ) )
                  ResourceStatus = QueryStatus( ResourceHandle );
              }
@@ -516,7 +515,7 @@ int pkgInternetStreamingAgent::TransferData( int fd )
      } while( dl_status && (count > 0) );
 
   DEBUG_INVOKE_IF(
-      (DEBUGLEVEL & DEBUG_TRACE_INTERNET_REQUESTS) && (dl_status == 0),
+      DEBUG_REQUEST( DEBUG_TRACE_INTERNET_REQUESTS ) && (dl_status == 0),
       dmh_printf( "\nInternetReadFile:download error:%d\n", GetLastError() )
     );
   return dl_status;
@@ -605,7 +604,7 @@ int pkgInternetStreamingAgent::Get( const char *from_url )
        dl_meter = &download_meter;
        dl_status = TransferData( fd );
       }
-      else DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_INTERNET_REQUESTS,
+      else DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INTERNET_REQUESTS ),
          dmh_printf( "OpenURL:error:%d\n", GetLastError() )
        );
 
@@ -794,7 +793,7 @@ int pkgInternetLzmaStreamingAgent::TransferData( int fd )
      } while( dl_status && (count > 0) );
 
   DEBUG_INVOKE_IF(
-      (DEBUGLEVEL & DEBUG_TRACE_INTERNET_REQUESTS) && (dl_status == 0),
+      DEBUG_REQUEST( DEBUG_TRACE_INTERNET_REQUESTS ) && (dl_status == 0),
       dmh_printf( "\nInternetReadFile:download error:%d\n", GetLastError() )
     );
   return dl_status;
index 85aeb9c..dea7087 100644 (file)
@@ -76,6 +76,7 @@ struct pkgopts
 /* Options controlled by bit-mapped flags within OPTION_FLAGS...
  */
 #define OPTION_VERBOSE         (0x00000003)
+#define OPTION_VERBOSE_MAX     (0x00000003)
 
 #if __cplusplus
 /*
index 6b0e551..c43658a 100644 (file)
@@ -105,7 +105,7 @@ unsigned long pkgActionItem::SetAuthorities( pkgActionItem *current )
         * package which has been identified as "installed"...
         */
        const char *tarname = ref->GetPropVal( tarname_key, value_unknown );
-       DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_INIT,
+       DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INIT ),
            dmh_printf( "%s: selected for %s\n",
              (tarname = ref->GetPropVal( tarname_key, value_unknown )),
              (current->flags & ACTION_INSTALL) ? "upgrade" : "removal"
@@ -118,7 +118,7 @@ unsigned long pkgActionItem::SetAuthorities( pkgActionItem *current )
        {
          /* ...and then, having confirmed its validity...
           */
-         DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_INIT,
+         DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INIT ),
              dmh_printf( "%s: marked for %s\n",
                ref->GetPropVal( tarname_key, value_unknown ),
                (current->flags & ACTION_INSTALL) ? "upgrade" : "removal"
@@ -254,7 +254,7 @@ int pkg_rmdir( const char *sysroot, const char *pathname )
     char fullpath[ mkpath( NULL, sysroot, pathname, NULL ) ];
     mkpath( fullpath, sysroot, pathname, NULL );
 
-    DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_TRANSACTIONS,
+    DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_TRANSACTIONS ),
        dmh_printf( "  %s: rmdir\n", fullpath )
       );
 
@@ -286,7 +286,7 @@ int pkg_unlink( const char *sysroot, const char *pathname )
     char filepath[ mkpath( NULL, sysroot, pathname, NULL ) ];
     mkpath( filepath, sysroot, pathname, NULL );
 
-    DEBUG_INVOKE_IF( DEBUGLEVEL & DEBUG_TRACE_TRANSACTIONS,
+    DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_TRANSACTIONS ),
        dmh_printf( "  %s: unlink file\n", filepath )
       );
 
index 4c0cfdc..0158fad 100644 (file)
@@ -4,7 +4,7 @@
  * $Id$
  *
  * Written by Keith Marshall <keithmarshall@users.sourceforge.net>
- * Copyright (C) 2010, MinGW Project
+ * Copyright (C) 2010, 2011, MinGW Project
  *
  *
  * Implementation of the system map loader, sysroot management and
@@ -167,9 +167,9 @@ void pkgXmlDocument::LoadSystemMap()
        /* This system map is a candidate for loading;
         * process all of its subsystem specific sysroot declarations...
         */
-#      if DEBUGLEVEL & DEBUG_TRACE_INIT
-         fprintf( stderr, "Load system map: id = %s\n", id );
-#      endif
+       DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INIT ),
+           dmh_printf( "Load system map: id = %s\n", id )
+         );
        pkgXmlNode *subsystem = sysmap->FindFirstAssociate( sysroot_key );
        while( subsystem != NULL )
        {
@@ -188,11 +188,10 @@ void pkgXmlDocument::LoadSystemMap()
            && ! samepath( path, sysroot->GetPropVal( pathname_key, NULL )) )
              sysroot = sysroot->FindNextAssociate( sysroot_key );
 
-#          if DEBUGLEVEL & DEBUG_TRACE_INIT
-             fprintf( stderr, "Bind subsystem %s: sysroot = %s\n",
+           DEBUG_INVOKE_IF( DEBUG_REQUEST( DEBUG_TRACE_INIT ),
+               dmh_printf( "Bind subsystem %s: sysroot = %s\n",
                  subsystem->GetPropVal( subsystem_key, "<unknown>" ), path
-               );
-#          endif
+             ));
 
            if( sysroot == NULL )
            {