-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConsole.cpp
More file actions
377 lines (313 loc) · 10.1 KB
/
Console.cpp
File metadata and controls
377 lines (313 loc) · 10.1 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "Console.h"
#include <sstream>
#include <string>
#include "ffi/NativeScriptException.h"
#include "js_native_api.h"
#include "native_api_util.h"
#include "runtime/RuntimeConfig.h"
#include "runtime/Util.h"
#ifdef TARGET_ENGINE_V8
#include "v8-api.h"
#endif
#ifdef __APPLE__
#include <CoreFoundation/CFString.h>
extern "C" void NSLog(CFStringRef format, ...);
#if defined(__has_include)
#if __has_include(<os/log.h>)
#include <os/log.h>
#define TNS_HAVE_OS_LOG 1
#else
#define TNS_HAVE_OS_LOG 0
#endif
#else
#define TNS_HAVE_OS_LOG 0
#endif
#else
#include <iostream>
#endif
namespace nativescript {
JS_CLASS_INIT(Console::Init) {
napi_value Console, console;
const napi_property_descriptor properties[] = {
napi_util::desc("log", Log, (void*)kConsoleLogTypeLog),
napi_util::desc("error", Log, (void*)kConsoleLogTypeError),
napi_util::desc("warn", Log, (void*)kConsoleLogTypeWarn),
napi_util::desc("info", Log, (void*)kConsoleLogTypeInfo),
napi_util::desc("assert", Log, (void*)kConsoleLogTypeAssert),
napi_util::desc("time", Time, nullptr),
napi_util::desc("timeEnd", TimeEnd, nullptr),
napi_util::desc("dir", Dir, nullptr),
napi_util::desc("trace", Trace, nullptr),
};
napi_define_class(env, "Console", NAPI_AUTO_LENGTH, Console::Constructor,
nullptr, 9, properties, &Console);
napi_new_instance(env, Console, 0, nullptr, &console);
const napi_property_descriptor globalProperties[] = {
napi_util::desc("Console", Console),
napi_util::desc("console", console),
};
napi_define_properties(env, global, 2, globalProperties);
}
JS_METHOD(Console::Constructor) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
std::string transformJSObject(napi_env env, napi_value object) {
napi_value toStringFunc;
bool hasToString = false;
// Check if the object has a toString method
napi_has_named_property(env, object, "toString", &hasToString);
if (hasToString) {
napi_get_named_property(env, object, "toString", &toStringFunc);
if (napi_util::is_of_type(env, toStringFunc, napi_function)) {
napi_value result;
napi_call_function(env, object, toStringFunc, 0, nullptr, &result);
auto value = napi_util::get_cxx_string(env, result);
auto hasCustomToStringImplementation =
value.find("[object Object]") == std::string::npos;
if (hasCustomToStringImplementation) return value;
}
}
// If no custom toString method, stringify the object
return JsonStringifyObject(env, object, false);
}
std::string buildStringFromArg(napi_env env, napi_value val,
napi_value inspectSymbol) {
napi_valuetype type;
napi_typeof(env, val, &type);
if (type == napi_function) {
napi_value funcString;
napi_coerce_to_string(env, val, &funcString);
return napi_util::get_string_value(env, funcString);
} else if (napi_util::is_array(env, val)) {
napi_value cachedSelf = val;
// Get array length
uint32_t arrayLength;
napi_get_array_length(env, val, &arrayLength);
std::stringstream arrayStr;
arrayStr << "[";
for (uint32_t i = 0; i < arrayLength; i++) {
napi_value propertyValue;
napi_get_element(env, val, i, &propertyValue);
// Check for circular reference
bool isStrictEqual = false;
napi_strict_equals(env, propertyValue, cachedSelf, &isStrictEqual);
if (isStrictEqual) {
arrayStr << "[Circular]";
} else {
std::string elementString =
buildStringFromArg(env, propertyValue, inspectSymbol);
arrayStr << elementString;
}
if (i != arrayLength - 1) {
arrayStr << ", ";
}
}
arrayStr << "]";
return arrayStr.str();
} else if (type == napi_object) {
napi_value inspectFunc = nullptr;
napi_status getInspectStatus =
napi_get_property(env, val, inspectSymbol, &inspectFunc);
if (getInspectStatus == napi_ok &&
napi_util::is_of_type(env, inspectFunc, napi_function)) {
napi_value inspectedValue;
napi_call_function(env, val, inspectFunc, 0, nullptr, &inspectedValue);
return buildStringFromArg(env, inspectedValue, inspectSymbol);
}
return transformJSObject(env, val);
} else if (type == napi_symbol) {
napi_value symString;
napi_coerce_to_string(env, val, &symString);
return "Symbol(" + napi_util::get_cxx_string(env, symString) + ")";
} else {
napi_value defaultToString;
napi_coerce_to_string(env, val, &defaultToString);
return napi_util::get_string_value(env, defaultToString);
}
}
std::string buildLogString(napi_env env, napi_callback_info info,
int startingIndex = 0) {
NAPI_CALLBACK_BEGIN_VARGS()
napi_value global, Symbol, SymbolFor, symbolDescription, inspectSymbol;
napi_get_global(env, &global);
napi_get_named_property(env, global, "Symbol", &Symbol);
napi_get_named_property(env, Symbol, "for", &SymbolFor);
napi_create_string_utf8(env, "nodejs.util.inspect.custom", NAPI_AUTO_LENGTH,
&symbolDescription);
napi_call_function(env, global, SymbolFor, 1, &symbolDescription,
&inspectSymbol);
std::stringstream ss;
if (argc) {
for (size_t i = startingIndex; i < argc; i++) {
// separate args with a space
if (i != 0) {
ss << " ";
}
std::string argString = buildStringFromArg(env, argv[i], inspectSymbol);
ss << argString;
}
} else {
ss << std::endl;
}
return ss.str();
}
JS_METHOD(Console::Log) {
try {
if (!RuntimeConfig.LogToSystemConsole) {
return UNDEFINED;
}
size_t argc = 0;
ConsoleLogType stream;
void* data = nullptr;
napi_get_cb_info(env, cbinfo, &argc, nullptr, nullptr, &data);
stream = ConsoleLogType((unsigned long)data);
size_t initialArg = 0;
if (stream == kConsoleLogTypeAssert) {
bool passes = false;
if (argc > 0) {
napi_value firstArg = nullptr;
napi_get_cb_info(env, cbinfo, &argc, &firstArg, nullptr, nullptr);
napi_coerce_to_bool(env, firstArg, &firstArg);
napi_get_value_bool(env, firstArg, &passes);
}
if (!passes) {
initialArg = 1;
} else {
return UNDEFINED;
}
}
napi_value argv[argc];
napi_get_cb_info(env, cbinfo, &argc, argv, nullptr, nullptr);
std::stringstream log;
// TODO(dj): what if we made this pretty?
log << "CONSOLE";
switch (stream) {
case kConsoleLogTypeLog:
log << " LOG";
break;
case kConsoleLogTypeError:
log << " ERROR";
break;
case kConsoleLogTypeWarn:
log << " WARN";
break;
case kConsoleLogTypeInfo:
log << " INFO";
break;
case kConsoleLogTypeAssert:
log << " ASSERT FAILED";
break;
}
log << ": ";
log << buildLogString(env, cbinfo, initialArg);
log << "\n";
std::string logString = log.str();
#ifdef __APPLE__
#if TARGET_OS_OSX
NSLog(CFSTR("%s"), logString.c_str());
#elif TNS_HAVE_OS_LOG
os_log(OS_LOG_DEFAULT, "%{public}s", logString.c_str());
#else
NSLog(CFSTR("%s"), logString.c_str());
#endif
#else
switch (stream) {
case kConsoleLogTypeLog:
case kConsoleLogTypeInfo:
std::cout << logString;
break;
case kConsoleLogTypeError:
case kConsoleLogTypeWarn:
case kConsoleLogTypeAssert:
std::cerr << logString;
break;
}
#endif
if (RuntimeConfig.CustomLogCallback) {
RuntimeConfig.CustomLogCallback(logString.c_str());
}
#ifdef TARGET_ENGINE_V8
v8_inspector::ConsoleAPIType method;
switch (stream) {
case kConsoleLogTypeLog:
method = v8_inspector::ConsoleAPIType::kLog;
break;
case kConsoleLogTypeError:
method = v8_inspector::ConsoleAPIType::kError;
break;
case kConsoleLogTypeWarn:
method = v8_inspector::ConsoleAPIType::kWarning;
break;
case kConsoleLogTypeInfo:
method = v8_inspector::ConsoleAPIType::kInfo;
break;
case kConsoleLogTypeAssert:
method = v8_inspector::ConsoleAPIType::kAssert;
break;
default:
break;
}
sendToDevToolsFrontEnd(env, method, logString);
#endif
} catch (NativeScriptException& e) {
e.ReThrowToJS(env);
}
return UNDEFINED;
}
JS_METHOD(Console::Time) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
JS_METHOD(Console::TimeEnd) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
JS_METHOD(Console::Dir) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
JS_METHOD(Console::Trace) {
napi_value thisArg;
napi_get_cb_info(env, cbinfo, nullptr, nullptr, &thisArg, nullptr);
return thisArg;
}
#ifdef TARGET_ENGINE_V8
int Console::sendToDevToolsFrontEnd(napi_env env,
v8_inspector::ConsoleAPIType method,
napi_callback_info info) {
if (!m_callback) {
return 0;
}
NAPI_CALLBACK_BEGIN_VARGS()
v8::Local<v8::Value>* v8_args = reinterpret_cast<v8::Local<v8::Value>*>(
const_cast<napi_value*>(argv.data()));
std::vector<v8::Local<v8::Value>> arg_vector;
unsigned nargs = argc;
arg_vector.reserve(nargs);
for (unsigned ix = 0; ix < nargs; ix++) arg_vector.push_back(v8_args[ix]);
m_callback(env, method, arg_vector);
return 0;
}
void Console::sendToDevToolsFrontEnd(napi_env env,
v8_inspector::ConsoleAPIType method,
const std::string& message) {
if (!m_callback) {
return;
}
v8::Local<v8::Value> v8_message =
v8::String::NewFromUtf8(env->isolate, message.c_str(),
v8::NewStringType::kNormal, message.length())
.ToLocalChecked();
std::vector<v8::Local<v8::Value>> args{
v8_message,
};
m_callback(env, method, args);
}
#endif
ConsoleCallback Console::m_callback = nullptr;
} // namespace nativescript