diff options
author | Andreas Madsen <amwebdk@gmail.com> | 2018-01-31 19:12:09 +0300 |
---|---|---|
committer | Anatoli Papirovski <apapirovski@mac.com> | 2018-03-04 14:07:39 +0300 |
commit | 85212bb182f555f6d09e0b2f5f78d2d8e19bcef1 (patch) | |
tree | 182b8e0698057e8760462964c0deef66e6ccb34c /src/tracing | |
parent | 523d44a66e5a4f9bbe335b7872919aa39d6ee4c4 (diff) |
trace_events: add file pattern cli option
Allow the user to specify the filepath for the trace_events log file
using a template string.
PR-URL: https://github.com/nodejs/node/pull/18480
Reviewed-By: Ali Ijaz Sheikh <ofrobots@google.com>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Diffstat (limited to 'src/tracing')
-rw-r--r-- | src/tracing/agent.cc | 5 | ||||
-rw-r--r-- | src/tracing/agent.h | 2 | ||||
-rw-r--r-- | src/tracing/node_trace_writer.cc | 26 | ||||
-rw-r--r-- | src/tracing/node_trace_writer.h | 4 |
4 files changed, 28 insertions, 9 deletions
diff --git a/src/tracing/agent.cc b/src/tracing/agent.cc index 4514a0fce1f..71e53e787a4 100644 --- a/src/tracing/agent.cc +++ b/src/tracing/agent.cc @@ -13,11 +13,12 @@ namespace tracing { using v8::platform::tracing::TraceConfig; using std::string; -Agent::Agent() { +Agent::Agent(const std::string& log_file_pattern) { int err = uv_loop_init(&tracing_loop_); CHECK_EQ(err, 0); - NodeTraceWriter* trace_writer = new NodeTraceWriter(&tracing_loop_); + NodeTraceWriter* trace_writer = new NodeTraceWriter( + log_file_pattern, &tracing_loop_); TraceBuffer* trace_buffer = new NodeTraceBuffer( NodeTraceBuffer::kBufferChunks, trace_writer, &tracing_loop_); tracing_controller_ = new TracingController(); diff --git a/src/tracing/agent.h b/src/tracing/agent.h index 203f53be7ea..9d6bc4e90a3 100644 --- a/src/tracing/agent.h +++ b/src/tracing/agent.h @@ -19,7 +19,7 @@ class TracingController : public v8::platform::tracing::TracingController { class Agent { public: - Agent(); + explicit Agent(const std::string& log_file_pattern); void Start(const std::string& enabled_categories); void Stop(); diff --git a/src/tracing/node_trace_writer.cc b/src/tracing/node_trace_writer.cc index 9293e9cb8f7..15c59fc98a8 100644 --- a/src/tracing/node_trace_writer.cc +++ b/src/tracing/node_trace_writer.cc @@ -8,8 +8,9 @@ namespace node { namespace tracing { -NodeTraceWriter::NodeTraceWriter(uv_loop_t* tracing_loop) - : tracing_loop_(tracing_loop) { +NodeTraceWriter::NodeTraceWriter(const std::string& log_file_pattern, + uv_loop_t* tracing_loop) + : tracing_loop_(tracing_loop), log_file_pattern_(log_file_pattern) { flush_signal_.data = this; int err = uv_async_init(tracing_loop_, &flush_signal_, FlushSignalCb); CHECK_EQ(err, 0); @@ -54,12 +55,27 @@ NodeTraceWriter::~NodeTraceWriter() { } } +void replace_substring(std::string* target, + const std::string& search, + const std::string& insert) { + size_t pos = target->find(search); + for (; pos != std::string::npos; pos = target->find(search, pos)) { + target->replace(pos, search.size(), insert); + pos += insert.size(); + } +} + void NodeTraceWriter::OpenNewFileForStreaming() { ++file_num_; uv_fs_t req; - std::ostringstream log_file; - log_file << "node_trace." << file_num_ << ".log"; - fd_ = uv_fs_open(tracing_loop_, &req, log_file.str().c_str(), + + // Evaluate a JS-style template string, it accepts the values ${pid} and + // ${rotation} + std::string filepath(log_file_pattern_); + replace_substring(&filepath, "${pid}", std::to_string(uv_os_getpid())); + replace_substring(&filepath, "${rotation}", std::to_string(file_num_)); + + fd_ = uv_fs_open(tracing_loop_, &req, filepath.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644, nullptr); CHECK_NE(fd_, -1); uv_fs_req_cleanup(&req); diff --git a/src/tracing/node_trace_writer.h b/src/tracing/node_trace_writer.h index 5abb3e2a617..9211790777b 100644 --- a/src/tracing/node_trace_writer.h +++ b/src/tracing/node_trace_writer.h @@ -16,7 +16,8 @@ using v8::platform::tracing::TraceWriter; class NodeTraceWriter : public TraceWriter { public: - explicit NodeTraceWriter(uv_loop_t* tracing_loop); + explicit NodeTraceWriter(const std::string& log_file_pattern, + uv_loop_t* tracing_loop); ~NodeTraceWriter(); void AppendTraceEvent(TraceObject* trace_event) override; @@ -62,6 +63,7 @@ class NodeTraceWriter : public TraceWriter { int highest_request_id_completed_ = 0; int total_traces_ = 0; int file_num_ = 0; + const std::string& log_file_pattern_; std::ostringstream stream_; TraceWriter* json_trace_writer_ = nullptr; bool exited_ = false; |