log-utils.h 4.02 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 34
           FLAG_log_handles || FLAG_log_suspect || FLAG_ll_prof ||
           FLAG_perf_basic_prof || FLAG_perf_prof ||
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 90 91 92 93 94

    // 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);

    // Write the log message to the log file currently opened.
    void WriteToLogFile();

   private:
    Log* log_;
95
    base::LockGuard<base::Mutex> lock_guard_;
96 97 98
    int pos_;
  };

99
 private:
100 101 102 103
  explicit Log(Logger* logger);

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

105 106
  // Opens file for logging.
  void OpenFile(const char* name);
107

108 109
  // Opens a temporary file for logging.
  void OpenTemporaryFile();
110 111

  // Implementation of writing to a log file.
112
  int WriteToFile(const char* msg, int length) {
113
    DCHECK_NOT_NULL(output_handle_);
114
    size_t rv = fwrite(msg, 1, length, output_handle_);
115
    DCHECK_EQ(length, rv);
116
    USE(rv);
117
    fflush(output_handle_);
118
    return length;
119 120
  }

121
  // Whether logging is stopped (e.g. due to insufficient resources).
122
  bool is_stopped_;
123

124 125
  // When logging is active output_handle_ is used to store a pointer to log
  // destination.  mutex_ should be acquired before using output_handle_.
126
  FILE* output_handle_;
127 128 129

  // mutex_ is a Mutex used for enforcing exclusive
  // access to the formatting buffer and the log file or log memory buffer.
130
  base::Mutex mutex_;
131 132 133

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

  Logger* logger_;
137

138
  friend class Logger;
139 140 141
};


142 143
}  // namespace internal
}  // namespace v8
144 145

#endif  // V8_LOG_UTILS_H_