]> git.cworth.org Git - apitrace/blob - cli/cli_dump.cpp
f6c51291af3597e6d5f2067a1c7a2bb401ed491a
[apitrace] / cli / cli_dump.cpp
1 /**************************************************************************
2  *
3  * Copyright 2011 Jose Fonseca
4  * Copyright 2010 VMware, Inc.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies 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 included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  *
25  **************************************************************************/
26
27
28 #include <string.h>
29
30 #include "cli.hpp"
31 #include "cli_pager.hpp"
32
33 #include "trace_parser.hpp"
34 #include "trace_dump.hpp"
35
36
37 enum ColorOption {
38     COLOR_OPTION_NEVER = 0,
39     COLOR_OPTION_ALWAYS = 1,
40     COLOR_OPTION_AUTO = -1
41 };
42
43 static ColorOption color = COLOR_OPTION_AUTO;
44
45 static bool verbose = false;
46
47 static const char *synopsis = "Dump given trace(s) to standard output.";
48
49 static void
50 usage(void)
51 {
52     std::cout
53         << "usage: apitrace dump [OPTIONS] <trace-file>...\n"
54         << synopsis << "\n"
55         "\n"
56         "    -v, --verbose       verbose output\n"
57         "    --color=<WHEN>\n"
58         "    --colour=<WHEN>     Colored syntax highlighting\n"
59         "                        WHEN is 'auto', 'always', or 'never'\n"
60         "    --no-arg-names      Don't dump argument names\n"
61     ;
62 }
63
64 static int
65 command(int argc, char *argv[])
66 {
67     trace::DumpFlags dumpFlags = 0;
68
69     int i;
70
71     for (i = 0; i < argc; ++i) {
72         const char *arg = argv[i];
73
74         if (arg[0] != '-') {
75             break;
76         }
77
78         if (!strcmp(arg, "--")) {
79             break;
80         } else if (!strcmp(arg, "--help")) {
81             usage();
82             return 0;
83         } else if (strcmp(arg, "-v") == 0 ||
84                    strcmp(arg, "--verbose") == 0) {
85             verbose = true;
86         } else if (!strcmp(arg, "--color=auto") ||
87                    !strcmp(arg, "--colour=auto")) {
88             color = COLOR_OPTION_AUTO;
89         } else if (!strcmp(arg, "--color") ||
90                    !strcmp(arg, "--colour") ||
91                    !strcmp(arg, "--color=always") ||
92                    !strcmp(arg, "--colour=always")) {
93             color = COLOR_OPTION_ALWAYS;
94         } else if (!strcmp(arg, "--color=never") ||
95                    !strcmp(arg, "--colour=never") ||
96                    !strcmp(arg, "--no-color") ||
97                    !strcmp(arg, "--no-colour")) {
98             color = COLOR_OPTION_NEVER;
99         } else if (!strcmp(arg, "--no-arg-names")) {
100             dumpFlags |= trace::DUMP_FLAG_NO_ARG_NAMES;
101         } else {
102             std::cerr << "error: unknown option " << arg << "\n";
103             usage();
104             return 1;
105         }
106     }
107
108     if (color == COLOR_OPTION_AUTO) {
109 #ifdef _WIN32
110         color = COLOR_OPTION_ALWAYS;
111 #else
112         color = isatty(1) ? COLOR_OPTION_ALWAYS : COLOR_OPTION_NEVER;
113         pipepager();
114 #endif
115     }
116
117     if (color == COLOR_OPTION_NEVER) {
118         dumpFlags |= trace::DUMP_FLAG_NO_COLOR;
119     }
120
121     for (; i < argc; ++i) {
122         trace::Parser p;
123
124         if (!p.open(argv[i])) {
125             std::cerr << "error: failed to open " << argv[i] << "\n";
126             return 1;
127         }
128
129         trace::Call *call;
130         while ((call = p.parse_call())) {
131             if (verbose ||
132                 !(call->flags & trace::CALL_FLAG_VERBOSE)) {
133                 trace::dump(*call, std::cout, dumpFlags);
134             }
135             delete call;
136         }
137     }
138
139     return 0;
140 }
141
142 const Command dump_command = {
143     "dump",
144     synopsis,
145     usage,
146     command
147 };