Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tracecallextractor.cc
Go to the documentation of this file.
1 
19 
20 #include <algorithm>
21 
22 #include "ion/base/invalid.h"
23 #include "ion/base/logging.h"
24 #include "ion/base/stringutils.h"
25 
26 namespace ion {
27 namespace gfx {
28 
30  SetTrace("");
31 }
32 
33 TraceCallExtractor::TraceCallExtractor(const std::string& trace) {
34  SetTrace(trace);
35 }
36 
37 void TraceCallExtractor::SetTrace(const std::string& trace) {
38  trace_ = trace;
39  CreateCallVector();
40 }
41 
43  return calls_.size();
44 }
45 
46 size_t TraceCallExtractor::GetCountOf(const std::string& call_prefix) const {
47  size_t count = 0;
48  for (size_t i = 0, n = calls_.size(); i < n; ++i) {
49  if (base::StartsWith(calls_[i], call_prefix))
50  count++;
51  }
52  return count;
53 }
54 
56  size_t n, const std::string& call_prefix) const {
57  for (size_t i = 0, num_calls = calls_.size(); i < num_calls; ++i) {
58  if (base::StartsWith(calls_[i], call_prefix)) {
59  if (n == 0)
60  return i;
61  else
62  --n;
63  }
64  }
65  return base::kInvalidIndex;
66 }
67 
68 void TraceCallExtractor::CreateCallVector() {
69  const std::vector<std::string> raw_calls =
70  base::SplitString(trace_, "\n");
71  const size_t count = raw_calls.size();
72  calls_.clear();
73  calls_.reserve(count);
74  for (size_t i = 0; i < count; ++i) {
75  if (!base::StartsWith(raw_calls[i], ">") &&
76  !base::StartsWith(raw_calls[i], "-")) {
78  std::string call;
79  const std::vector<std::string> args =
80  base::SplitString(base::TrimStartWhitespace(raw_calls[i]), "(),");
81  const size_t arg_count = args.size();
82  for (size_t j = 0; j < arg_count; ++j) {
83  const size_t pos = args[j].find(" = ");
84  std::string arg = args[j];
85  if (pos != std::string::npos)
86  arg = arg.substr(pos + 3, std::string::npos);
87  call += arg;
88  if (j == 0)
89  call += "(";
90  else if (j < arg_count - 1)
91  call += ", ";
92  }
93  call += ")";
94  calls_.push_back(call);
95  }
96  }
97 }
98 
99 } // namespace gfx
100 } // namespace ion
bool StartsWith(const std::string &target, const std::string &start)
Returns whether target begins with start.
Definition: stringutils.h:76
size_t GetCallCount() const
Returns the number of OpenGL calls in the stream.
void SetTrace(const std::string &trace)
Sets trace string and extracts vector of calls.
const size_t kInvalidIndex
kInvalidIndex is a size_t value that is very unlikely to be a valid index.
Definition: invalid.cc:23
std::vector< std::string > ION_API SplitString(const std::string &str, const std::string &delimiters)
Splits a string into a vector of substrings, given a set of delimiter characters (expressed as a stri...
Definition: stringutils.cc:187
Copyright 2016 Google Inc.
size_t GetCountOf(const std::string &call_prefix) const
Returns the number of times the passed call start occurs in the trace stream.
size_t GetNthIndexOf(size_t n, const std::string &call_prefix) const
Returns the index of the nth call in the trace stream beginning with start, if it exists...
std::string TrimStartWhitespace(const std::string &target)
Removes any whitespace characters at the beginning of the string.
Definition: stringutils.h:137