@@ -44,7 +44,7 @@ using v8::platform::tracing::TraceWriter;
4444using std::string;
4545
4646Agent::Agent (const std::string& log_file_pattern)
47- : log_file_pattern_(log_file_pattern), file_writer_(EmptyClientHandle()) {
47+ : log_file_pattern_(log_file_pattern) {
4848 tracing_controller_ = new TracingController ();
4949 tracing_controller_->Initialize (nullptr );
5050}
@@ -62,20 +62,23 @@ void Agent::Start() {
6262 // This thread should be created *after* async handles are created
6363 // (within NodeTraceWriter and NodeTraceBuffer constructors).
6464 // Otherwise the thread could shut down prematurely.
65- CHECK_EQ (0 , uv_thread_create (&thread_, ThreadCb, this ));
65+ CHECK_EQ (0 , uv_thread_create (&thread_, [](void * arg) {
66+ Agent* agent = static_cast <Agent*>(arg);
67+ uv_run (&agent->tracing_loop_ , UV_RUN_DEFAULT);
68+ }, this ));
6669 started_ = true ;
6770}
6871
69- Agent::ClientHandle Agent::AddClient (const std::set<std::string>& categories,
70- std::unique_ptr<AsyncTraceWriter> writer) {
72+ AgentWriterHandle Agent::AddClient (
73+ const std::set<std::string>& categories,
74+ std::unique_ptr<AsyncTraceWriter> writer) {
7175 Start ();
7276 ScopedSuspendTracing suspend (tracing_controller_, this );
7377 int id = next_writer_id_++;
7478 writers_[id] = std::move (writer);
7579 categories_[id] = categories;
7680
77- auto client_id = new std::pair<Agent*, int >(this , id);
78- return ClientHandle (client_id, &DisconnectClient);
81+ return AgentWriterHandle (this , id);
7982}
8083
8184void Agent::Stop () {
@@ -101,61 +104,52 @@ void Agent::Disconnect(int client) {
101104 categories_.erase (client);
102105}
103106
104- // static
105- void Agent::ThreadCb (void * arg) {
106- Agent* agent = static_cast <Agent*>(arg);
107- uv_run (&agent->tracing_loop_ , UV_RUN_DEFAULT);
108- }
109-
110107void Agent::Enable (const std::string& categories) {
111108 if (categories.empty ())
112109 return ;
113110 std::set<std::string> categories_set;
114- std::stringstream category_list (categories);
111+ std::istringstream category_list (categories);
115112 while (category_list.good ()) {
116113 std::string category;
117114 getline (category_list, category, ' ,' );
118- categories_set.insert ( category);
115+ categories_set.emplace ( std::move ( category) );
119116 }
120117 Enable (categories_set);
121118}
122119
123120void Agent::Enable (const std::set<std::string>& categories) {
124- std::string cats;
125- for (const std::string cat : categories)
126- cats += cat + " , " ;
127121 if (categories.empty ())
128122 return ;
129123
130124 file_writer_categories_.insert (categories.begin (), categories.end ());
131125 std::set<std::string> full_list (file_writer_categories_.begin (),
132126 file_writer_categories_.end ());
133- if (! file_writer_) {
127+ if (file_writer_. empty () ) {
134128 // Ensure background thread is running
135129 Start ();
136130 std::unique_ptr<NodeTraceWriter> writer (
137131 new NodeTraceWriter (log_file_pattern_, &tracing_loop_));
138132 file_writer_ = AddClient (full_list, std::move (writer));
139133 } else {
140134 ScopedSuspendTracing suspend (tracing_controller_, this );
141- categories_[file_writer_-> second ] = full_list;
135+ categories_[file_writer_. id_ ] = full_list;
142136 }
143137}
144138
145139void Agent::Disable (const std::set<std::string>& categories) {
146- for (auto category : categories) {
140+ for (const std::string& category : categories) {
147141 auto it = file_writer_categories_.find (category);
148142 if (it != file_writer_categories_.end ())
149143 file_writer_categories_.erase (it);
150144 }
151- if (! file_writer_)
145+ if (file_writer_. empty () )
152146 return ;
153147 ScopedSuspendTracing suspend (tracing_controller_, this );
154- categories_[file_writer_-> second ] = { file_writer_categories_.begin (),
155- file_writer_categories_.end () };
148+ categories_[file_writer_. id_ ] = { file_writer_categories_.begin (),
149+ file_writer_categories_.end () };
156150}
157151
158- TraceConfig* Agent::CreateTraceConfig () {
152+ TraceConfig* Agent::CreateTraceConfig () const {
159153 if (categories_.empty ())
160154 return nullptr ;
161155 TraceConfig* trace_config = new TraceConfig ();
@@ -165,9 +159,9 @@ TraceConfig* Agent::CreateTraceConfig() {
165159 return trace_config;
166160}
167161
168- std::string Agent::GetEnabledCategories () {
162+ std::string Agent::GetEnabledCategories () const {
169163 std::string categories;
170- for (const auto & category : flatten (categories_)) {
164+ for (const std::string & category : flatten (categories_)) {
171165 if (!categories.empty ())
172166 categories += ' ,' ;
173167 categories += category;
0 commit comments