From: Carl Worth Date: Sat, 22 Oct 2011 03:40:56 +0000 (-0700) Subject: apitrace: Replace tracedump program with new "apitrace dump" command X-Git-Url: https://git.notmuchmail.org/git?a=commitdiff_plain;h=bd5e1649f9c658ddfa3090ec70fb3f741a6ded76;p=apitrace apitrace: Replace tracedump program with new "apitrace dump" command The code just copies right over so should work exactly as before, but with our nice, new, unified command-line syntax. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 82cad7e..0b4ab84 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 8772179..c25b4ce 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -1,3 +1,5 @@ -add_executable (apitrace apitrace.cpp) +add_executable (apitrace + apitrace.cpp + apitrace_dump.cpp) install (TARGETS apitrace RUNTIME DESTINATION bin) diff --git a/cli/apitrace.cpp b/cli/apitrace.cpp index b36064a..107952c 100644 --- a/cli/apitrace.cpp +++ b/cli/apitrace.cpp @@ -31,39 +31,48 @@ * functionality. */ -#include -#include - -#include -#include -#include -#include - -#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 << " []" + "\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, - "[]", - "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 index 0000000..7394bb9 --- /dev/null +++ b/cli/apitrace_cli.hpp @@ -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 +#include + +#include +#include +#include +#include + +#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_ */ diff --git a/cli/apitrace_dump.cpp b/cli/apitrace_dump.cpp new file mode 100644 index 0000000..41900cf --- /dev/null +++ b/cli/apitrace_dump.cpp @@ -0,0 +1,109 @@ +/************************************************************************** + * + * Copyright 2010 VMware, Inc. + * 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. + * + **************************************************************************/ + +#include "apitrace_cli.hpp" + +#include "trace_parser.hpp" + +enum ColorOption { + COLOR_OPTION_NEVER = 0, + COLOR_OPTION_ALWAYS = 1, + COLOR_OPTION_AUTO = -1 +}; + +static ColorOption color = COLOR_OPTION_AUTO; + +void +apitrace_dump_usage(const char *argv0) +{ + std::cout << argv0 << " [OPTIONS] ..." + "\n\n\t" + APITRACE_DUMP_SYNOPSIS + "\n\n\t" + "Supports the following options:\n\t" + "\t--color=\n\t" + "\t--colour= Colored syntax highlighting\n\t" + "\t WHEN is 'auto', 'always', or 'never'\n"; +} + +int +apitrace_dump_command(int argc, char *argv[], int first_arg_command) +{ + int i; + + for (i = first_arg_command; i < argc; ++i) { + const char *arg = argv[i]; + + if (arg[0] != '-') { + break; + } + + if (!strcmp(arg, "--")) { + break; + } else if (!strcmp(arg, "--color=auto") || + !strcmp(arg, "--colour=auto")) { + color = COLOR_OPTION_AUTO; + } else if (!strcmp(arg, "--color") || + !strcmp(arg, "--colour") || + !strcmp(arg, "--color=always") || + !strcmp(arg, "--colour=always")) { + color = COLOR_OPTION_ALWAYS; + } else if (!strcmp(arg, "--color=never") || + !strcmp(arg, "--colour=never") || + !strcmp(arg, "--no-color") || + !strcmp(arg, "--no-colour")) { + color = COLOR_OPTION_NEVER; + } else { + std::cerr << "error: unknown option " << arg << "\n"; + usage(); + return 1; + } + } + + if (color == COLOR_OPTION_AUTO) { +#ifdef _WIN32 + color = COLOR_OPTION_ALWAYS; +#else + color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER; +#endif + } + + for (; i < argc; ++i) { + trace::Parser p; + + if (!p.open(argv[i])) { + std::cerr << "error: failed to open " << argv[i] << "\n"; + return 1; + } + + trace::Call *call; + while ((call = p.parse_call())) { + call->dump(std::cout, color); + delete call; + } + } + + return 0; +} diff --git a/tracedump.cpp b/tracedump.cpp deleted file mode 100644 index 44ca609..0000000 --- a/tracedump.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/************************************************************************** - * - * Copyright 2010 VMware, Inc. - * 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. - * - **************************************************************************/ - - -/* - * Simple utility to dump a trace to standard output. - */ - - -#include - -#include "trace_parser.hpp" - - -enum ColorOption { - COLOR_OPTION_NEVER = 0, - COLOR_OPTION_ALWAYS = 1, - COLOR_OPTION_AUTO = -1 -}; - -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" - ; -} - - -int main(int argc, char **argv) -{ - int i; - - for (i = 1; i < argc; ++i) { - const char *arg = argv[i]; - - if (arg[0] != '-') { - break; - } - - 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; - } else if (!strcmp(arg, "--color") || - !strcmp(arg, "--colour") || - !strcmp(arg, "--color=always") || - !strcmp(arg, "--colour=always")) { - color = COLOR_OPTION_ALWAYS; - } else if (!strcmp(arg, "--color=never") || - !strcmp(arg, "--colour=never") || - !strcmp(arg, "--no-color") || - !strcmp(arg, "--no-colour")) { - color = COLOR_OPTION_NEVER; - } else { - std::cerr << "error: unknown option " << arg << "\n"; - usage(); - return 1; - } - } - - if (color == COLOR_OPTION_AUTO) { -#ifdef _WIN32 - color = COLOR_OPTION_ALWAYS; -#else - color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER; -#endif - } - - for (; i < argc; ++i) { - trace::Parser p; - - if (!p.open(argv[i])) { - std::cerr << "error: failed to open " << argv[i] << "\n"; - return 1; - } - - trace::Call *call; - while ((call = p.parse_call())) { - call->dump(std::cout, color); - delete call; - } - } - - return 0; -}