From 6b58746b2e78aae4ea3c68db84bd516184b52708 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Wed, 18 Jul 2018 02:15:17 +0200 Subject: [PATCH] src: use unique_ptr for internal JSON trace writer PR-URL: https://github.com/nodejs/node/pull/21867 Reviewed-By: James M Snell Reviewed-By: Eugene Ostroukhov Reviewed-By: Ali Ijaz Sheikh --- src/tracing/node_trace_writer.cc | 8 +++----- src/tracing/node_trace_writer.h | 2 +- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/tracing/node_trace_writer.cc b/src/tracing/node_trace_writer.cc index 2fb7c12a82dace..c188495422e029 100644 --- a/src/tracing/node_trace_writer.cc +++ b/src/tracing/node_trace_writer.cc @@ -37,9 +37,7 @@ void NodeTraceWriter::WriteSuffix() { { Mutex::ScopedLock scoped_lock(stream_mutex_); if (total_traces_ > 0) { - total_traces_ = 0; // so we don't write it again in FlushPrivate - // Appends "]}" to stream_. - delete json_trace_writer_; + total_traces_ = kTracesPerFile; // Act as if we reached the file limit. should_flush = true; } } @@ -111,7 +109,7 @@ void NodeTraceWriter::AppendTraceEvent(TraceObject* trace_event) { // to a state where we can start writing trace events to it. // Repeatedly constructing and destroying json_trace_writer_ allows // us to use V8's JSON writer instead of implementing our own. - json_trace_writer_ = TraceWriter::CreateJSONTraceWriter(stream_); + json_trace_writer_.reset(TraceWriter::CreateJSONTraceWriter(stream_)); } ++total_traces_; json_trace_writer_->AppendTraceEvent(trace_event); @@ -126,7 +124,7 @@ void NodeTraceWriter::FlushPrivate() { total_traces_ = 0; // Destroying the member JSONTraceWriter object appends "]}" to // stream_ - in other words, ending a JSON file. - delete json_trace_writer_; + json_trace_writer_.reset(); } // str() makes a copy of the contents of the stream. str = stream_.str(); diff --git a/src/tracing/node_trace_writer.h b/src/tracing/node_trace_writer.h index 9c4decc8eec3d4..53311db9922e5e 100644 --- a/src/tracing/node_trace_writer.h +++ b/src/tracing/node_trace_writer.h @@ -63,7 +63,7 @@ class NodeTraceWriter : public AsyncTraceWriter { int file_num_ = 0; const std::string& log_file_pattern_; std::ostringstream stream_; - TraceWriter* json_trace_writer_ = nullptr; + std::unique_ptr json_trace_writer_; bool exited_ = false; };