| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef VM_LOG_H_ | 5 #ifndef VM_LOG_H_ |
| 6 #define VM_LOG_H_ | 6 #define VM_LOG_H_ |
| 7 | 7 |
| 8 #include "vm/allocation.h" | 8 #include "vm/allocation.h" |
| 9 #include "vm/growable_array.h" | 9 #include "vm/growable_array.h" |
| 10 #include "vm/os.h" | 10 #include "vm/os.h" |
| 11 | 11 |
| 12 namespace dart { | 12 namespace dart { |
| 13 | 13 |
| 14 class Isolate; | |
| 15 class LogBlock; | 14 class LogBlock; |
| 16 class Thread; | 15 class Thread; |
| 17 | 16 |
| 18 #if defined(_MSC_VER) | 17 #if defined(_MSC_VER) |
| 19 #define ISL_Print(format, ...) \ | 18 #define THR_Print(format, ...) \ |
| 20 Isolate::Current()->Log()->Print(format, __VA_ARGS__) | 19 Log::Current()->Print(format, __VA_ARGS__) |
| 21 #else | 20 #else |
| 22 #define ISL_Print(format, ...) \ | 21 #define THR_Print(format, ...) \ |
| 23 Isolate::Current()->Log()->Print(format, ##__VA_ARGS__) | 22 Log::Current()->Print(format, ##__VA_ARGS__) |
| 24 #endif | 23 #endif |
| 25 | 24 |
| 26 #define ISL_VPrint(format, args) \ | 25 #define THR_VPrint(format, args) \ |
| 27 Isolate::Current()->Log()->VPrint(format, args) | 26 Log::Current()->VPrint(format, args) |
| 28 | 27 |
| 29 typedef void (*LogPrinter)(const char* str, ...); | 28 typedef void (*LogPrinter)(const char* str, ...); |
| 30 | 29 |
| 31 class Log { | 30 class Log { |
| 32 public: | 31 public: |
| 33 explicit Log(LogPrinter printer = OS::Print); | 32 explicit Log(LogPrinter printer = OS::Print); |
| 33 ~Log(); |
| 34 |
| 35 static Log* Current(); |
| 34 | 36 |
| 35 // Append a formatted string to the log. | 37 // Append a formatted string to the log. |
| 36 void Print(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); | 38 void Print(const char* format, ...) PRINTF_ATTRIBUTE(2, 3); |
| 37 | 39 |
| 38 void VPrint(const char* format, va_list args); | 40 void VPrint(const char* format, va_list args); |
| 39 | 41 |
| 40 // Flush and truncate the log. The log is flushed starting at cursor | 42 // Flush and truncate the log. The log is flushed starting at cursor |
| 41 // and truncated to cursor afterwards. | 43 // and truncated to cursor afterwards. |
| 42 void Flush(const intptr_t cursor = 0); | 44 void Flush(const intptr_t cursor = 0); |
| 43 | 45 |
| 44 // Clears the log. | 46 // Clears the log. |
| 45 void Clear(); | 47 void Clear(); |
| 46 | 48 |
| 47 // Current cursor. | 49 // Current cursor. |
| 48 intptr_t cursor() const; | 50 intptr_t cursor() const; |
| 49 | 51 |
| 50 // A logger that does nothing. | 52 // A logger that does nothing. |
| 51 static Log* NoOpLog(); | 53 static Log* NoOpLog(); |
| 52 | 54 |
| 53 private: | 55 private: |
| 54 void TerminateString(); | 56 void TerminateString(); |
| 55 void EnableManualFlush(); | 57 void EnableManualFlush(); |
| 56 void DisableManualFlush(); | 58 void DisableManualFlush(); |
| 57 | 59 |
| 60 // Returns false if we should drop log messages related to 'isolate'. |
| 61 static bool ShouldLogForIsolate(const Isolate* isolate); |
| 62 |
| 58 static Log noop_log_; | 63 static Log noop_log_; |
| 59 LogPrinter printer_; | 64 LogPrinter printer_; |
| 60 intptr_t manual_flush_; | 65 intptr_t manual_flush_; |
| 61 MallocGrowableArray<char> buffer_; | 66 MallocGrowableArray<char> buffer_; |
| 62 | 67 |
| 63 friend class LogBlock; | 68 friend class LogBlock; |
| 64 friend class LogTestHelper; | 69 friend class LogTestHelper; |
| 65 DISALLOW_COPY_AND_ASSIGN(Log); | 70 DISALLOW_COPY_AND_ASSIGN(Log); |
| 66 }; | 71 }; |
| 67 | 72 |
| 68 | 73 |
| 69 // Causes all log messages to be buffered until destructor is called. | 74 // Causes all log messages to be buffered until destructor is called. |
| 70 // Can be nested. | 75 // Can be nested. |
| 71 class LogBlock : public StackResource { | 76 class LogBlock : public StackResource { |
| 72 public: | 77 public: |
| 73 LogBlock(Isolate* isolate, Log* log) | 78 LogBlock(Thread* thread, Log* log) |
| 74 : StackResource(isolate), | 79 : StackResource(thread), log_(log), cursor_(log->cursor()) { |
| 75 log_(log), cursor_(log->cursor()) { | 80 Initialize(); |
| 76 CommonConstructor(); | |
| 77 } | 81 } |
| 78 | 82 |
| 79 explicit LogBlock(Isolate* isolate); | 83 LogBlock() |
| 80 explicit LogBlock(Thread* thread); | 84 : StackResource(Thread::Current()), |
| 81 | 85 log_(Log::Current()), |
| 82 LogBlock(Thread* thread, Log* log); | 86 cursor_(Log::Current()->cursor()) { |
| 83 | 87 Initialize(); |
| 84 ~LogBlock() { | |
| 85 CommonDestructor(); | |
| 86 } | 88 } |
| 87 | 89 |
| 90 ~LogBlock(); |
| 91 |
| 88 private: | 92 private: |
| 89 void CommonConstructor() { | 93 void Initialize(); |
| 90 log_->EnableManualFlush(); | |
| 91 } | |
| 92 | 94 |
| 93 void CommonDestructor() { | 95 Log* const log_; |
| 94 log_->Flush(cursor_); | |
| 95 log_->DisableManualFlush(); | |
| 96 } | |
| 97 Log* log_; | |
| 98 const intptr_t cursor_; | 96 const intptr_t cursor_; |
| 99 }; | 97 }; |
| 100 | 98 |
| 101 } // namespace dart | 99 } // namespace dart |
| 102 | 100 |
| 103 #endif // VM_LOG_H_ | 101 #endif // VM_LOG_H_ |
| OLD | NEW |