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 |
| 9 namespace dart { |
| 10 |
| 11 DEFINE_FLAG(bool, force_log_flush, false, "Always flush log messages."); |
| 12 |
| 13 Log::Log(LogPrinter printer) |
| 14 : printer_(printer), |
| 15 manual_flush_(0), |
| 16 buffer_(0) { |
| 17 } |
| 18 |
| 19 |
| 20 Log::~Log() { |
| 21 } |
| 22 |
| 23 |
| 24 void Log::Print(const char* format, ...) { |
| 25 if (this == NoOpLog()) { |
| 26 return; |
| 27 } |
| 28 // Measure. |
| 29 va_list args; |
| 30 va_start(args, format); |
| 31 intptr_t len = OS::VSNPrint(NULL, 0, format, args); |
| 32 va_end(args); |
| 33 |
| 34 // Print string to buffer. |
| 35 char* buffer = reinterpret_cast<char*>(malloc(len + 1)); |
| 36 va_list args2; |
| 37 va_start(args2, format); |
| 38 OS::VSNPrint(buffer, (len + 1), format, args2); |
| 39 va_end(args2); |
| 40 |
| 41 // Append. |
| 42 Append(buffer); |
| 43 free(buffer); |
| 44 } |
| 45 |
| 46 |
| 47 void Log::Append(const char* str) { |
| 48 if (this == NoOpLog()) { |
| 49 return; |
| 50 } |
| 51 ASSERT(str != NULL); |
| 52 intptr_t len = strlen(str); |
| 53 // Does not append the '\0' character. |
| 54 for (intptr_t i = 0; i < len; i++) { |
| 55 buffer_.Add(str[i]); |
| 56 } |
| 57 if ((manual_flush_ == 0) || FLAG_force_log_flush) { |
| 58 Flush(); |
| 59 } |
| 60 } |
| 61 |
| 62 |
| 63 void Log::Flush(const intptr_t cursor) { |
| 64 if (this == NoOpLog()) { |
| 65 return; |
| 66 } |
| 67 if (buffer_.is_empty()) { |
| 68 return; |
| 69 } |
| 70 if (buffer_.length() <= cursor) { |
| 71 return; |
| 72 } |
| 73 TerminateString(); |
| 74 const char* str = &buffer_[cursor]; |
| 75 ASSERT(str != NULL); |
| 76 printer_(str); |
| 77 buffer_.TruncateTo(cursor); |
| 78 } |
| 79 |
| 80 |
| 81 void Log::Clear() { |
| 82 if (this == NoOpLog()) { |
| 83 return; |
| 84 } |
| 85 buffer_.TruncateTo(0); |
| 86 } |
| 87 |
| 88 |
| 89 intptr_t Log::cursor() const { |
| 90 return buffer_.length(); |
| 91 } |
| 92 |
| 93 |
| 94 Log Log::noop_log_; |
| 95 Log* Log::NoOpLog() { |
| 96 return &noop_log_; |
| 97 } |
| 98 |
| 99 |
| 100 void Log::TerminateString() { |
| 101 buffer_.Add('\0'); |
| 102 } |
| 103 |
| 104 |
| 105 void Log::EnableManualFlush() { |
| 106 manual_flush_++; |
| 107 } |
| 108 |
| 109 |
| 110 void Log::DisableManualFlush() { |
| 111 manual_flush_--; |
| 112 ASSERT(manual_flush_ >= 0); |
| 113 if (manual_flush_ == 0) { |
| 114 Flush(); |
| 115 } |
| 116 } |
| 117 |
| 118 } // namespace dart |
OLD | NEW |