blob: f96c5ea28286b37931f26b6dd8d6a049e6d7f51c [file] [log] [blame]
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "vm/allocation.h"
#include "vm/growable_array.h"
#include "vm/os.h"
namespace dart {
class IsolateGroup;
class LogBlock;
#if defined(_MSC_VER)
#define THR_Print(format, ...) Log::Current()->Print(format, __VA_ARGS__)
#define THR_Print(format, ...) Log::Current()->Print(format, ##__VA_ARGS__)
#define THR_VPrint(format, args) Log::Current()->VPrint(format, args)
typedef void (*LogPrinter)(const char* str, ...) PRINTF_ATTRIBUTE(1, 2);
class Log {
explicit Log(LogPrinter printer = OS::PrintErr);
static Log* Current();
// Append a formatted string to the log.
void Print(const char* format, ...) PRINTF_ATTRIBUTE(2, 3);
void VPrint(const char* format, va_list args);
// Flush and truncate the log. The log is flushed starting at cursor
// and truncated to cursor afterwards.
void Flush(const intptr_t cursor = 0);
// Clears the log.
void Clear();
// Current cursor.
intptr_t cursor() const;
// A logger that does nothing.
static Log* NoOpLog();
void TerminateString();
void EnableManualFlush();
void DisableManualFlush(const intptr_t cursor);
// Returns true when flush is required.
bool ShouldFlush() const;
// Returns false if we should drop log messages related to 'isolate'.
static bool ShouldLogForIsolateGroup(const IsolateGroup* isolate);
static Log noop_log_;
LogPrinter printer_;
intptr_t manual_flush_;
MallocGrowableArray<char> buffer_;
friend class LogBlock;
friend class LogTestHelper;
// Causes all log messages to be buffered until destructor is called.
// Can be nested.
class LogBlock : public StackResource {
LogBlock(ThreadState* thread, Log* log)
: StackResource(thread), log_(log), cursor_(log->cursor()) {
: StackResource(ThreadState::Current()),
cursor_(Log::Current()->cursor()) {
void Initialize();
Log* const log_;
const intptr_t cursor_;
} // namespace dart
#endif // RUNTIME_VM_LOG_H_