[C++] Add a way to retrieve stats
diff --git a/Makefile b/Makefile
index 7e523ec..5b0f4f7 100644
--- a/Makefile
+++ b/Makefile
@@ -28,6 +28,7 @@
 	ninja.cc \
 	parser.cc \
 	rule.cc \
+	stats.cc \
 	string_piece.cc \
 	string_pool.cc \
 	stringprintf.cc \
diff --git a/func.cc b/func.cc
index 306de80..a0ef199 100644
--- a/func.cc
+++ b/func.cc
@@ -30,6 +30,7 @@
 #include "eval.h"
 #include "log.h"
 #include "parser.h"
+#include "stats.h"
 #include "strutil.h"
 #include "symtab.h"
 #include "var.h"
@@ -242,6 +243,7 @@
 }
 
 void WildcardFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
+  COLLECT_STATS("func wildcard time");
   shared_ptr<string> pat = args[0]->Eval(ev);
   if (ev->avoid_io()) {
     *s += "$(/bin/ls -d ";
@@ -411,6 +413,7 @@
 }
 
 void ShellFunc(const vector<Value*>& args, Evaluator* ev, string* s) {
+  COLLECT_STATS("func shell time");
   shared_ptr<string> cmd = args[0]->Eval(ev);
   if (ev->avoid_io()) {
     *s += "$(";
diff --git a/main.cc b/main.cc
index aa09bed..31a0d23 100644
--- a/main.cc
+++ b/main.cc
@@ -31,6 +31,7 @@
 #include "log.h"
 #include "ninja.h"
 #include "parser.h"
+#include "stats.h"
 #include "string_piece.h"
 #include "stringprintf.h"
 #include "strutil.h"
@@ -111,6 +112,8 @@
 }
 
 static void Quit() {
+  ReportAllStats();
+
   QuitParser();
   QuitDepNodePool();
   QuitFuncTable();
diff --git a/stats.cc b/stats.cc
new file mode 100644
index 0000000..2d747a4
--- /dev/null
+++ b/stats.cc
@@ -0,0 +1,66 @@
+// Copyright 2015 Google Inc. All rights reserved
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// +build ignore
+
+#include "stats.h"
+
+#include <vector>
+
+#include "log.h"
+#include "stringprintf.h"
+#include "time.h"
+
+namespace {
+
+vector<Stats*>* g_stats;
+
+}  // namespace
+
+Stats::Stats(const char* name)
+    : name_(name), start_time_(0), elapsed_(0) {
+  if (g_stats == NULL)
+    g_stats = new vector<Stats*>;
+  g_stats->push_back(this);
+}
+
+string Stats::String() const {
+  return StringPrintf("%s: %f", name_, elapsed_);
+}
+
+void Stats::Start() {
+  start_time_ = GetTime();
+}
+
+void Stats::End() {
+  elapsed_ += GetTime() - start_time_;
+}
+
+ScopedStatsRecorder::ScopedStatsRecorder(Stats* st)
+    : st_(st) {
+  st_->Start();
+}
+
+ScopedStatsRecorder::~ScopedStatsRecorder() {
+  st_->End();
+}
+
+void ReportAllStats() {
+  if (!g_stats)
+    return;
+  for (Stats* st : *g_stats) {
+    LOG_STAT("%s", st->String().c_str());
+  }
+  delete g_stats;
+}
diff --git a/stats.h b/stats.h
new file mode 100644
index 0000000..41525e1
--- /dev/null
+++ b/stats.h
@@ -0,0 +1,54 @@
+// Copyright 2015 Google Inc. All rights reserved
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef STATS_H_
+#define STATS_H_
+
+#include <string>
+
+using namespace std;
+
+class Stats {
+ public:
+  explicit Stats(const char* name);
+
+  string String() const;
+
+ private:
+  void Start();
+  void End();
+
+  friend class ScopedStatsRecorder;
+
+  const char* name_;
+  double start_time_;
+  double elapsed_;
+};
+
+class ScopedStatsRecorder {
+ public:
+  explicit ScopedStatsRecorder(Stats* st);
+  ~ScopedStatsRecorder();
+
+ private:
+  Stats* st_;
+};
+
+void ReportAllStats();
+
+#define COLLECT_STATS(name)                     \
+  static Stats stats(name);                     \
+  ScopedStatsRecorder ssr(&stats)
+
+#endif  // STATS_H_