log-utils.h 4.83 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
#ifndef V8_LOGGING_LOG_UTILS_H_
#define V8_LOGGING_LOG_UTILS_H_
7

8 9
#include <stdio.h>

10
#include <atomic>
11
#include <cstdarg>
12
#include <memory>
13 14

#include "src/base/compiler-specific.h"
15
#include "src/base/optional.h"
16
#include "src/base/platform/mutex.h"
17
#include "src/common/assert-scope.h"
18
#include "src/flags/flags.h"
19 20
#include "src/utils/allocation.h"
#include "src/utils/ostreams.h"
21

22 23
namespace v8 {

24
namespace base {
25 26
template <typename T>
class Vector;
27 28 29 30 31
}  // namespace base

namespace internal {

class Logger;
32

33 34
enum class LogSeparator { kSeparator };

35
// Functions and data for performing output of log messages.
36
class Log {
37
 public:
38
  explicit Log(Logger* logger, std::string log_file_name);
39

40 41 42
  V8_EXPORT_PRIVATE static bool IsLoggingToConsole(std::string file_name);
  V8_EXPORT_PRIVATE static bool IsLoggingToTemporaryFile(std::string file_name);

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 49
  std::string file_name() const;

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

58 59
  // Utility class for formatting log messages. It escapes the given messages
  // and then appends them to the static buffer in Log.
60
  class MessageBuilder {
61
   public:
62
    ~MessageBuilder() = default;
63

64
    void AppendString(String str,
65
                      base::Optional<int> length_limit = base::nullopt);
66
    void AppendString(base::Vector<const char> str);
67
    void AppendString(const char* str);
68
    void AppendString(const char* str, size_t length, bool is_one_byte = true);
69 70
    void PRINTF_FORMAT(2, 3) AppendFormatString(const char* format, ...);
    void AppendCharacter(char c);
71
    void AppendTwoByteCharacter(char c1, char c2);
72
    void AppendSymbolName(Symbol symbol);
73

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

82
    // Finish the current log line an flush the it to the log file.
83 84 85
    void WriteToLogFile();

   private:
86 87 88 89
    // Create a message builder starting from position 0.
    // This acquires the mutex in the log as well.
    explicit MessageBuilder(Log* log);

90 91 92 93 94
    // 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);

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

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

100
    Log* log_;
101
    NoGarbageCollectionMutexGuard lock_guard_;
102 103

    friend class Log;
104 105
  };

106 107 108 109
  // Use this method to create an instance of Log::MessageBuilder. This method
  // will return null if logging is disabled.
  std::unique_ptr<Log::MessageBuilder> NewMessageBuilder();

110
 private:
111
  static FILE* CreateOutputHandle(std::string file_name);
112
  base::Mutex* mutex() { return &mutex_; }
113 114

  void WriteLogHeader();
115

116 117
  Logger* logger_;

118
  std::string file_name_;
119

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

124
  OFStream os_;
125

126 127 128 129
  // mutex_ is a Mutex used for enforcing exclusive
  // access to the formatting buffer and the log file or log memory buffer.
  base::Mutex mutex_;

130 131
  // Buffer used for formatting log messages. This is a singleton buffer and
  // mutex_ should be acquired before using it.
132
  std::unique_ptr<char[]> format_buffer_;
133 134

  friend class Logger;
135 136
};

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

154 155
}  // namespace internal
}  // namespace v8
156

157
#endif  // V8_LOGGING_LOG_UTILS_H_