]> git.cworth.org Git - apitrace/blob - cli/cli_trace.cpp
Use getopt on apitrace trace.
[apitrace] / cli / cli_trace.cpp
1 /*********************************************************************
2  *
3  * Copyright 2011 Intel Corporation
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  *********************************************************************/
27
28
29 #include <assert.h>
30 #include <string.h>
31 #include <getopt.h>
32
33 #include <iostream>
34
35 #include "cli.hpp"
36
37 #include "trace_tools.hpp"
38
39
40 static const char *synopsis = "Generate a new trace by executing the given program.";
41
42 static void
43 usage(void)
44 {
45     std::cout << "usage: apitrace trace [OPTIONS] PROGRAM [ARGS ...]\n"
46         << synopsis << "\n"
47         "\n"
48         "    The given program will be executed with the given arguments.\n"
49         "    During execution, all OpenGL calls will be captured to a trace\n"
50         "    file. That trace file can then be used\n"
51         "    with other apitrace utilities for replay or analysis.\n"
52         "\n"
53         "    -v, --verbose       verbose output\n"
54         "    -a, --api=API       specify API to trace (gl or egl);\n"
55         "                        default is `gl`\n"
56         "    -o, --output=TRACE  specify output trace file;\n"
57         "                        default is `PROGRAM.trace`\n";
58 }
59
60 const static char *
61 shortOptions = "+hva:o:";
62
63 const static struct option
64 longOptions[] = {
65     {"help", no_argument, 0, 'h'},
66     {"verbose", no_argument, 0, 'v'},
67     {"api", required_argument, 0, 'a'},
68     {"output", required_argument, 0, 'o'},
69     {0, 0, 0, 0}
70 };
71
72 static int
73 command(int argc, char *argv[])
74 {
75     bool verbose = false;
76     trace::API api = trace::API_GL;
77     const char *output = NULL;
78
79     int opt;
80     while ((opt = getopt_long(argc, argv, shortOptions, longOptions, NULL)) != -1) {
81         switch (opt) {
82         case 'h':
83             usage();
84             return 0;
85         case 'v':
86             verbose = true;
87             break;
88         case 'a':
89             if (strcmp(optarg, "gl") == 0) {
90                 api = trace::API_GL;
91             } else if (strcmp(optarg, "egl") == 0) {
92                 api = trace::API_EGL;
93             } else {
94                 std::cerr << "error: unknown API `" << optarg << "`\n";
95                 usage();
96                 return 1;
97             }
98             break;
99         case 'o':
100             output = optarg;
101             break;
102         default:
103             std::cerr << "error: unexpected option `" << opt << "`\n";
104             usage();
105             return 1;
106         }
107     }
108
109     if (optind == argc) {
110         std::cerr << "error: no command specified\n";
111         usage();
112         return 1;
113     }
114
115     assert(argv[argc] == 0);
116     return trace::traceProgram(api, argv + optind, output, verbose);
117 }
118
119 const Command trace_command = {
120     "trace",
121     synopsis,
122     usage,
123     command
124 };