trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zw...@apache.org
Subject [trafficserver] 01/09: Switch traffic_top to standard argument processing.
Date Fri, 12 Jan 2018 16:03:42 GMT
This is an automated email from the ASF dual-hosted git repository.

zwoop pushed a commit to branch 7.1.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 10c22e0e43fa0e8da66f313cf6c0741196cb7126
Author: James Peach <jpeach@apache.org>
AuthorDate: Fri Jun 9 21:38:43 2017 -0700

    Switch traffic_top to standard argument processing.
    
    (cherry picked from commit e4ee12c005f90231e9ea6038266314f2b372eb7f)
    
     Conflicts:
    	cmd/traffic_top/traffic_top.cc
---
 cmd/traffic_top/stats.h        |  4 +--
 cmd/traffic_top/traffic_top.cc | 79 ++++++++++++++++++++++++------------------
 2 files changed, 48 insertions(+), 35 deletions(-)

diff --git a/cmd/traffic_top/stats.h b/cmd/traffic_top/stats.h
index ffb1c1e..f35c163 100644
--- a/cmd/traffic_top/stats.h
+++ b/cmd/traffic_top/stats.h
@@ -326,8 +326,8 @@ public:
           _now       = now;
           _time_diff = _now - _old_time;
         } else {
-          fprintf(stderr, "Can't fetch url %s", _url.c_str());
-          abort();
+          fprintf(stderr, "Can't fetch url %s\n", _url.c_str());
+          exit(1);
         }
 
         /* always cleanup */
diff --git a/cmd/traffic_top/traffic_top.cc b/cmd/traffic_top/traffic_top.cc
index 32441ae..16ad488 100644
--- a/cmd/traffic_top/traffic_top.cc
+++ b/cmd/traffic_top/traffic_top.cc
@@ -53,6 +53,11 @@
 
 #include "stats.h"
 
+#include "ts/I_Layout.h"
+#include "ts/ink_args.h"
+#include "I_RecProcess.h"
+#include "RecordsConfig.h"
+
 using namespace std;
 #if HAS_CURL
 char curl_error[CURL_ERROR_SIZE];
@@ -262,17 +267,6 @@ help(const string &host, const string &version)
   }
 }
 
-static void
-usage()
-{
-#if HAS_CURL
-  fprintf(stderr, "Usage: traffic_top [-s seconds] [URL|hostname|hostname:port]\n");
-#else
-  fprintf(stderr, "Usage: traffic_top [-s seconds]\n");
-#endif
-  exit(1);
-}
-
 //----------------------------------------------------------------------------
 void
 main_stats_page(Stats &stats)
@@ -390,38 +384,57 @@ main_stats_page(Stats &stats)
 
 //----------------------------------------------------------------------------
 int
-main(int argc, char **argv)
+main(int argc, const char **argv)
 {
+#if HAS_CURL
+  static const char USAGE[] = "Usage: traffic_top [-s seconds] [URL|hostname|hostname:port]";
+#else
+  static const char USAGE[] = "Usage: traffic_top [-s seconds]";
+#endif
+
   int sleep_time = 6000;
   bool absolute  = false;
-  int opt;
-  while ((opt = getopt(argc, argv, "s:")) != -1) {
-    switch (opt) {
-    case 's':
-      sleep_time = atoi(optarg) * 1000;
-      break;
-    default:
-      usage();
+  string url;
+
+  AppVersionInfo version;
+  version.setup(PACKAGE_NAME, "traffic_top", PACKAGE_VERSION, __DATE__, __TIME__, BUILD_MACHINE,
BUILD_PERSON, "");
+
+  const ArgumentDescription argument_descriptions[] = {
+    {"sleep", 's', "Enable debugging output", "I", &sleep_time, nullptr, nullptr},
+    HELP_ARGUMENT_DESCRIPTION(),
+    VERSION_ARGUMENT_DESCRIPTION(),
+  };
+
+  process_args(&version, argument_descriptions, countof(argument_descriptions), argv,
USAGE);
+
+  Layout::create();
+  RecProcessInit(RECM_STAND_ALONE, nullptr /* diags */);
+  LibRecordsConfigInit();
+
+  switch (n_file_arguments) {
+  case 0: {
+    ats_scoped_str rundir(RecConfigReadRuntimeDir());
+
+    TSMgmtError err = TSInit(rundir, static_cast<TSInitOptionT>(TS_MGMT_OPT_NO_EVENTS
| TS_MGMT_OPT_NO_SOCK_TESTS));
+    if (err != TS_ERR_OKAY) {
+      fprintf(stderr, "Error: connecting to local manager: %s\n", TSGetErrorMessage(err));
+      exit(1);
     }
+    break;
   }
 
-  string url = "";
+  case 1:
 #if HAS_CURL
-  if (optind >= argc) {
+    url = file_arguments[0];
 #else
-  if (1) {
+    usage(argument_descriptions, countof(argument_descriptions), USAGE);
 #endif
-    if (TS_ERR_OKAY != TSInit(nullptr, static_cast<TSInitOptionT>(TS_MGMT_OPT_NO_EVENTS
| TS_MGMT_OPT_NO_SOCK_TESTS))) {
-#if HAS_CURL
-      fprintf(stderr, "Error: missing URL on command line or error connecting to the local
manager\n");
-#else
-      fprintf(stderr, "Error: error connecting to the local manager\n");
-#endif
-      usage();
-    }
-  } else {
-    url = argv[optind];
+    break;
+
+  default:
+    usage(argument_descriptions, countof(argument_descriptions), USAGE);
   }
+
   Stats stats(url);
   stats.getStats();
   const string &host = stats.getHost();

-- 
To stop receiving notification emails like this one, please contact
"commits@trafficserver.apache.org" <commits@trafficserver.apache.org>.

Mime
View raw message