Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 the V8 project authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "src/d8-console.h" | |
| 6 #include "src/d8.h" | |
| 7 | |
| 8 namespace v8 { | |
| 9 | |
| 10 namespace { | |
| 11 static const char* kConsoleDefaultColor = ""; | |
| 12 static const char* kConsoleRed = "\x1b[31m"; | |
| 13 static const char* kConsoleCyan = "\x1b[36m"; | |
| 14 static const char* kConsoleColorReset = "\x1b[0m"; | |
|
rongjie
2017/04/24 11:35:56
Note that Windows does not support ANSI color, the
| |
| 15 | |
| 16 void WriteToFile(FILE* file, Isolate* isolate, const char* color, | |
| 17 const debug::ConsoleCallArguments& args) { | |
| 18 fprintf(file, "%s", color); | |
| 19 for (int i = 0; i < args.Length(); i++) { | |
| 20 HandleScope handle_scope(isolate); | |
| 21 if (i != 0) fprintf(file, " "); | |
| 22 | |
| 23 // Explicitly catch potential exceptions in toString(). | |
| 24 v8::TryCatch try_catch(isolate); | |
| 25 Local<Value> arg = args[i]; | |
| 26 Local<String> str_obj; | |
| 27 | |
| 28 if (arg->IsSymbol()) arg = Local<Symbol>::Cast(arg)->Name(); | |
| 29 if (!arg->ToString(isolate->GetCurrentContext()).ToLocal(&str_obj)) { | |
| 30 try_catch.ReThrow(); | |
| 31 return; | |
| 32 } | |
| 33 | |
| 34 v8::String::Utf8Value str(str_obj); | |
| 35 int n = static_cast<int>(fwrite(*str, sizeof(**str), str.length(), file)); | |
| 36 if (n != str.length()) { | |
| 37 printf("Error in fwrite\n"); | |
| 38 Shell::Exit(1); | |
| 39 } | |
| 40 } | |
| 41 fprintf(file, "%s\n", kConsoleColorReset); | |
| 42 } | |
| 43 } // anonymous namespace | |
| 44 | |
| 45 D8Console::D8Console(Isolate* isolate) : isolate_(isolate) { | |
| 46 default_timer_ = base::TimeTicks::HighResolutionNow(); | |
| 47 } | |
| 48 | |
| 49 void D8Console::Log(const debug::ConsoleCallArguments& args) { | |
| 50 WriteToFile(stdout, isolate_, kConsoleDefaultColor, args); | |
| 51 } | |
| 52 | |
| 53 void D8Console::Error(const debug::ConsoleCallArguments& args) { | |
| 54 WriteToFile(stderr, isolate_, kConsoleRed, args); | |
| 55 } | |
| 56 | |
| 57 void D8Console::Warn(const debug::ConsoleCallArguments& args) { | |
| 58 WriteToFile(stdout, isolate_, kConsoleRed, args); | |
| 59 } | |
| 60 | |
| 61 void D8Console::Info(const debug::ConsoleCallArguments& args) { | |
| 62 WriteToFile(stdout, isolate_, kConsoleCyan, args); | |
| 63 } | |
| 64 | |
| 65 void D8Console::Debug(const debug::ConsoleCallArguments& args) { | |
| 66 WriteToFile(stdout, isolate_, kConsoleDefaultColor, args); | |
| 67 } | |
| 68 | |
| 69 void D8Console::Time(const debug::ConsoleCallArguments& args) { | |
| 70 if (args.Length() == 0) { | |
| 71 default_timer_ = base::TimeTicks::HighResolutionNow(); | |
| 72 } else { | |
| 73 Local<Value> arg = args[0]; | |
| 74 Local<String> label; | |
| 75 v8::TryCatch try_catch(isolate_); | |
| 76 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) { | |
| 77 try_catch.ReThrow(); | |
| 78 return; | |
| 79 } | |
| 80 v8::String::Utf8Value utf8(label); | |
| 81 std::string string(*utf8); | |
| 82 auto find = timers_.find(string); | |
| 83 if (find != timers_.end()) { | |
| 84 find->second = base::TimeTicks::HighResolutionNow(); | |
| 85 } else { | |
| 86 timers_.insert(std::pair<std::string, base::TimeTicks>( | |
| 87 string, base::TimeTicks::HighResolutionNow())); | |
| 88 } | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void D8Console::TimeEnd(const debug::ConsoleCallArguments& args) { | |
| 93 base::TimeDelta delta; | |
| 94 base::TimeTicks now = base::TimeTicks::HighResolutionNow(); | |
| 95 if (args.Length() == 0) { | |
| 96 delta = base::TimeTicks::HighResolutionNow() - default_timer_; | |
| 97 printf("default: "); | |
| 98 } else { | |
| 99 Local<Value> arg = args[0]; | |
| 100 Local<String> label; | |
| 101 v8::TryCatch try_catch(isolate_); | |
| 102 if (!arg->ToString(isolate_->GetCurrentContext()).ToLocal(&label)) { | |
| 103 try_catch.ReThrow(); | |
| 104 return; | |
| 105 } | |
| 106 v8::String::Utf8Value utf8(label); | |
| 107 std::string string(*utf8); | |
| 108 auto find = timers_.find(string); | |
| 109 if (find != timers_.end()) { | |
| 110 delta = now - find->second; | |
| 111 } | |
| 112 printf("%s: ", *utf8); | |
| 113 } | |
| 114 printf("%f\n", delta.InMillisecondsF()); | |
| 115 } | |
| 116 | |
| 117 } // namespace v8 | |
| OLD | NEW |