log-utils.h 3.8 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 "allocation.h"

10 11 12
namespace v8 {
namespace internal {

13 14
class Logger;

15
// Functions and data for performing output of log messages.
16
class Log {
17
 public:
18
  // Performs process-wide initialization.
19
  void Initialize(const char* log_file_name);
20

21
  // Disables logging, but preserves acquired resources.
22
  void stop() { is_stopped_ = true; }
23

24
  static bool InitLogAtStart() {
25 26 27 28
    return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc
        || FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp
        || FLAG_ll_prof || FLAG_perf_basic_prof || FLAG_perf_jit_prof
        || FLAG_log_internal_timer_events;
29 30
  }

31
  // Frees all resources acquired in Initialize and Open... functions.
32 33 34
  // When a temporary file is used for the log, returns its stream descriptor,
  // leaving the file open.
  FILE* Close();
35 36

  // Returns whether logging is enabled.
37
  bool IsEnabled() {
38
    return !is_stopped_ && output_handle_ != NULL;
39 40
  }

41
  // Size of buffer used for formatting log messages.
42 43 44 45
  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.
46
  static const char* const kLogToTemporaryFile;
47
  static const char* const kLogToConsole;
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  // 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.
    void Append(const char* format, ...);

    // Append string data to the log message.
    void AppendVA(const char* format, va_list args);

    // 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_;
88
    LockGuard<Mutex> lock_guard_;
89 90 91
    int pos_;
  };

92
 private:
93 94 95 96
  explicit Log(Logger* logger);

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

98 99
  // Opens file for logging.
  void OpenFile(const char* name);
100

101 102
  // Opens a temporary file for logging.
  void OpenTemporaryFile();
103 104

  // Implementation of writing to a log file.
105
  int WriteToFile(const char* msg, int length) {
106
    ASSERT(output_handle_ != NULL);
107 108 109
    size_t rv = fwrite(msg, 1, length, output_handle_);
    ASSERT(static_cast<size_t>(length) == rv);
    USE(rv);
110
    fflush(output_handle_);
111
    return length;
112 113
  }

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

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

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

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

  Logger* logger_;
130

131
  friend class Logger;
132 133 134 135 136 137
};


} }  // namespace v8::internal

#endif  // V8_LOG_UTILS_H_