log-utils.h 4.91 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 12 13
#include <cstdarg>

#include "src/allocation.h"
#include "src/base/compiler-specific.h"
14
#include "src/base/optional.h"
15
#include "src/base/platform/mutex.h"
16
#include "src/flags.h"
17
#include "src/ostreams.h"
18

19 20 21
namespace v8 {
namespace internal {

22
class Logger;
23 24
template <typename T>
class Vector;
25

26 27
enum class LogSeparator { kSeparator };

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

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

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

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

51
  // Size of buffer used for formatting log messages.
52 53 54 55
  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.
56
  static const char* const kLogToTemporaryFile;
57
  static const char* const kLogToConsole;
58

59 60
  // Utility class for formatting log messages. It escapes the given messages
  // and then appends them to the static buffer in Log.
61
  class MessageBuilder {
62 63 64 65
   public:
    // Create a message builder starting from position 0.
    // This acquires the mutex in the log as well.
    explicit MessageBuilder(Log* log);
66
    ~MessageBuilder() = default;
67

68
    void AppendString(String str,
69 70 71 72 73 74
                      base::Optional<int> length_limit = base::nullopt);
    void AppendString(Vector<const char> str);
    void AppendString(const char* str);
    void AppendString(const char* str, size_t length);
    void PRINTF_FORMAT(2, 3) AppendFormatString(const char* format, ...);
    void AppendCharacter(char c);
75
    void AppendSymbolName(Symbol symbol);
76

77
    // Delegate insertion to the underlying {log_}.
78
    // All appended strings are escaped to maintain one-line log entries.
79 80 81 82 83
    template <typename T>
    MessageBuilder& operator<<(T value) {
      log_->os_ << value;
      return *this;
    }
84

85
    // Finish the current log line an flush the it to the log file.
86 87 88
    void WriteToLogFile();

   private:
89 90 91 92 93
    // Prints the format string into |log_->format_buffer_|. Returns the length
    // of the result, or kMessageBufferSize if it was truncated.
    int PRINTF_FORMAT(2, 0)
        FormatStringIntoBuffer(const char* format, va_list args);

94
    void AppendSymbolNameDetails(String str, bool show_impl_info);
95 96 97 98

    void PRINTF_FORMAT(2, 3) AppendRawFormatString(const char* format, ...);
    void AppendRawCharacter(const char character);

99
    Log* log_;
100
    base::MutexGuard lock_guard_;
101 102
  };

103
 private:
104
  static FILE* CreateOutputHandle(const char* file_name);
105 106

  // Implementation of writing to a log file.
107
  int WriteToFile(const char* msg, int length) {
108
    DCHECK_NOT_NULL(output_handle_);
109 110
    os_.write(msg, length);
    DCHECK(!os_.bad());
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
  OFStream os_;
121

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

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

  Logger* logger_;
131

132
  friend class Logger;
133 134
};

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

152 153
}  // namespace internal
}  // namespace v8
154 155

#endif  // V8_LOG_UTILS_H_