apitrace: Replace tracedump program with new "apitrace dump" command
authorCarl Worth <cworth@cworth.org>
Sat, 22 Oct 2011 03:40:56 +0000 (20:40 -0700)
committerJosé Fonseca <jose.r.fonseca@gmail.com>
Sun, 30 Oct 2011 13:27:59 +0000 (13:27 +0000)
The code just copies right over so should work exactly as before, but
with our nice, new, unified command-line syntax.

CMakeLists.txt
cli/CMakeLists.txt
cli/apitrace.cpp
cli/apitrace_cli.hpp [new file with mode: 0644]
cli/apitrace_dump.cpp [moved from tracedump.cpp with 82% similarity]

index 82cad7e..0b4ab84 100755 (executable)
@@ -230,10 +230,6 @@ set_target_properties (common PROPERTIES
 
 link_libraries (common)
 
-add_executable (tracedump tracedump.cpp)
-
-install (TARGETS tracedump RUNTIME DESTINATION bin)
-
 ##############################################################################
 # API tracers
 
index 8772179..c25b4ce 100644 (file)
@@ -1,3 +1,5 @@
-add_executable (apitrace apitrace.cpp)
+add_executable (apitrace
+  apitrace.cpp
+  apitrace_dump.cpp)
 
 install (TARGETS apitrace RUNTIME DESTINATION bin)
index b36064a..107952c 100644 (file)
  * functionality.
  */
 
-#include <iostream>
-#include <iomanip>
-
-#include <string.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#include "config.h"
+#include "apitrace_cli.hpp"
 
 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
 
+typedef void (*command_usage_t) (const char *argv0);
 typedef int (*command_function_t) (int argc, char *argv[], int first_command_arg);
 
 typedef struct {
     const char *name;
+    const char *synopsis;
+    command_usage_t usage;
     command_function_t function;
-    const char *arguments;
-    const char *summary;
-    const char *documentation;
 } Command;
 
+#define APITRACE_HELP_SYNOPSIS "Print detailed help for the given command."
+
+static void
+apitrace_help_usage(const char *argv0)
+{
+    std::cout << argv0 << " [<command>]"
+        "\n\n\t"
+        APITRACE_HELP_SYNOPSIS
+        "\n\n\t"
+        "Except in this case, where this is all the help you will get."
+        "\n\n";
+}
+
 static int
 apitrace_help_command(int argc, char *argv[], int first_command_arg);
 
 static Command commands[] = {
-    { "help", apitrace_help_command,
-      "[<command>]",
-      "Print detailed help for the given command.",
-      "\tExcept in this case, where this is all the help you will get." }
+    { "dump",
+      APITRACE_DUMP_SYNOPSIS,
+      apitrace_dump_usage,
+      apitrace_dump_command },
+    { "help",
+      APITRACE_HELP_SYNOPSIS,
+      apitrace_help_usage,
+      apitrace_help_command }
 };
 
-static void
+void
 usage(void)
 {
     Command *command;
@@ -85,7 +94,7 @@ usage(void)
         command = &commands[i];
 
         std::cout << " " << std::setw(max_width+2) << command->name
-                  << " " << command->summary << "\n";
+                  << " " << command->synopsis << "\n";
     }
 
     std::cout << "\n"
@@ -110,13 +119,7 @@ apitrace_help_command(int argc, char *argv[], int first_arg_command)
 
         if (strcmp(command_name, command->name) == 0) {
             std::cout << "Help for \"apitrace " << command_name << "\":\n\n";
-            std::cout << command->name;
-            if (command->arguments)
-                std::cout << " " << command->arguments
-                          << "\n\n\t" << command->summary;
-            else
-                std::cout << "\t" << command->summary;
-            std::cout << "\n\n" << command->documentation << "\n\n";
+            (command->usage) ("apitrace");
 
             return 0;
         }
diff --git a/cli/apitrace_cli.hpp b/cli/apitrace_cli.hpp
new file mode 100644 (file)
index 0000000..7394bb9
--- /dev/null
@@ -0,0 +1,50 @@
+/*********************************************************************
+ *
+ * Copyright 2011 Intel Corporation
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person
+ * obtaining a copy of this software and associated documentation
+ * files (the "Software"), to deal in the Software without
+ * restriction, including without limitation the rights to use, copy,
+ * modify, merge, publish, distribute, sublicense, and/or sell copies
+ * of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+ * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+ * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ *********************************************************************/
+
+#ifndef _APITRACE_CLI_HPP_
+#define _APITRACE_CLI_HPP_
+
+#include <iostream>
+#include <iomanip>
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#define APITRACE_DUMP_SYNOPSIS "Dump given trace(s) to standard output."
+
+void
+apitrace_dump_usage(const char *argv0);
+
+int
+apitrace_dump_command(int argc, char *argv[], int first_command_arg);
+
+void
+usage(void);
+
+#endif /* _APITRACE_CLI_HPP_ */
similarity index 82%
rename from tracedump.cpp
rename to cli/apitrace_dump.cpp
index 44ca609..41900cf 100644 (file)
  *
  **************************************************************************/
 
-
-/*
- * Simple utility to dump a trace to standard output.
- */
-
-
-#include <string.h>
+#include "apitrace_cli.hpp"
 
 #include "trace_parser.hpp"
 
-
 enum ColorOption {
     COLOR_OPTION_NEVER = 0,
     COLOR_OPTION_ALWAYS = 1,
@@ -42,25 +35,25 @@ enum ColorOption {
 
 static ColorOption color = COLOR_OPTION_AUTO;
 
-
-static void usage(void) {
-    std::cout <<
-        "Usage: tracedump [OPTION] [TRACE]...\n"
-        "Dump TRACE to standard output.\n"
-        "\n"
-        "  --help               display this help and exit\n"
-        "  --color[=WHEN]\n"
-        "  --colour[=WHEN]      colored syntax highlighting;\n"
-        "                       WHEN is 'always', 'never', or 'auto'\n"
-    ;
+void
+apitrace_dump_usage(const char *argv0)
+{
+    std::cout << argv0 << " [OPTIONS] <trace-file>..."
+        "\n\n\t"
+        APITRACE_DUMP_SYNOPSIS
+        "\n\n\t"
+        "Supports the following options:\n\t"
+       "\t--color=<WHEN>\n\t"
+       "\t--colour=<WHEN>     Colored syntax highlighting\n\t"
+       "\t                    WHEN is 'auto', 'always', or 'never'\n";
 }
 
-
-int main(int argc, char **argv)
+int
+apitrace_dump_command(int argc, char *argv[], int first_arg_command)
 {
     int i;
 
-    for (i = 1; i < argc; ++i) {
+    for (i = first_arg_command; i < argc; ++i) {
         const char *arg = argv[i];
 
         if (arg[0] != '-') {
@@ -69,9 +62,6 @@ int main(int argc, char **argv)
 
         if (!strcmp(arg, "--")) {
             break;
-        } else if (!strcmp(arg, "--help")) {
-            usage();
-            return 0;
         } else if (!strcmp(arg, "--color=auto") ||
                    !strcmp(arg, "--colour=auto")) {
             color = COLOR_OPTION_AUTO;