log-utils.h 4.66 KB
Newer Older
1
// Copyright 2006-2009 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4 5 6 7

#ifndef V8_LOG_UTILS_H_
#define V8_LOG_UTILS_H_

8 9
#include <stdio.h>

10 11
#include <cstdarg>

12
#include "src/allocation.h"
jfb's avatar
jfb committed
13
#include "src/base/compiler-specific.h"
14 15
#include "src/base/platform/mutex.h"
#include "src/flags.h"
16
#include "src/ostreams.h"
17

18 19 20
namespace v8 {
namespace internal {

21 22
class Logger;

23 24
enum class LogSeparator { kSeparator };

25
// Functions and data for performing output of log messages.
26
class Log {
27
 public:
28
  Log(Logger* log, const char* log_file_name);
29
  // Disables logging, but preserves acquired resources.
30
  void stop() { is_stopped_ = true; }
31

32
  static bool InitLogAtStart() {
Hannes Payer's avatar
Hannes Payer committed
33 34 35
    return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_handles ||
           FLAG_log_suspect || FLAG_ll_prof || FLAG_perf_basic_prof ||
           FLAG_perf_prof || FLAG_log_source_code ||
36 37
           FLAG_log_internal_timer_events || FLAG_prof_cpp || FLAG_trace_ic ||
           FLAG_log_function_events;
38 39
  }

40
  // Frees all resources acquired in Initialize and Open... functions.
41 42 43
  // When a temporary file is used for the log, returns its stream descriptor,
  // leaving the file open.
  FILE* Close();
44 45

  // Returns whether logging is enabled.
46
  bool IsEnabled() { return !is_stopped_ && output_handle_ != nullptr; }
47

48
  // Size of buffer used for formatting log messages.
49 50 51 52
  static const int kMessageBufferSize = 2048;

  // This mode is only used in tests, as temporary files are automatically
  // deleted on close and thus can't be accessed afterwards.
53
  static const char* const kLogToTemporaryFile;
54
  static const char* const kLogToConsole;
55

56 57 58 59 60 61 62 63 64 65
  // Utility class for formatting log messages. It fills the message into the
  // static buffer in Log.
  class MessageBuilder BASE_EMBEDDED {
   public:
    // Create a message builder starting from position 0.
    // This acquires the mutex in the log as well.
    explicit MessageBuilder(Log* log);
    ~MessageBuilder() { }

    // Append string data to the log message.
jfb's avatar
jfb committed
66
    void PRINTF_FORMAT(2, 3) Append(const char* format, ...);
67 68

    // Append string data to the log message.
jfb's avatar
jfb committed
69
    void PRINTF_FORMAT(2, 0) AppendVA(const char* format, va_list args);
70 71 72 73 74

    void AppendSymbolName(Symbol* symbol);

    void AppendDetailed(String* str, bool show_impl_info);

75 76 77 78 79 80
    // Append and escape a full string.
    void AppendString(String* source);
    void AppendString(const char* string);

    // Append and escpae a portion of a string.
    void AppendStringPart(String* source, int len);
81
    void AppendStringPart(const char* str, size_t len);
82

83
    void AppendCharacter(const char character);
84 85

    // Delegate insertion to the underlying {log_}.
86
    // All appened srings are escaped to maintain one-line log entries.
87 88 89 90 91
    template <typename T>
    MessageBuilder& operator<<(T value) {
      log_->os_ << value;
      return *this;
    }
92

93
    // Finish the current log line an flush the it to the log file.
94 95 96 97
    void WriteToLogFile();

   private:
    Log* log_;
98
    base::LockGuard<base::Mutex> lock_guard_;
99 100
  };

101
 private:
102
  static FILE* CreateOutputHandle(const char* file_name);
103 104

  // Implementation of writing to a log file.
105
  int WriteToFile(const char* msg, int length) {
106
    DCHECK_NOT_NULL(output_handle_);
107 108
    os_.write(msg, length);
    DCHECK(!os_.bad());
109
    return length;
110 111
  }

112
  // Whether logging is stopped (e.g. due to insufficient resources).
113
  bool is_stopped_;
114

115 116
  // When logging is active output_handle_ is used to store a pointer to log
  // destination.  mutex_ should be acquired before using output_handle_.
117
  FILE* output_handle_;
118
  OFStream os_;
119 120 121

  // mutex_ is a Mutex used for enforcing exclusive
  // access to the formatting buffer and the log file or log memory buffer.
122
  base::Mutex mutex_;
123 124 125

  // Buffer used for formatting log messages. This is a singleton buffer and
  // mutex_ should be acquired before using it.
126
  char* format_buffer_;
127 128

  Logger* logger_;
129

130
  friend class Logger;
131 132
};

133 134 135 136
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<LogSeparator>(
    LogSeparator separator);
template <>
137 138
Log::MessageBuilder& Log::MessageBuilder::operator<<<void*>(void* pointer);
template <>
139 140 141 142 143 144 145 146 147 148
Log::MessageBuilder& Log::MessageBuilder::operator<<<const char*>(
    const char* string);
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<char>(char c);
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<String*>(String* string);
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<Symbol*>(Symbol* symbol);
template <>
Log::MessageBuilder& Log::MessageBuilder::operator<<<Name*>(Name* name);
149

150 151
}  // namespace internal
}  // namespace v8
152 153

#endif  // V8_LOG_UTILS_H_