87dab52406338facb3fe484bfb7f12ee38e3fc08
[platform/upstream/v8.git] / src / log-utils.h
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 "src/allocation.h"
9 #include "src/base/platform/mutex.h"
10 #include "src/flags.h"
11
12 namespace v8 {
13 namespace internal {
14
15 class Logger;
16
17 // Functions and data for performing output of log messages.
18 class Log {
19  public:
20   // Performs process-wide initialization.
21   void Initialize(const char* log_file_name);
22
23   // Disables logging, but preserves acquired resources.
24   void stop() { is_stopped_ = true; }
25
26   static bool InitLogAtStart() {
27     return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
28            FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp ||
29            FLAG_ll_prof || FLAG_perf_basic_prof ||
30            FLAG_log_internal_timer_events || FLAG_prof_cpp;
31   }
32
33   // Frees all resources acquired in Initialize and Open... functions.
34   // When a temporary file is used for the log, returns its stream descriptor,
35   // leaving the file open.
36   FILE* Close();
37
38   // Returns whether logging is enabled.
39   bool IsEnabled() {
40     return !is_stopped_ && output_handle_ != NULL;
41   }
42
43   // Size of buffer used for formatting log messages.
44   static const int kMessageBufferSize = 2048;
45
46   // This mode is only used in tests, as temporary files are automatically
47   // deleted on close and thus can't be accessed afterwards.
48   static const char* const kLogToTemporaryFile;
49   static const char* const kLogToConsole;
50
51   // Utility class for formatting log messages. It fills the message into the
52   // static buffer in Log.
53   class MessageBuilder BASE_EMBEDDED {
54    public:
55     // Create a message builder starting from position 0.
56     // This acquires the mutex in the log as well.
57     explicit MessageBuilder(Log* log);
58     ~MessageBuilder() { }
59
60     // Append string data to the log message.
61     void Append(const char* format, ...);
62
63     // Append string data to the log message.
64     void AppendVA(const char* format, va_list args);
65
66     // Append a character to the log message.
67     void Append(const char c);
68
69     // Append double quoted string to the log message.
70     void AppendDoubleQuotedString(const char* string);
71
72     // Append a heap string.
73     void Append(String* str);
74
75     // Appends an address.
76     void AppendAddress(Address addr);
77
78     void AppendSymbolName(Symbol* symbol);
79
80     void AppendDetailed(String* str, bool show_impl_info);
81
82     // Append a portion of a string.
83     void AppendStringPart(const char* str, int len);
84
85     // Write the log message to the log file currently opened.
86     void WriteToLogFile();
87
88    private:
89     Log* log_;
90     base::LockGuard<base::Mutex> lock_guard_;
91     int pos_;
92   };
93
94  private:
95   explicit Log(Logger* logger);
96
97   // Opens stdout for logging.
98   void OpenStdout();
99
100   // Opens file for logging.
101   void OpenFile(const char* name);
102
103   // Opens a temporary file for logging.
104   void OpenTemporaryFile();
105
106   // Implementation of writing to a log file.
107   int WriteToFile(const char* msg, int length) {
108     DCHECK(output_handle_ != NULL);
109     size_t rv = fwrite(msg, 1, length, output_handle_);
110     DCHECK(static_cast<size_t>(length) == rv);
111     USE(rv);
112     fflush(output_handle_);
113     return length;
114   }
115
116   // Whether logging is stopped (e.g. due to insufficient resources).
117   bool is_stopped_;
118
119   // When logging is active output_handle_ is used to store a pointer to log
120   // destination.  mutex_ should be acquired before using output_handle_.
121   FILE* output_handle_;
122
123   // mutex_ is a Mutex used for enforcing exclusive
124   // access to the formatting buffer and the log file or log memory buffer.
125   base::Mutex mutex_;
126
127   // Buffer used for formatting log messages. This is a singleton buffer and
128   // mutex_ should be acquired before using it.
129   char* message_buffer_;
130
131   Logger* logger_;
132
133   friend class Logger;
134 };
135
136
137 } }  // namespace v8::internal
138
139 #endif  // V8_LOG_UTILS_H_