summaryrefslogtreecommitdiff
path: root/src/node_report_module.cc
blob: f35d23a06e32321d272fdaeec7c0dfa1351fc25e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "env.h"
#include "node_errors.h"
#include "node_internals.h"
#include "node_options.h"
#include "node_report.h"
#include "util-inl.h"

#include "handle_wrap.h"
#include "node_buffer.h"
#include "stream_base-inl.h"
#include "stream_wrap.h"

#include <v8.h>
#include <atomic>
#include <sstream>

namespace report {
using node::Environment;
using node::Utf8Value;
using v8::Boolean;
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::String;
using v8::Value;

void WriteReport(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  Isolate* isolate = env->isolate();
  HandleScope scope(isolate);
  std::string filename;
  Local<String> stackstr;

  CHECK_EQ(info.Length(), 4);
  String::Utf8Value message(isolate, info[0].As<String>());
  String::Utf8Value trigger(isolate, info[1].As<String>());
  stackstr = info[3].As<String>();

  if (info[2]->IsString())
    filename = *String::Utf8Value(isolate, info[2]);

  filename = TriggerNodeReport(
      isolate, env, *message, *trigger, filename, stackstr);
  // Return value is the report filename
  info.GetReturnValue().Set(
      String::NewFromUtf8(isolate, filename.c_str(), v8::NewStringType::kNormal)
          .ToLocalChecked());
}

// External JavaScript API for returning a report
void GetReport(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  Isolate* isolate = env->isolate();
  HandleScope scope(isolate);
  std::ostringstream out;

  GetNodeReport(
      isolate, env, "JavaScript API", __func__, info[0].As<String>(), out);

  // Return value is the contents of a report as a string.
  info.GetReturnValue().Set(String::NewFromUtf8(isolate,
                                                out.str().c_str(),
                                                v8::NewStringType::kNormal)
                                .ToLocalChecked());
}

static void GetDirectory(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  std::string directory = env->isolate_data()->options()->report_directory;
  auto result = String::NewFromUtf8(env->isolate(),
                                    directory.c_str(),
                                    v8::NewStringType::kNormal);
  info.GetReturnValue().Set(result.ToLocalChecked());
}

static void SetDirectory(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  CHECK(info[0]->IsString());
  Utf8Value dir(env->isolate(), info[0].As<String>());
  env->isolate_data()->options()->report_directory = *dir;
}

static void GetFilename(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  std::string filename = env->isolate_data()->options()->report_filename;
  auto result = String::NewFromUtf8(env->isolate(),
                                    filename.c_str(),
                                    v8::NewStringType::kNormal);
  info.GetReturnValue().Set(result.ToLocalChecked());
}

static void SetFilename(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  CHECK(info[0]->IsString());
  Utf8Value name(env->isolate(), info[0].As<String>());
  env->isolate_data()->options()->report_filename = *name;
}

static void GetSignal(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  std::string signal = env->isolate_data()->options()->report_signal;
  auto result = String::NewFromUtf8(env->isolate(),
                                    signal.c_str(),
                                    v8::NewStringType::kNormal);
  info.GetReturnValue().Set(result.ToLocalChecked());
}

static void SetSignal(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  CHECK(info[0]->IsString());
  Utf8Value signal(env->isolate(), info[0].As<String>());
  env->isolate_data()->options()->report_signal = *signal;
}

static void ShouldReportOnFatalError(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  info.GetReturnValue().Set(
      env->isolate_data()->options()->report_on_fatalerror);
}

static void SetReportOnFatalError(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  CHECK(info[0]->IsBoolean());
  env->isolate_data()->options()->report_on_fatalerror = info[0]->IsTrue();
}

static void ShouldReportOnSignal(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  info.GetReturnValue().Set(env->isolate_data()->options()->report_on_signal);
}

static void SetReportOnSignal(const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  CHECK(info[0]->IsBoolean());
  env->isolate_data()->options()->report_on_signal = info[0]->IsTrue();
}

static void ShouldReportOnUncaughtException(
    const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  info.GetReturnValue().Set(
      env->isolate_data()->options()->report_uncaught_exception);
}

static void SetReportOnUncaughtException(
    const FunctionCallbackInfo<Value>& info) {
  Environment* env = Environment::GetCurrent(info);
  CHECK(info[0]->IsBoolean());
  env->isolate_data()->options()->report_uncaught_exception = info[0]->IsTrue();
}

static void Initialize(Local<Object> exports,
                       Local<Value> unused,
                       Local<Context> context,
                       void* priv) {
  Environment* env = Environment::GetCurrent(context);

  env->SetMethod(exports, "writeReport", WriteReport);
  env->SetMethod(exports, "getReport", GetReport);
  env->SetMethod(exports, "getDirectory", GetDirectory);
  env->SetMethod(exports, "setDirectory", SetDirectory);
  env->SetMethod(exports, "getFilename", GetFilename);
  env->SetMethod(exports, "setFilename", SetFilename);
  env->SetMethod(exports, "getSignal", GetSignal);
  env->SetMethod(exports, "setSignal", SetSignal);
  env->SetMethod(exports, "shouldReportOnFatalError", ShouldReportOnFatalError);
  env->SetMethod(exports, "setReportOnFatalError", SetReportOnFatalError);
  env->SetMethod(exports, "shouldReportOnSignal", ShouldReportOnSignal);
  env->SetMethod(exports, "setReportOnSignal", SetReportOnSignal);
  env->SetMethod(exports, "shouldReportOnUncaughtException",
                 ShouldReportOnUncaughtException);
  env->SetMethod(exports, "setReportOnUncaughtException",
                 SetReportOnUncaughtException);
}

}  // namespace report

NODE_MODULE_CONTEXT_AWARE_INTERNAL(report, report::Initialize)