Skip to content

Commit

Permalink
src: nest namespace report in namespace node
Browse files Browse the repository at this point in the history
PR-URL: nodejs/node#44069
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Feng Yu <F3n67u@outlook.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
legendecas authored and guangwong committed Jan 3, 2023
1 parent fa3904e commit 67cfeef
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
43 changes: 17 additions & 26 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,9 @@ constexpr int NODE_REPORT_VERSION = 2;
constexpr int NANOS_PER_SEC = 1000 * 1000 * 1000;
constexpr double SEC_PER_MICROS = 1e-6;

namespace node {
namespace report {
using node::arraysize;
using node::ConditionVariable;
using node::DiagnosticFilename;
using node::Environment;
using node::JSONWriter;
using node::Mutex;
using node::NativeSymbolDebuggingContext;
using node::TIME_TYPE;

using node::worker::Worker;
using v8::Array;
using v8::Context;
Expand All @@ -54,8 +48,6 @@ using v8::TryCatch;
using v8::V8;
using v8::Value;

namespace per_process = node::per_process;

// Internal/static function declarations
static void WriteNodeReport(Isolate* isolate,
Environment* env,
Expand Down Expand Up @@ -129,7 +121,7 @@ std::string TriggerNodeReport(Isolate* isolate,
// Regular file. Append filename to directory path if one was specified
if (report_directory.length() > 0) {
std::string pathname = report_directory;
pathname += node::kPathSeparator;
pathname += kPathSeparator;
pathname += filename;
outfile.open(pathname, std::ios::out | std::ios::binary);
} else {
Expand Down Expand Up @@ -260,9 +252,9 @@ static void WriteNodeReport(Isolate* isolate,
}

// Report out the command line.
if (!node::per_process::cli_options->cmdline.empty()) {
if (!per_process::cli_options->cmdline.empty()) {
writer.json_arraystart("commandLine");
for (const std::string& arg : node::per_process::cli_options->cmdline) {
for (const std::string& arg : per_process::cli_options->cmdline) {
writer.json_element(arg);
}
writer.json_arrayend();
Expand Down Expand Up @@ -377,8 +369,8 @@ static void PrintVersionInformation(JSONWriter* writer) {

// Report Process word size
writer->json_keyvalue("wordSize", sizeof(void*) * 8);
writer->json_keyvalue("arch", node::per_process::metadata.arch);
writer->json_keyvalue("platform", node::per_process::metadata.platform);
writer->json_keyvalue("arch", per_process::metadata.arch);
writer->json_keyvalue("platform", per_process::metadata.platform);

// Report deps component versions
PrintComponentVersions(writer);
Expand Down Expand Up @@ -528,7 +520,7 @@ static Maybe<std::string> ErrorToString(Isolate* isolate,
maybe_str = error->ToString(context);
} else if (error->IsObject()) {
MaybeLocal<Value> stack = error.As<Object>()->Get(
context, node::FIXED_ONE_BYTE_STRING(isolate, "stack"));
context, FIXED_ONE_BYTE_STRING(isolate, "stack"));
if (!stack.IsEmpty() && stack.ToLocalChecked()->IsString()) {
maybe_str = stack.ToLocalChecked().As<String>();
}
Expand Down Expand Up @@ -656,7 +648,7 @@ static void PrintGCStatistics(JSONWriter* writer, Isolate* isolate) {
static void PrintResourceUsage(JSONWriter* writer) {
// Get process uptime in seconds
uint64_t uptime =
(uv_hrtime() - node::per_process::node_start_time) / (NANOS_PER_SEC);
(uv_hrtime() - per_process::node_start_time) / (NANOS_PER_SEC);
if (uptime == 0) uptime = 1; // avoid division by zero.

// Process and current thread usage statistics
Expand Down Expand Up @@ -714,7 +706,7 @@ static void PrintSystemInformation(JSONWriter* writer) {
writer->json_objectstart("environmentVariables");

{
Mutex::ScopedLock lock(node::per_process::env_var_mutex);
Mutex::ScopedLock lock(per_process::env_var_mutex);
r = uv_os_environ(&envitems, &envcount);
}

Expand Down Expand Up @@ -794,8 +786,7 @@ static void PrintComponentVersions(JSONWriter* writer) {

writer->json_objectstart("componentVersions");

#define V(key) \
writer->json_keyvalue(#key, node::per_process::metadata.versions.key);
#define V(key) writer->json_keyvalue(#key, per_process::metadata.versions.key);
NODE_VERSIONS_KEYS(V)
#undef V

Expand All @@ -805,22 +796,22 @@ static void PrintComponentVersions(JSONWriter* writer) {
// Report runtime release information.
static void PrintRelease(JSONWriter* writer) {
writer->json_objectstart("release");
writer->json_keyvalue("name", node::per_process::metadata.release.name);
writer->json_keyvalue("name", per_process::metadata.release.name);
#if NODE_VERSION_IS_LTS
writer->json_keyvalue("lts", node::per_process::metadata.release.lts);
writer->json_keyvalue("lts", per_process::metadata.release.lts);
#endif

#ifdef NODE_HAS_RELEASE_URLS
writer->json_keyvalue("headersUrl",
node::per_process::metadata.release.headers_url);
writer->json_keyvalue("sourceUrl",
node::per_process::metadata.release.source_url);
per_process::metadata.release.headers_url);
writer->json_keyvalue("sourceUrl", per_process::metadata.release.source_url);
#ifdef _WIN32
writer->json_keyvalue("libUrl", node::per_process::metadata.release.lib_url);
writer->json_keyvalue("libUrl", per_process::metadata.release.lib_url);
#endif // _WIN32
#endif // NODE_HAS_RELEASE_URLS

writer->json_objectend();
}

} // namespace report
} // namespace node
15 changes: 12 additions & 3 deletions src/node_report.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#pragma once
#ifndef SRC_NODE_REPORT_H_
#define SRC_NODE_REPORT_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "node.h"
#include "node_buffer.h"
Expand All @@ -12,17 +15,18 @@

#include <iomanip>

namespace node {
namespace report {

// Function declarations - functions in src/node_report.cc
std::string TriggerNodeReport(v8::Isolate* isolate,
node::Environment* env,
Environment* env,
const char* message,
const char* trigger,
const std::string& name,
v8::Local<v8::Value> error);
void GetNodeReport(v8::Isolate* isolate,
node::Environment* env,
Environment* env,
const char* message,
const char* trigger,
v8::Local<v8::Value> error,
Expand All @@ -45,3 +49,8 @@ void WriteReport(const v8::FunctionCallbackInfo<v8::Value>& info);
void GetReport(const v8::FunctionCallbackInfo<v8::Value>& info);

} // namespace report
} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_NODE_REPORT_H_
41 changes: 21 additions & 20 deletions src/node_report_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <atomic>
#include <sstream>

namespace node {
namespace report {
using node::Environment;
using node::Mutex;
Expand Down Expand Up @@ -76,48 +77,48 @@ void GetReport(const FunctionCallbackInfo<Value>& info) {
}

static void GetCompact(const FunctionCallbackInfo<Value>& info) {
node::Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
info.GetReturnValue().Set(node::per_process::cli_options->report_compact);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
info.GetReturnValue().Set(per_process::cli_options->report_compact);
}

static void SetCompact(const FunctionCallbackInfo<Value>& info) {
node::Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
bool compact = info[0]->ToBoolean(isolate)->Value();
node::per_process::cli_options->report_compact = compact;
per_process::cli_options->report_compact = compact;
}

static void GetDirectory(const FunctionCallbackInfo<Value>& info) {
node::Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
Environment* env = Environment::GetCurrent(info);
std::string directory = node::per_process::cli_options->report_directory;
std::string directory = per_process::cli_options->report_directory;
auto result = String::NewFromUtf8(env->isolate(), directory.c_str());
info.GetReturnValue().Set(result.ToLocalChecked());
}

static void SetDirectory(const FunctionCallbackInfo<Value>& info) {
node::Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
Environment* env = Environment::GetCurrent(info);
CHECK(info[0]->IsString());
Utf8Value dir(env->isolate(), info[0].As<String>());
node::per_process::cli_options->report_directory = *dir;
per_process::cli_options->report_directory = *dir;
}

static void GetFilename(const FunctionCallbackInfo<Value>& info) {
node::Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
Environment* env = Environment::GetCurrent(info);
std::string filename = node::per_process::cli_options->report_filename;
std::string filename = per_process::cli_options->report_filename;
auto result = String::NewFromUtf8(env->isolate(), filename.c_str());
info.GetReturnValue().Set(result.ToLocalChecked());
}

static void SetFilename(const FunctionCallbackInfo<Value>& info) {
node::Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
Environment* env = Environment::GetCurrent(info);
CHECK(info[0]->IsString());
Utf8Value name(env->isolate(), info[0].As<String>());
node::per_process::cli_options->report_filename = *name;
per_process::cli_options->report_filename = *name;
}

static void GetSignal(const FunctionCallbackInfo<Value>& info) {
Expand All @@ -135,15 +136,14 @@ static void SetSignal(const FunctionCallbackInfo<Value>& info) {
}

static void ShouldReportOnFatalError(const FunctionCallbackInfo<Value>& info) {
Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
info.GetReturnValue().Set(
node::per_process::cli_options->report_on_fatalerror);
Mutex::ScopedLock lock(per_process::cli_options_mutex);
info.GetReturnValue().Set(per_process::cli_options->report_on_fatalerror);
}

static void SetReportOnFatalError(const FunctionCallbackInfo<Value>& info) {
CHECK(info[0]->IsBoolean());
Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
node::per_process::cli_options->report_on_fatalerror = info[0]->IsTrue();
Mutex::ScopedLock lock(per_process::cli_options_mutex);
per_process::cli_options->report_on_fatalerror = info[0]->IsTrue();
}

static void ShouldReportOnSignal(const FunctionCallbackInfo<Value>& info) {
Expand Down Expand Up @@ -197,7 +197,7 @@ static void Initialize(Local<Object> exports,
SetReportOnUncaughtException);
}

void RegisterExternalReferences(node::ExternalReferenceRegistry* registry) {
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(WriteReport);
registry->Register(GetReport);
registry->Register(GetCompact);
Expand All @@ -217,6 +217,7 @@ void RegisterExternalReferences(node::ExternalReferenceRegistry* registry) {
}

} // namespace report
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(report, report::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(report, report::RegisterExternalReferences)
NODE_MODULE_CONTEXT_AWARE_INTERNAL(report, node::report::Initialize)
NODE_MODULE_EXTERNAL_REFERENCE(report, node::report::RegisterExternalReferences)
8 changes: 3 additions & 5 deletions src/node_report_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
#include "node_report.h"
#include "util-inl.h"

namespace node {
namespace report {

using node::JSONWriter;
using node::MallocedBuffer;

static constexpr auto null = JSONWriter::Null{};

// Utility function to format socket information.
Expand Down Expand Up @@ -210,8 +208,7 @@ void WalkHandle(uv_handle_t* h, void* arg) {
// SIGWINCH is used by libuv so always appears.
// See http://docs.libuv.org/en/v1.x/signal.html
writer->json_keyvalue("signum", handle->signal.signum);
writer->json_keyvalue("signal",
node::signo_string(handle->signal.signum));
writer->json_keyvalue("signal", signo_string(handle->signal.signum));
break;
default:
break;
Expand Down Expand Up @@ -276,3 +273,4 @@ void WalkHandle(uv_handle_t* h, void* arg) {
}

} // namespace report
} // namespace node

0 comments on commit 67cfeef

Please sign in to comment.