1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_TRACE_H_ 6 #define TOOLS_GN_TRACE_H_ 7 8 #include <memory> 9 #include <string> 10 #include <thread> 11 12 #include "util/ticks.h" 13 14 class Label; 15 16 namespace base { 17 class CommandLine; 18 class FilePath; 19 } // namespace base 20 21 class TraceItem { 22 public: 23 enum Type { 24 TRACE_SETUP, 25 TRACE_FILE_LOAD, 26 TRACE_FILE_PARSE, 27 TRACE_FILE_EXECUTE, 28 TRACE_FILE_WRITE, 29 TRACE_IMPORT_LOAD, 30 TRACE_IMPORT_BLOCK, 31 TRACE_SCRIPT_EXECUTE, 32 TRACE_DEFINE_TARGET, 33 TRACE_ON_RESOLVED, 34 TRACE_CHECK_HEADER, // One file. 35 TRACE_CHECK_HEADERS, // All files. 36 }; 37 38 TraceItem(Type type, const std::string& name, std::thread::id thread_id); 39 ~TraceItem(); 40 type()41 Type type() const { return type_; } name()42 const std::string& name() const { return name_; } thread_id()43 std::thread::id thread_id() const { return thread_id_; } 44 begin()45 Ticks begin() const { return begin_; } set_begin(Ticks b)46 void set_begin(Ticks b) { begin_ = b; } end()47 Ticks end() const { return end_; } set_end(Ticks e)48 void set_end(Ticks e) { end_ = e; } 49 delta()50 TickDelta delta() const { return TicksDelta(end_, begin_); } 51 52 // Optional toolchain label. toolchain()53 const std::string& toolchain() const { return toolchain_; } set_toolchain(const std::string & t)54 void set_toolchain(const std::string& t) { toolchain_ = t; } 55 56 // Optional command line. cmdline()57 const std::string& cmdline() const { return cmdline_; } set_cmdline(const std::string & c)58 void set_cmdline(const std::string& c) { cmdline_ = c; } 59 60 private: 61 Type type_; 62 std::string name_; 63 std::thread::id thread_id_; 64 65 Ticks begin_; 66 Ticks end_; 67 68 std::string toolchain_; 69 std::string cmdline_; 70 }; 71 72 class ScopedTrace { 73 public: 74 ScopedTrace(TraceItem::Type t, const std::string& name); 75 ScopedTrace(TraceItem::Type t, const Label& label); 76 ~ScopedTrace(); 77 78 void SetToolchain(const Label& label); 79 void SetCommandLine(const base::CommandLine& cmdline); 80 81 void Done(); 82 83 private: 84 std::unique_ptr<TraceItem> item_; 85 bool done_; 86 }; 87 88 // Call to turn tracing on. It's off by default. 89 void EnableTracing(); 90 91 // Returns whether tracing is enabled. 92 bool TracingEnabled(); 93 94 // Adds a trace event to the log. 95 void AddTrace(std::unique_ptr<TraceItem> item); 96 97 // Returns a summary of the current traces, or the empty string if tracing is 98 // not enabled. 99 std::string SummarizeTraces(); 100 101 // Saves the current traces to the given filename in JSON format. 102 void SaveTraces(const base::FilePath& file_name); 103 104 #endif // TOOLS_GN_TRACE_H_ 105