Repository: trafficserver
Updated Branches:
  refs/heads/master d94a47ba8 -> a08f5da2b


TS-2582: add traffic_cop debugging mode

Replace custom traffic_cop option parsing with the standard libts
API. This has the side-effect of now requireing a double-dash for
long options.

Instead of a build-time option to enable traffic_cop debugging, add
a command line option that simply logs debug messages to standard
output. Add another command line option to enable the debug log
level.


Project: http://git-wip-us.apache.org/repos/asf/trafficserver/repo
Commit: http://git-wip-us.apache.org/repos/asf/trafficserver/commit/a08f5da2
Tree: http://git-wip-us.apache.org/repos/asf/trafficserver/tree/a08f5da2
Diff: http://git-wip-us.apache.org/repos/asf/trafficserver/diff/a08f5da2

Branch: refs/heads/master
Commit: a08f5da2b764966446a5a7e26a15b5971b6d55e6
Parents: d94a47b
Author: James Peach <[email protected]>
Authored: Fri Feb 14 14:04:03 2014 -0800
Committer: James Peach <[email protected]>
Committed: Fri Feb 21 21:02:29 2014 -0800

----------------------------------------------------------------------
 CHANGES                                   |   2 +
 cmd/traffic_cop/traffic_cop.cc            | 141 ++++++++++++-------------
 configure.ac                              |  15 ---
 doc/reference/commands/traffic_cop.en.rst |  12 ++-
 lib/ts/ink_config.h.in                    |   1 -
 5 files changed, 79 insertions(+), 92 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a08f5da2/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index e33e4b9..16463e3 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,8 @@
                                                          -*- coding: utf-8 -*-
 Changes with Apache Traffic Server 5.0.0
 
+  *) [TS-2582] Make traffic_cop debugging eadier by logging to stdout.
+
   *) [TS-2579] Remove ipv4 limit for FetchSM and TSFetchUrl/TSFetchPages.
 
   *) [TS-1893] Add more options to server session control.

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a08f5da2/cmd/traffic_cop/traffic_cop.cc
----------------------------------------------------------------------
diff --git a/cmd/traffic_cop/traffic_cop.cc b/cmd/traffic_cop/traffic_cop.cc
index 088be6a..8d34b13 100644
--- a/cmd/traffic_cop/traffic_cop.cc
+++ b/cmd/traffic_cop/traffic_cop.cc
@@ -48,7 +48,6 @@ union semun
 #include <grp.h>
 
 static const long MAX_LOGIN =  sysconf(_SC_LOGIN_NAME_MAX) <= 0 ? 
_POSIX_LOGIN_NAME_MAX :  sysconf(_SC_LOGIN_NAME_MAX);
-static const char COP_TRACE_FILE[] = "/tmp/traffic_cop.trace";
 
 #define OPTIONS_MAX     32
 #define OPTIONS_LEN_MAX 1024
@@ -60,6 +59,7 @@ static const char COP_TRACE_FILE[] = "/tmp/traffic_cop.trace";
 #define COP_FATAL    LOG_ALERT
 #define COP_WARNING  LOG_ERR
 #define COP_DEBUG    LOG_DEBUG
+#define COP_NOTICE   LOG_NOTICE
 
 static const char *runtime_dir;
 static char config_file[PATH_NAME_MAX];
@@ -77,6 +77,11 @@ static char syslog_fac_str[PATH_NAME_MAX] = "LOG_DAEMON";
 static int killsig = SIGKILL;
 static int coresig = 0;
 
+static int debug_flag = false;
+static int stdout_flag = false;
+static int version_flag = false;
+static int stop_flag = false;
+
 static char* admin_user;
 static uid_t admin_uid;
 static gid_t admin_gid;
@@ -132,7 +137,6 @@ AppVersionInfo appVersionInfo;
 static char const localhost[] = "127.0.0.1";
 
 static void cop_log(int priority, const char *format, ...) TS_PRINTFLIKE(2, 3);
-inline static void dummy_cop_log_trace(const char *format, ...) 
TS_PRINTFLIKE(1, 2);
 
 static void get_admin_user(void);
 
@@ -150,67 +154,42 @@ struct ConfigValue
 typedef std::map<std::string, ConfigValue> ConfigValueTable;
 static ConfigValueTable configTable;
 
-inline static void
-dummy_cop_log_trace(const char *format, ...)
+#define cop_log_trace(...) do { if (debug_flag) cop_log(COP_DEBUG, 
__VA_ARGS__); } while (0)
+
+static const char *
+priority_name(int priority)
 {
-  (void)format;
+  switch (priority) {
+    case COP_DEBUG:   return "DEBUG";
+    case COP_WARNING: return "WARNING";
+    case COP_FATAL:   return "FATAL";
+    case COP_NOTICE:  return "NOTICE";
+    default:          return "unknown";
+  }
 }
 
-#if TS_USE_COP_DEBUG
-#define cop_log_trace(...)              cop_log(COP_DEBUG, __VA_ARGS__)
-#else
-#define cop_log_trace(...)      if (0) dummy_cop_log_trace(__VA_ARGS__)
-#endif
-
-
 static void
 cop_log(int priority, const char *format, ...)
 {
   va_list args;
-  char buffer[8192];
-#if TS_USE_COP_DEBUG
-  static FILE *trace_file = NULL;
-  struct timeval now;
-  double now_f;
-#endif
 
   va_start(args, format);
 
-#if TS_USE_COP_DEBUG
-  if (!trace_file) {
-    if (!unlink(COP_TRACE_FILE))
-      trace_file = fopen(COP_TRACE_FILE, "w");
-  }
-  if (trace_file) {
+  if (stdout_flag) {
+    struct timeval now;
+    double now_f;
+
     gettimeofday(&now, NULL);
     now_f = now.tv_sec + now.tv_usec / 1000000.0f;
-    switch (priority) {
-    case COP_DEBUG:
-      fprintf(trace_file, "<%.4f> [DEBUG]: ", now_f);
-      break;
-    case COP_WARNING:
-      fprintf(trace_file, "<%.4f> [WARNING]: ", now_f);
-      break;
-    case COP_FATAL:
-      fprintf(trace_file, "<%.4f> [FATAL]: ", now_f);
-      break;
-    default:
-      fprintf(trace_file, "<%.4f> [unknown]: ", now_f);
-      break;
-    }
-
-    va_list args_tmp;
-    va_copy(args_tmp, args);
-
-    vfprintf(trace_file, format, args_tmp);
-    fflush(trace_file);
 
-    va_end(args_tmp);
+    fprintf(stdout, "<%.4f> [%s]: ", now_f, priority_name(priority));
+    vfprintf(stdout, format, args);
+    fflush(stdout);
+  } else {
+    char buffer[8192];
+    vsprintf(buffer, format, args);
+    syslog(priority, "%s", buffer);
   }
-#endif
-
-  vsprintf(buffer, format, args);
-  syslog(priority, "%s", buffer);
 
   va_end(args);
 }
@@ -620,8 +599,11 @@ config_reload_records()
   config_read_int("proxy.local.cluster.type", &tmp_int);
   cluster_type = static_cast<MgmtClusterType>(tmp_int);
 
-  config_read_string("proxy.config.syslog_facility", syslog_fac_str, 
sizeof(syslog_fac_str), true);
-  process_syslog_config();
+  if (stdout_flag) {
+    config_read_string("proxy.config.syslog_facility", syslog_fac_str, 
sizeof(syslog_fac_str), true);
+    process_syslog_config();
+  }
+
   config_read_int("proxy.config.cop.core_signal", &coresig, true);
 
   config_read_int("proxy.config.cop.linux_min_swapfree_kb", 
&check_memory_min_swapfree_kb, true);
@@ -1633,7 +1615,7 @@ check_lockfile()
     exit(1);
   }
 
-  cop_log(LOG_NOTICE, "--- Cop Starting [Version: %s] ---\n", 
appVersionInfo.FullVersionInfoStr);
+  cop_log(COP_NOTICE, "--- Cop Starting [Version: %s] ---\n", 
appVersionInfo.FullVersionInfoStr);
   cop_log_trace("Leaving check_lockfile()\n");
 }
 
@@ -1773,10 +1755,15 @@ init()
   cop_log_trace("Leaving init()\n");
 }
 
-int version_flag = 0;
+static const ArgumentDescription argument_descriptions[] = {
+  { "debug", 'd', "Enable debug logging", "F", &debug_flag, NULL, NULL },
+  { "stdout", 'o', "Print log messages to standard output", "F", &stdout_flag, 
NULL, NULL },
+  { "stop", 's', "Send child processes SIGSTOP instead of SIGKILL", "F", 
&stop_flag, NULL, NULL },
+  { "version", 'V', "Print Version String", "T", &version_flag, NULL, NULL},
+};
 
 int
-main(int argc, char *argv[])
+main(int /* argc */, char *argv[])
 {
   int fd;
   appVersionInfo.setup(PACKAGE_NAME,"traffic_cop", PACKAGE_VERSION, __DATE__, 
__TIME__, BUILD_MACHINE, BUILD_PERSON, "");
@@ -1784,16 +1771,19 @@ main(int argc, char *argv[])
   // Before accessing file system initialize Layout engine
   Layout::create();
 
-  for (int i = 1; i < argc; i++) {
-    if (strcmp(argv[i], "-stop") == 0) {
-      ink_fputln(stdout, "Cool! I think I'll be a STOP cop!");
-      killsig = SIGSTOP;
-    } else if (strcmp(argv[i], "-V") == 0) {
-      ink_fputln(stderr, appVersionInfo.FullVersionInfoStr);
-      exit(0);
-    }
+  process_args(argument_descriptions, countof(argument_descriptions), argv);
+
+  // Check for version number request
+  if (version_flag) {
+    fprintf(stderr, "%s\n", appVersionInfo.FullVersionInfoStr);
+    exit(0);
   }
-  // Detach STDIN, STDOUT, and STDERR (basically, "nohup"). /leif
+
+  if (stop_flag) {
+    cop_log_trace("Cool! I think I'll be a STOP cop!");
+    killsig = SIGSTOP;
+  }
+
   signal(SIGHUP, SIG_IGN);
   signal(SIGTSTP, SIG_IGN);
   signal(SIGTTOU, SIG_IGN);
@@ -1823,17 +1813,20 @@ main(int argc, char *argv[])
   setpgrp();
 #endif
 
-  close(STDIN_FILENO);
-  close(STDOUT_FILENO);
-  close(STDERR_FILENO);
-  if ((fd = open("/dev/null", O_WRONLY, 0)) >= 0) {
-    fcntl(fd, F_DUPFD, STDIN_FILENO);
-    fcntl(fd, F_DUPFD, STDOUT_FILENO);
-    fcntl(fd, F_DUPFD, STDERR_FILENO);
-    close(fd);
-  } else {
-    ink_fputln(stderr, "Unable to open /dev/null");
-    return 0;
+  // Detach STDIN, STDOUT, and STDERR (basically, "nohup"). /leif
+  if (!stdout_flag) {
+    close(STDIN_FILENO);
+    close(STDOUT_FILENO);
+    close(STDERR_FILENO);
+    if ((fd = open("/dev/null", O_WRONLY, 0)) >= 0) {
+      fcntl(fd, F_DUPFD, STDIN_FILENO);
+      fcntl(fd, F_DUPFD, STDOUT_FILENO);
+      fcntl(fd, F_DUPFD, STDERR_FILENO);
+      close(fd);
+    } else {
+      ink_fputln(stderr, "Unable to open /dev/null");
+      return 0;
+    }
   }
 
   // Initialize and start it up.

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a08f5da2/configure.ac
----------------------------------------------------------------------
diff --git a/configure.ac b/configure.ac
index db3996f..169fa16 100644
--- a/configure.ac
+++ b/configure.ac
@@ -341,21 +341,6 @@ AC_ARG_ENABLE([ccache],
 AC_MSG_RESULT([$enable_ccache])
 
 #
-# Enble traffic_cop debugging
-#
-AC_MSG_CHECKING([whether to enable traffic_cop debugging])
-AC_ARG_ENABLE([cop-debug],
-  [AS_HELP_STRING([--enable-cop-debug],[Enable Traffic Cop debugging (for 
developers)])],
-  [],
-  [enable_cop_debug="no"]
-)
-AC_MSG_RESULT([$enable_cop_debug])
-TS_ARG_ENABLE_VAR([use], [cop-debug])
-AC_SUBST(use_cop_debug)
-
-
-
-#
 # Use TPROXY for connection transparency.
 #
 AC_MSG_CHECKING([whether to enable TPROXY based transparency])

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a08f5da2/doc/reference/commands/traffic_cop.en.rst
----------------------------------------------------------------------
diff --git a/doc/reference/commands/traffic_cop.en.rst 
b/doc/reference/commands/traffic_cop.en.rst
index a19c2a3..a28d442 100644
--- a/doc/reference/commands/traffic_cop.en.rst
+++ b/doc/reference/commands/traffic_cop.en.rst
@@ -38,12 +38,20 @@ and :ts:cv:`proxy.config.cop.linux_min_memfree_kb` 
variables.
 
 The following options are available:
 
-.. option:: -stop
+.. option:: -d, --debug
+
+   Emit debugging messages.
+
+.. option:: -o, --stdout
+
+    Print messages to standard output instead of the system log.
+
+.. option:: -s, --stop
 
    Kill children using ``SIGSTOP`` instead of ``SIGKILL``. This
    option is primarily for debugging.
 
-.. option:: -V
+.. option:: -V, --version
 
    Print version information and exit.
 

http://git-wip-us.apache.org/repos/asf/trafficserver/blob/a08f5da2/lib/ts/ink_config.h.in
----------------------------------------------------------------------
diff --git a/lib/ts/ink_config.h.in b/lib/ts/ink_config.h.in
index 455ec93..44e4df3 100644
--- a/lib/ts/ink_config.h.in
+++ b/lib/ts/ink_config.h.in
@@ -70,7 +70,6 @@
 #define TS_USE_TLS_SNI                 @use_tls_sni@
 #define TS_USE_TLS_ECKEY               @use_tls_eckey@
 #define TS_USE_LINUX_NATIVE_AIO        @use_linux_native_aio@
-#define TS_USE_COP_DEBUG               @use_cop_debug@
 #define TS_USE_INTERIM_CACHE           @has_interim_cache@
 
 

Reply via email to