Chromium Code Reviews| Index: src/d8-console.cc |
| diff --git a/src/d8-console.cc b/src/d8-console.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..204bbd7e052a1efeb63f3b88af9c489a2ba91e80 |
| --- /dev/null |
| +++ b/src/d8-console.cc |
| @@ -0,0 +1,117 @@ |
| +// Copyright 2017 the V8 project authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "src/d8-console.h" |
| +#include "src/d8.h" |
| + |
| +namespace v8 { |
| + |
| +namespace { |
| +static const char* kConsoleDefaultColor = ""; |
| +static const char* kConsoleRed = "\x1b[31m"; |
| +static const char* kConsoleCyan = "\x1b[36m"; |
| +static const char* kConsoleColorReset = "\x1b[0m"; |
|
rongjie
2017/04/24 11:35:56
Note that Windows does not support ANSI color, the
|
| + |
| +void WriteToFile(FILE* file, Isolate* isolate, const char* color, |
| + const debug::ConsoleCallArguments& args) { |
| + fprintf(file, "%s", color); |
| + for (int i = 0; i < args.Length(); i++) { |
| + HandleScope handle_scope(isolate); |
| + if (i != 0) fprintf(file, " "); |
| + |
| + // Explicitly catch potential exceptions in toString(). |
| + v8::TryCatch try_catch(isolate); |
| + Local<Value> arg = args[i]; |
| + Local<String> str_obj; |
| + |
| + if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Name(); |
| + if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj)) { |
| + try_catch.ReThrow(); |
| + return; |
| + } |
| + |
| + v8::String::Utf8Value str(str_obj); |
| + int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file)); |
| + if (n != str.length()) { |
| + printf("Error in fwrite\n"); |
| + Shell::Exit(1); |
| + } |
| + } |
| + fprintf(file, "%s\n", kConsoleColorReset); |
| +} |
| +} // anonymous namespace |
| + |
| +D8Console::D8Console(Isolate* isolate) : isolate_(isolate) { |
| + default_timer_ = base::TimeTicks::HighResolutionNow(); |
| +} |
| + |
| +void D8Console::Log(const debug::ConsoleCallArguments& args) { |
| + WriteToFile(stdout, isolate_, kConsoleDefaultColor, args); |
| +} |
| + |
| +void D8Console::Error(const debug::ConsoleCallArguments& args) { |
| + WriteToFile(stderr, isolate_, kConsoleRed, args); |
| +} |
| + |
| +void D8Console::Warn(const debug::ConsoleCallArguments& args) { |
| + WriteToFile(stdout, isolate_, kConsoleRed, args); |
| +} |
| + |
| +void D8Console::Info(const debug::ConsoleCallArguments& args) { |
| + WriteToFile(stdout, isolate_, kConsoleCyan, args); |
| +} |
| + |
| +void D8Console::Debug(const debug::ConsoleCallArguments& args) { |
| + WriteToFile(stdout, isolate_, kConsoleDefaultColor, args); |
| +} |
| + |
| +void D8Console::Time(const debug::ConsoleCallArguments& args) { |
| + if (args.Length() == 0) { |
| + default_timer_ = base::TimeTicks::HighResolutionNow(); |
| + } else { |
| + Local<Value> arg = args[0]; |
| + Local<String> label; |
| + v8::TryCatch try_catch(isolate_); |
| + if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) { |
| + try_catch.ReThrow(); |
| + return; |
| + } |
| + v8::String::Utf8Value utf8(label); |
| + std::string string(*utf8); |
| + auto find = timers_.find(string); |
| + if (find != timers_.end()) { |
| + find->second = base::TimeTicks::HighResolutionNow(); |
| + } else { |
| + timers_.insert(std::pair<std::string, base::TimeTicks>( |
| + string, base::TimeTicks::HighResolutionNow())); |
| + } |
| + } |
| +} |
| + |
| +void D8Console::TimeEnd(const debug::ConsoleCallArguments& args) { |
| + base::TimeDelta delta; |
| + base::TimeTicks now = base::TimeTicks::HighResolutionNow(); |
| + if (args.Length() == 0) { |
| + delta = base::TimeTicks::HighResolutionNow() - default_timer_; |
| + printf("default: "); |
| + } else { |
| + Local<Value> arg = args[0]; |
| + Local<String> label; |
| + v8::TryCatch try_catch(isolate_); |
| + if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) { |
| + try_catch.ReThrow(); |
| + return; |
| + } |
| + v8::String::Utf8Value utf8(label); |
| + std::string string(*utf8); |
| + auto find = timers_.find(string); |
| + if (find != timers_.end()) { |
| + delta = now - find->second; |
| + } |
| + printf("%s: ", *utf8); |
| + } |
| + printf("%f\n", delta.InMillisecondsF()); |
| +} |
| + |
| +} // namespace v8 |