1// Copyright 2006-2009 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_LOG_UTILS_H_
6#define V8_LOG_UTILS_H_
7
8#include <stdio.h>
9
10#include <cstdarg>
11
12#include "src/allocation.h"
13#include "src/base/compiler-specific.h"
14#include "src/base/platform/mutex.h"
15#include "src/flags.h"
16
17namespace v8 {
18namespace internal {
19
20class Logger;
21
22// Functions and data for performing output of log messages.
23class 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() {
32    return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
33           FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp ||
34           FLAG_ll_prof || FLAG_perf_basic_prof || FLAG_perf_prof ||
35           FLAG_log_internal_timer_events || FLAG_prof_cpp;
36  }
37
38  // Frees all resources acquired in Initialize and Open... functions.
39  // When a temporary file is used for the log, returns its stream descriptor,
40  // leaving the file open.
41  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  static const int kMessageBufferSize = 2048;
50
51  // This mode is only used in tests, as temporary files are automatically
52  // deleted on close and thus can't be accessed afterwards.
53  static const char* const kLogToTemporaryFile;
54  static const char* const kLogToConsole;
55
56  // Utility class for formatting log messages. It fills the message into the
57  // static buffer in Log.
58  class MessageBuilder BASE_EMBEDDED {
59   public:
60    // Create a message builder starting from position 0.
61    // This acquires the mutex in the log as well.
62    explicit MessageBuilder(Log* log);
63    ~MessageBuilder() { }
64
65    // Append string data to the log message.
66    void PRINTF_FORMAT(2, 3) Append(const char* format, ...);
67
68    // Append string data to the log message.
69    void PRINTF_FORMAT(2, 0) AppendVA(const char* format, va_list args);
70
71    // Append a character to the log message.
72    void Append(const char c);
73
74    // Append double quoted string to the log message.
75    void AppendDoubleQuotedString(const char* string);
76
77    // Append a heap string.
78    void Append(String* str);
79
80    // Appends an address.
81    void AppendAddress(Address addr);
82
83    void AppendSymbolName(Symbol* symbol);
84
85    void AppendDetailed(String* str, bool show_impl_info);
86
87    // Append a portion of a string.
88    void AppendStringPart(const char* str, int len);
89
90    // Write the log message to the log file currently opened.
91    void WriteToLogFile();
92
93   private:
94    Log* log_;
95    base::LockGuard<base::Mutex> lock_guard_;
96    int pos_;
97  };
98
99 private:
100  explicit Log(Logger* logger);
101
102  // Opens stdout for logging.
103  void OpenStdout();
104
105  // Opens file for logging.
106  void OpenFile(const char* name);
107
108  // Opens a temporary file for logging.
109  void OpenTemporaryFile();
110
111  // Implementation of writing to a log file.
112  int WriteToFile(const char* msg, int length) {
113    DCHECK(output_handle_ != NULL);
114    size_t rv = fwrite(msg, 1, length, output_handle_);
115    DCHECK(static_cast<size_t>(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  // When logging is active output_handle_ is used to store a pointer to log
125  // destination.  mutex_ should be acquired before using output_handle_.
126  FILE* output_handle_;
127
128  // mutex_ is a Mutex used for enforcing exclusive
129  // access to the formatting buffer and the log file or log memory buffer.
130  base::Mutex mutex_;
131
132  // Buffer used for formatting log messages. This is a singleton buffer and
133  // mutex_ should be acquired before using it.
134  char* message_buffer_;
135
136  Logger* logger_;
137
138  friend class Logger;
139};
140
141
142}  // namespace internal
143}  // namespace v8
144
145#endif  // V8_LOG_UTILS_H_
146