Skip to content

Commit 9916afd

Browse files
committed
replaced <unistd.h> and <regex> includes with "uv.h" for consistency
1 parent ead4d7a commit 9916afd

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

doc/api/cli.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ If `--cpu-prof-name` is specified, the provided value is used as a template
544544
for the file name. Some placeholders are supported and will be substituted
545545
at runtime:
546546

547-
- `${pid}` — the current process ID
547+
* `${pid}` — the current process ID
548548

549549
```console
550550
$ node --cpu-prof --cpu-prof-name 'CPU.${pid}.cpuprofile' index.js
@@ -661,7 +661,7 @@ For example, the following script will not emit
661661

662662
```mjs
663663
import sys from 'node:sys';
664-
```
664+
````
665665

666666
```cjs
667667
const sys = require('node:sys');

src/inspector_profiler.cc

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "inspector_profiler.h"
2+
#include "uv.h"
23
#include "base_object-inl.h"
34
#include "debug_utils-inl.h"
45
#include "diagnosticfilename-inl.h"
@@ -9,8 +10,6 @@
910
#include "node_internals.h"
1011
#include "util-inl.h"
1112
#include "v8-inspector.h"
12-
#include <unistd.h>
13-
#include <regex>
1413

1514
#include <cinttypes>
1615
#include <limits>
@@ -467,11 +466,22 @@ static void EndStartedProfilers(Environment* env) {
467466
}
468467
}
469468

470-
std::string ReplacePlaceholders(const std::string& pattern) {
469+
static std::string ReplacePlaceholders(const std::string& pattern) {
471470
std::string result = pattern;
472-
std::string pid_str = std::to_string(getpid());
473-
result = std::regex_replace(result, std::regex("\\$\\{pid\\}"), pid_str);
474-
// TODO: Add more placeholders as needed.
471+
472+
static const std::unordered_map<std::string, std::function<std::string()>> kPlaceholderMap = {
473+
{ "${pid}", []() { return std::to_string(uv_os_getpid()); } },
474+
// TODO(haramj): Add more placeholders as needed.
475+
};
476+
477+
for (const auto& [placeholder, getter] : kPlaceholderMap) {
478+
size_t pos = 0;
479+
while ((pos = result.find(placeholder, pos)) != std::string::npos) {
480+
result.replace(pos, placeholder.length(), getter());
481+
pos += getter().length();
482+
}
483+
}
484+
475485
return result;
476486
}
477487

@@ -496,7 +506,8 @@ void StartProfilers(Environment* env) {
496506
DiagnosticFilename filename(env, "CPU", "cpuprofile");
497507
env->set_cpu_prof_name(*filename);
498508
} else {
499-
std::string resolved_name = ReplacePlaceholders(env->options()->cpu_prof_name);
509+
std::string resolved_name =
510+
ReplacePlaceholders(env->options()->cpu_prof_name);
500511
env->set_cpu_prof_name(resolved_name);
501512
}
502513
CHECK_NULL(env->cpu_profiler_connection());

0 commit comments

Comments
 (0)