OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include "vm/log.h" |
| 6 |
| 7 #include "vm/flags.h" |
| 8 #include "vm/thread.h" |
| 9 |
| 10 namespace dart { |
| 11 |
| 12 DEFINE_FLAG(bool, force_log_flush, false, "Always flush log messages."); |
| 13 |
| 14 Log::Log(LogPrinter printer) |
| 15 : printer_(printer), |
| 16 manual_flush_(0), |
| 17 buffer_(0) { |
| 18 } |
| 19 |
| 20 |
| 21 void Log::Print(const char* format, ...) { |
| 22 if (this == NoOpLog()) { |
| 23 return; |
| 24 } |
| 25 // Measure. |
| 26 va_list args; |
| 27 va_start(args, format); |
| 28 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 29 va_end(args); |
| 30 |
| 31 // Print string to buffer. |
| 32 char* buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 33 va_list args2; |
| 34 va_start(args2, format); |
| 35 OS::VSNPrint(buffer, (len + 1), format, args2); |
| 36 va_end(args2); |
| 37 |
| 38 // Does not append the '\0' character. |
| 39 for (intptr_t i = 0; i < len; i++) { |
| 40 buffer_.Add(buffer[i]); |
| 41 } |
| 42 free(buffer); |
| 43 if ((manual_flush_ == 0) || FLAG_force_log_flush) { |
| 44 Flush(); |
| 45 } |
| 46 } |
| 47 |
| 48 |
| 49 void Log::Flush(const intptr_t cursor) { |
| 50 if (this == NoOpLog()) { |
| 51 return; |
| 52 } |
| 53 if (buffer_.is_empty()) { |
| 54 return; |
| 55 } |
| 56 if (buffer_.length() <= cursor) { |
| 57 return; |
| 58 } |
| 59 TerminateString(); |
| 60 const char* str = &buffer_[cursor]; |
| 61 ASSERT(str != NULL); |
| 62 printer_(str); |
| 63 buffer_.TruncateTo(cursor); |
| 64 } |
| 65 |
| 66 |
| 67 void Log::Clear() { |
| 68 if (this == NoOpLog()) { |
| 69 return; |
| 70 } |
| 71 buffer_.TruncateTo(0); |
| 72 } |
| 73 |
| 74 |
| 75 intptr_t Log::cursor() const { |
| 76 return buffer_.length(); |
| 77 } |
| 78 |
| 79 |
| 80 Log Log::noop_log_; |
| 81 Log* Log::NoOpLog() { |
| 82 return &noop_log_; |
| 83 } |
| 84 |
| 85 |
| 86 void Log::TerminateString() { |
| 87 buffer_.Add('\0'); |
| 88 } |
| 89 |
| 90 |
| 91 void Log::EnableManualFlush() { |
| 92 manual_flush_++; |
| 93 } |
| 94 |
| 95 |
| 96 void Log::DisableManualFlush() { |
| 97 manual_flush_--; |
| 98 ASSERT(manual_flush_ >= 0); |
| 99 if (manual_flush_ == 0) { |
| 100 Flush(); |
| 101 } |
| 102 } |
| 103 |
| 104 |
| 105 LogBlock::LogBlock(Thread* thread, Log* log) |
| 106 : StackResource(thread->isolate()), |
| 107 log_(log), cursor_(log->cursor()) { |
| 108 CommonConstructor(); |
| 109 } |
| 110 |
| 111 |
| 112 LogBlock::LogBlock(Isolate* isolate) |
| 113 : StackResource(isolate), |
| 114 log_(isolate->Log()), cursor_(isolate->Log()->cursor()) { |
| 115 CommonConstructor(); |
| 116 } |
| 117 |
| 118 |
| 119 LogBlock::LogBlock(Thread* thread) |
| 120 : StackResource(thread->isolate()), |
| 121 log_(thread->isolate()->Log()), |
| 122 cursor_(thread->isolate()->Log()->cursor()) { |
| 123 CommonConstructor(); |
| 124 } |
| 125 |
| 126 } // namespace dart |
OLD | NEW |