Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report: use DiagnosticFilename #26647

Merged
merged 2 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions doc/api/report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ is provided below for reference.
"header": {
"event": "exception",
"trigger": "Exception",
"filename": "report.20181221.005011.8974.001.json",
"filename": "report.20181221.005011.8974.0.001.json",
"dumpEventTime": "2018-12-21T00:50:11Z",
"dumpEventTimeStamp": "1545371411331",
"processId": 8974,
Expand Down Expand Up @@ -471,7 +471,7 @@ triggered using the Node.js REPL:
```raw
$ node
> process.report.writeReport();
Writing Node.js report to file: report.20181126.091102.8480.001.json
Writing Node.js report to file: report.20181126.091102.8480.0.001.json
Node.js report completed
>
```
Expand Down
88 changes: 26 additions & 62 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ constexpr double SEC_PER_MICROS = 1e-6;

namespace report {
using node::arraysize;
using node::DiagnosticFilename;
using node::Environment;
using node::Mutex;
using node::NativeSymbolDebuggingContext;
Expand All @@ -64,8 +65,7 @@ static void WriteNodeReport(Isolate* isolate,
const char* trigger,
const std::string& filename,
std::ostream& out,
Local<String> stackstr,
TIME_TYPE* tm_struct);
Local<String> stackstr);
static void PrintVersionInformation(JSONWriter* writer);
static void PrintJavaScriptStack(JSONWriter* writer,
Isolate* isolate,
Expand All @@ -78,7 +78,6 @@ static void PrintSystemInformation(JSONWriter* writer);
static void PrintLoadedLibraries(JSONWriter* writer);
static void PrintComponentVersions(JSONWriter* writer);
static void PrintRelease(JSONWriter* writer);
static void LocalTime(TIME_TYPE* tm_struct);

// Global variables
static std::atomic_int seq = {0}; // sequence number for report filenames
Expand All @@ -92,46 +91,23 @@ std::string TriggerNodeReport(Isolate* isolate,
const char* trigger,
const std::string& name,
Local<String> stackstr) {
std::ostringstream oss;
std::string filename;
std::shared_ptr<PerIsolateOptions> options;
if (env != nullptr) options = env->isolate_data()->options();

// Obtain the current time and the pid (platform dependent)
TIME_TYPE tm_struct;
LocalTime(&tm_struct);
// Determine the required report filename. In order of priority:
// 1) supplied on API 2) configured on startup 3) default generated
if (!name.empty()) {
// Filename was specified as API parameter, use that
oss << name;
// Filename was specified as API parameter.
filename = name;
} else if (env != nullptr && options->report_filename.length() > 0) {
// File name was supplied via start-up option, use that
oss << options->report_filename;
// File name was supplied via start-up option.
filename = options->report_filename;
} else {
// Construct the report filename, with timestamp, pid and sequence number
oss << "report";
#ifdef _WIN32
oss << "." << std::setfill('0') << std::setw(4) << tm_struct.wYear;
oss << std::setfill('0') << std::setw(2) << tm_struct.wMonth;
oss << std::setfill('0') << std::setw(2) << tm_struct.wDay;
oss << "." << std::setfill('0') << std::setw(2) << tm_struct.wHour;
oss << std::setfill('0') << std::setw(2) << tm_struct.wMinute;
oss << std::setfill('0') << std::setw(2) << tm_struct.wSecond;
#else // UNIX, OSX
oss << "." << std::setfill('0') << std::setw(4) << tm_struct.tm_year + 1900;
oss << std::setfill('0') << std::setw(2) << tm_struct.tm_mon + 1;
oss << std::setfill('0') << std::setw(2) << tm_struct.tm_mday;
oss << "." << std::setfill('0') << std::setw(2) << tm_struct.tm_hour;
oss << std::setfill('0') << std::setw(2) << tm_struct.tm_min;
oss << std::setfill('0') << std::setw(2) << tm_struct.tm_sec;
#endif
oss << "." << uv_os_getpid();
oss << "." << std::setfill('0') << std::setw(3) << ++seq;
oss << ".json";
filename = *DiagnosticFilename(env != nullptr ? env->thread_id() : 0,
"report", "json", seq++);
}

filename = oss.str();
// Open the report file stream for writing. Supports stdout/err,
// user-specified or (default) generated name
std::ofstream outfile;
Expand Down Expand Up @@ -166,7 +142,7 @@ std::string TriggerNodeReport(Isolate* isolate,
}

WriteNodeReport(isolate, env, message, trigger, filename, *outstream,
stackstr, &tm_struct);
stackstr);

// Do not close stdout/stderr, only close files we opened.
if (outfile.is_open()) {
Expand All @@ -184,11 +160,7 @@ void GetNodeReport(Isolate* isolate,
const char* trigger,
Local<String> stackstr,
std::ostream& out) {
// Obtain the current time and the pid (platform dependent)
TIME_TYPE tm_struct;
LocalTime(&tm_struct);
WriteNodeReport(
isolate, env, message, trigger, "", out, stackstr, &tm_struct);
WriteNodeReport(isolate, env, message, trigger, "", out, stackstr);
}

// Internal function to coordinate and write the various
Expand All @@ -199,8 +171,10 @@ static void WriteNodeReport(Isolate* isolate,
const char* trigger,
const std::string& filename,
std::ostream& out,
Local<String> stackstr,
TIME_TYPE* tm_struct) {
Local<String> stackstr) {
// Obtain the current time and the pid.
TIME_TYPE tm_struct;
DiagnosticFilename::LocalTime(&tm_struct);
uv_pid_t pid = uv_os_getpid();

// Save formatting for output stream.
Expand All @@ -227,23 +201,23 @@ static void WriteNodeReport(Isolate* isolate,
snprintf(timebuf,
sizeof(timebuf),
"%4d-%02d-%02dT%02d:%02d:%02dZ",
tm_struct->wYear,
tm_struct->wMonth,
tm_struct->wDay,
tm_struct->wHour,
tm_struct->wMinute,
tm_struct->wSecond);
tm_struct.wYear,
tm_struct.wMonth,
tm_struct.wDay,
tm_struct.wHour,
tm_struct.wMinute,
tm_struct.wSecond);
writer.json_keyvalue("dumpEventTime", timebuf);
#else // UNIX, OSX
snprintf(timebuf,
sizeof(timebuf),
"%4d-%02d-%02dT%02d:%02d:%02dZ",
tm_struct->tm_year + 1900,
tm_struct->tm_mon + 1,
tm_struct->tm_mday,
tm_struct->tm_hour,
tm_struct->tm_min,
tm_struct->tm_sec);
tm_struct.tm_year + 1900,
tm_struct.tm_mon + 1,
tm_struct.tm_mday,
tm_struct.tm_hour,
tm_struct.tm_min,
tm_struct.tm_sec);
writer.json_keyvalue("dumpEventTime", timebuf);
struct timeval ts;
gettimeofday(&ts, nullptr);
Expand Down Expand Up @@ -638,14 +612,4 @@ static void PrintRelease(JSONWriter* writer) {
writer->json_objectend();
}

static void LocalTime(TIME_TYPE* tm_struct) {
#ifdef _WIN32
GetLocalTime(tm_struct);
#else // UNIX, OSX
struct timeval time_val;
gettimeofday(&time_val, nullptr);
localtime_r(&time_val.tv_sec, tm_struct);
#endif
}

} // namespace report
4 changes: 2 additions & 2 deletions test/common/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ const path = require('path');

function findReports(pid, dir) {
// Default filenames are of the form
// report.<date>.<time>.<pid>.<seq>.json
const format = '^report\\.\\d+\\.\\d+\\.' + pid + '\\.\\d+\\.json$';
// report.<date>.<time>.<pid>.<tid>.<seq>.json
const format = '^report\\.\\d+\\.\\d+\\.' + pid + '\\.\\d+\\.\\d+\\.json$';
const filePattern = new RegExp(format);
const files = fs.readdirSync(dir);
const results = [];
Expand Down