5 #include "src/d8-console.h" 7 #include "src/isolate.h" 12 void WriteToFile(
const char* prefix, FILE* file, Isolate* isolate,
13 const debug::ConsoleCallArguments& args) {
14 if (prefix) fprintf(file,
"%s: ", prefix);
15 for (
int i = 0;
i < args.Length();
i++) {
16 HandleScope handle_scope(isolate);
17 if (
i > 0) fprintf(file,
" ");
19 Local<Value> arg = args[
i];
20 Local<String> str_obj;
23 if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj))
return;
26 int n =
static_cast<int>(fwrite(*str,
sizeof(**str), str.length(), file));
27 if (n != str.length()) {
28 printf(
"Error in fwrite\n");
29 base::OS::ExitProcess(1);
36 D8Console::D8Console(Isolate* isolate) : isolate_(isolate) {
37 default_timer_ = base::TimeTicks::HighResolutionNow();
40 void D8Console::Assert(
const debug::ConsoleCallArguments& args,
44 if (args.Length() > 0 && args[0]->BooleanValue(isolate_))
return;
45 WriteToFile(
"console.assert", stdout, isolate_, args);
46 isolate_->ThrowException(v8::Exception::Error(
52 void D8Console::Log(
const debug::ConsoleCallArguments& args,
54 WriteToFile(
nullptr, stdout, isolate_, args);
57 void D8Console::Error(
const debug::ConsoleCallArguments& args,
59 WriteToFile(
"console.error", stderr, isolate_, args);
62 void D8Console::Warn(
const debug::ConsoleCallArguments& args,
64 WriteToFile(
"console.warn", stdout, isolate_, args);
67 void D8Console::Info(
const debug::ConsoleCallArguments& args,
69 WriteToFile(
"console.info", stdout, isolate_, args);
72 void D8Console::Debug(
const debug::ConsoleCallArguments& args,
74 WriteToFile(
"console.debug", stdout, isolate_, args);
77 void D8Console::Time(
const debug::ConsoleCallArguments& args,
79 if (args.Length() == 0) {
80 default_timer_ = base::TimeTicks::HighResolutionNow();
82 Local<Value> arg = args[0];
84 v8::TryCatch try_catch(isolate_);
85 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label))
return;
87 std::string string(*utf8);
88 auto find = timers_.find(
string);
89 if (find != timers_.end()) {
90 find->second = base::TimeTicks::HighResolutionNow();
92 timers_.insert(std::pair<std::string, base::TimeTicks>(
93 string, base::TimeTicks::HighResolutionNow()));
98 void D8Console::TimeEnd(
const debug::ConsoleCallArguments& args,
100 base::TimeDelta delta;
101 if (args.Length() == 0) {
102 delta = base::TimeTicks::HighResolutionNow() - default_timer_;
103 printf(
"console.timeEnd: default, %f\n", delta.InMillisecondsF());
105 base::TimeTicks now = base::TimeTicks::HighResolutionNow();
106 Local<Value> arg = args[0];
108 v8::TryCatch try_catch(isolate_);
109 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label))
return;
111 std::string string(*utf8);
112 auto find = timers_.find(
string);
113 if (find != timers_.end()) {
114 delta = now - find->second;
116 printf(
"console.timeEnd: %s, %f\n", *utf8, delta.InMillisecondsF());
120 void D8Console::TimeStamp(
const debug::ConsoleCallArguments& args,
122 base::TimeDelta delta = base::TimeTicks::HighResolutionNow() - default_timer_;
123 if (args.Length() == 0) {
124 printf(
"console.timeStamp: default, %f\n", delta.InMillisecondsF());
126 Local<Value> arg = args[0];
128 v8::TryCatch try_catch(isolate_);
129 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label))
return;
131 std::string string(*utf8);
132 printf(
"console.timeStamp: %s, %f\n", *utf8, delta.InMillisecondsF());
136 void D8Console::Trace(
const debug::ConsoleCallArguments& args,
139 i_isolate->PrintStack(stderr, i::Isolate::kPrintStackConcise);
static V8_INLINE Local< T > Cast(Local< S > that)
static V8_WARN_UNUSED_RESULT MaybeLocal< String > NewFromUtf8(Isolate *isolate, const char *data, v8::NewStringType type, int length=-1)