• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <fstream>
16 #include <string>
17 
18 #include "absl/flags/parse.h"
19 #include "absl/log/initialize.h"
20 #include "absl/strings/string_view.h"
21 #include "google/protobuf/util/json_util.h"
22 #include "tools/render/trace_program.h"
23 
24 enum InputFormat {
25   INPUT_JSON,
26   INPUT_QTR,
27 };
28 
29 namespace {
GuessInputFileFormat(absl::string_view filename)30 InputFormat GuessInputFileFormat(absl::string_view filename) {
31   if (filename.find(".json") != std::string::npos) {
32     return INPUT_JSON;
33   } else {
34     return INPUT_QTR;
35   }
36 }
37 }  // namespace
38 
39 // render_trace renders the specified trace file using an OpenGL-based viewer.
main(int argc,char * argv[])40 int main(int argc, char* argv[]) {
41   std::vector<char*> args = absl::ParseCommandLine(argc, argv);
42   absl::InitializeLog();
43 
44   CHECK_GE(argc, 2) << "Specify file path";
45   auto trace = std::make_unique<quic_trace::Trace>();
46   {
47     std::string filename(args[1]);
48     std::ifstream f(filename);
49     switch (GuessInputFileFormat(filename)) {
50       case INPUT_QTR: {
51         trace->ParseFromIstream(&f);
52         break;
53       }
54       case INPUT_JSON: {
55         std::istreambuf_iterator<char> it(f);
56         std::istreambuf_iterator<char> end;
57 
58         auto status = google::protobuf::util::JsonStringToMessage(
59             std::string(it, end), &*trace);
60         if (!status.ok()) {
61           LOG(FATAL) << "Failed to load '" << filename << "': " << status;
62         }
63         break;
64       }
65       default:
66         LOG(FATAL) << "Unexpected format";
67     }
68   }
69 
70   quic_trace::render::TraceProgram program;
71   program.LoadTrace(std::move(trace));
72   program.Loop();
73   return 0;
74 }
75