log-utils.h 4.3 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

17 18 19
namespace v8 {
namespace internal {

20 21
class Logger;

22
// Functions and data for performing output of log messages.
23
class Log {
24
 public:
25
  // Performs process-wide initialization.
26
  void Initialize(const char* log_file_name);
27

28
  // Disables logging, but preserves acquired resources.
29
  void stop() { is_stopped_ = true; }
30

31
  static bool InitLogAtStart() {
jkummerow's avatar
jkummerow committed
32
    return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
yangguo's avatar
yangguo committed
33
           FLAG_log_handles || FLAG_log_suspect || FLAG_ll_prof ||
34
           FLAG_perf_basic_prof || FLAG_perf_prof || FLAG_log_source_code ||
35
           FLAG_log_internal_timer_events || FLAG_prof_cpp || FLAG_trace_ic;
36 37
  }

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

  // Returns whether logging is enabled.
44
  bool IsEnabled() {
45
    return !is_stopped_ && output_handle_ != NULL;
46 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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    // Append a character to the log message.
    void Append(const char c);

    // Append double quoted string to the log message.
    void AppendDoubleQuotedString(const char* string);

    // Append a heap string.
    void Append(String* str);

    // Appends an address.
    void AppendAddress(Address addr);

    void AppendSymbolName(Symbol* symbol);

    void AppendDetailed(String* str, bool show_impl_info);

    // Append a portion of a string.
    void AppendStringPart(const char* str, int len);

90 91 92 93 94 95 96
    // Helpers for appending char, C-string and heap string without
    // buffering. This is useful for entries that can exceed the 2kB
    // limit.
    void AppendUnbufferedChar(char c);
    void AppendUnbufferedCString(const char* str);
    void AppendUnbufferedHeapString(String* source);

97 98 99 100 101
    // Write the log message to the log file currently opened.
    void WriteToLogFile();

   private:
    Log* log_;
102
    base::LockGuard<base::Mutex> lock_guard_;
103 104 105
    int pos_;
  };

106
 private:
107 108 109 110
  explicit Log(Logger* logger);

  // Opens stdout for logging.
  void OpenStdout();
111

112 113
  // Opens file for logging.
  void OpenFile(const char* name);
114

115 116
  // Opens a temporary file for logging.
  void OpenTemporaryFile();
117 118

  // Implementation of writing to a log file.
119
  int WriteToFile(const char* msg, int length) {
120
    DCHECK_NOT_NULL(output_handle_);
121
    size_t rv = fwrite(msg, 1, length, output_handle_);
122
    DCHECK_EQ(length, rv);
123 124
    USE(rv);
    return length;
125 126
  }

127
  // Whether logging is stopped (e.g. due to insufficient resources).
128
  bool is_stopped_;
129

130 131
  // When logging is active output_handle_ is used to store a pointer to log
  // destination.  mutex_ should be acquired before using output_handle_.
132
  FILE* output_handle_;
133 134 135

  // mutex_ is a Mutex used for enforcing exclusive
  // access to the formatting buffer and the log file or log memory buffer.
136
  base::Mutex mutex_;
137 138 139

  // Buffer used for formatting log messages. This is a singleton buffer and
  // mutex_ should be acquired before using it.
140 141 142
  char* message_buffer_;

  Logger* logger_;
143

144
  friend class Logger;
145 146 147
};


148 149
}  // namespace internal
}  // namespace v8
150 151

#endif  // V8_LOG_UTILS_H_