Skip to content

Commit

Permalink
fixup! report: expose report public native apis
Browse files Browse the repository at this point in the history
  • Loading branch information
legendecas committed Aug 19, 2022
1 parent 8c148bf commit 980e038
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 36 deletions.
8 changes: 4 additions & 4 deletions src/node.h
Expand Up @@ -75,8 +75,9 @@
#include "v8-platform.h" // NOLINT(build/include_order)
#include "node_version.h" // NODE_MODULE_VERSION

#include <memory>
#include <functional>
#include <memory>
#include <ostream>

// We cannot use __POSIX__ in this header because that's only defined when
// building Node.js.
Expand Down Expand Up @@ -620,16 +621,15 @@ NODE_EXTERN v8::MaybeLocal<v8::Value> PrepareStackTraceCallback(
// Writes a diagnostic report to a file. If filename is not provided, the
// default filename includes the date, time, PID, and a sequence number.
// The report's JavaScript stack trace is taken from err, if present.
// If isolate or env is nullptr, no information about the isolate and env
// If isolate is nullptr, no information about the JavaScript environment
// is included in the report.
// Returns the filename of the written report.
NODE_EXTERN std::string TriggerNodeReport(v8::Isolate* isolate,
Environment* env,
const char* message,
const char* trigger,
const std::string& filename,
v8::Local<v8::Value> error);
NODE_EXTERN void GetNodeReport(v8::Isolate* isolate,
Environment* env,
const char* message,
const char* trigger,
v8::Local<v8::Value> error,
Expand Down
12 changes: 2 additions & 10 deletions src/node_errors.cc
Expand Up @@ -474,18 +474,14 @@ void OnFatalError(const char* location, const char* message) {
}

Isolate* isolate = Isolate::TryGetCurrent();
Environment* env = nullptr;
if (isolate != nullptr) {
env = Environment::GetCurrent(isolate);
}
bool report_on_fatalerror;
{
Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
report_on_fatalerror = per_process::cli_options->report_on_fatalerror;
}

if (report_on_fatalerror) {
TriggerNodeReport(isolate, env, message, "FatalError", "", Local<Object>());
TriggerNodeReport(isolate, message, "FatalError", "", Local<Object>());
}

fflush(stderr);
Expand All @@ -503,18 +499,14 @@ void OOMErrorHandler(const char* location, bool is_heap_oom) {
}

Isolate* isolate = Isolate::TryGetCurrent();
Environment* env = nullptr;
if (isolate != nullptr) {
env = Environment::GetCurrent(isolate);
}
bool report_on_fatalerror;
{
Mutex::ScopedLock lock(node::per_process::cli_options_mutex);
report_on_fatalerror = per_process::cli_options->report_on_fatalerror;
}

if (report_on_fatalerror) {
TriggerNodeReport(isolate, env, message, "OOMError", "", Local<Object>());
TriggerNodeReport(isolate, message, "OOMError", "", Local<Object>());
}

fflush(stderr);
Expand Down
20 changes: 19 additions & 1 deletion src/node_report.cc
Expand Up @@ -790,11 +790,15 @@ static void PrintRelease(JSONWriter* writer) {

// External function to trigger a report, writing to file.
std::string TriggerNodeReport(Isolate* isolate,
Environment* env,
const char* message,
const char* trigger,
const std::string& name,
Local<Value> error) {
Environment* env = nullptr;
if (isolate != nullptr) {
env = Environment::GetCurrent(isolate);
}

std::string filename;

// Determine the required report filename. In order of priority:
Expand Down Expand Up @@ -875,6 +879,20 @@ std::string TriggerNodeReport(Isolate* isolate,
}

// External function to trigger a report, writing to a supplied stream.
void GetNodeReport(Isolate* isolate,
const char* message,
const char* trigger,
Local<Value> error,
std::ostream& out) {
Environment* env = nullptr;
if (isolate != nullptr) {
env = Environment::GetCurrent(isolate);
}
report::WriteNodeReport(
isolate, env, message, trigger, "", out, error, false);
}

// Helper function to get report for an environment.
void GetNodeReport(Isolate* isolate,
Environment* env,
const char* message,
Expand Down
8 changes: 1 addition & 7 deletions src/node_report.h
Expand Up @@ -36,13 +36,7 @@ void GetReport(const v8::FunctionCallbackInfo<v8::Value>& info);

} // namespace report

// Function declarations - functions in src/node_report.cc
std::string TriggerNodeReport(v8::Isolate* isolate,
Environment* env,
const char* message,
const char* trigger,
const std::string& name,
v8::Local<v8::Value> error);
// Helper function to get report for an environment.
void GetNodeReport(v8::Isolate* isolate,
Environment* env,
const char* message,
Expand Down
3 changes: 1 addition & 2 deletions src/node_report_module.cc
Expand Up @@ -44,8 +44,7 @@ void WriteReport(const FunctionCallbackInfo<Value>& info) {
else
error = Local<Value>();

filename = TriggerNodeReport(
isolate, env, *message, *trigger, filename, error);
filename = TriggerNodeReport(isolate, *message, *trigger, filename, error);
// Return value is the report filename
info.GetReturnValue().Set(
String::NewFromUtf8(isolate, filename.c_str()).ToLocalChecked());
Expand Down
7 changes: 1 addition & 6 deletions test/addons/report-api/binding.cc
Expand Up @@ -11,12 +11,7 @@ void TriggerReport(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();

node::TriggerNodeReport(
isolate,
node::GetCurrentEnvironment(isolate->GetCurrentContext()),
"FooMessage",
"BarTrigger",
std::string(),
Local<Value>());
isolate, "FooMessage", "BarTrigger", std::string(), Local<Value>());
}

void init(Local<Object> exports) {
Expand Down
8 changes: 2 additions & 6 deletions test/cctest/test_report.cc
Expand Up @@ -29,8 +29,7 @@ TEST_F(ReportTest, ReportWithNoIsolateAndEnv) {
SealHandleScope handle_scope(isolate_);

std::ostringstream oss;
node::GetNodeReport(
nullptr, nullptr, "FooMessage", "BarTrigger", Local<Value>(), oss);
node::GetNodeReport(nullptr, "FooMessage", "BarTrigger", Local<Value>(), oss);

// Simple checks on the output string contains the message and trigger.
std::string actual = oss.str();
Expand All @@ -48,12 +47,9 @@ TEST_F(ReportTest, ReportWithIsolateAndEnv) {
Function::New(context, [](const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
Environment* env =
node::GetCurrentEnvironment(isolate->GetCurrentContext());

std::ostringstream oss;
node::GetNodeReport(
isolate, env, "FooMessage", "BarTrigger", args[0], oss);
node::GetNodeReport(isolate, "FooMessage", "BarTrigger", args[0], oss);

// Simple checks on the output string contains the message and trigger.
std::string actual = oss.str();
Expand Down

0 comments on commit 980e038

Please sign in to comment.