Line data Source code
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 :
17 : namespace v8 {
18 : namespace internal {
19 :
20 : class Logger;
21 :
22 : // Functions and data for performing output of log messages.
23 53365 : class 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 0 : void stop() { is_stopped_ = true; }
30 :
31 109995 : static bool InitLogAtStart() {
32 109953 : return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
33 109954 : FLAG_log_handles || FLAG_log_suspect || FLAG_ll_prof ||
34 109954 : FLAG_perf_basic_prof || FLAG_perf_prof || FLAG_log_source_code ||
35 219949 : FLAG_log_internal_timer_events || FLAG_prof_cpp || FLAG_trace_ic;
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 38873 : bool IsEnabled() { return !is_stopped_ && output_handle_ != nullptr; }
45 :
46 : // Size of buffer used for formatting log messages.
47 : static const int kMessageBufferSize = 2048;
48 :
49 : // This mode is only used in tests, as temporary files are automatically
50 : // deleted on close and thus can't be accessed afterwards.
51 : static const char* const kLogToTemporaryFile;
52 : static const char* const kLogToConsole;
53 :
54 : // Utility class for formatting log messages. It fills the message into the
55 : // static buffer in Log.
56 : class MessageBuilder BASE_EMBEDDED {
57 : public:
58 : // Create a message builder starting from position 0.
59 : // This acquires the mutex in the log as well.
60 : explicit MessageBuilder(Log* log);
61 : ~MessageBuilder() { }
62 :
63 : // Append string data to the log message.
64 : void PRINTF_FORMAT(2, 3) Append(const char* format, ...);
65 :
66 : // Append string data to the log message.
67 : void PRINTF_FORMAT(2, 0) AppendVA(const char* format, va_list args);
68 :
69 : // Append a character to the log message.
70 : void Append(const char c);
71 :
72 : // Append double quoted string to the log message.
73 : void AppendDoubleQuotedString(const char* string);
74 :
75 : // Append a heap string.
76 : void Append(String* str);
77 :
78 : // Appends an address.
79 : void AppendAddress(Address addr);
80 :
81 : void AppendSymbolName(Symbol* symbol);
82 :
83 : void AppendDetailed(String* str, bool show_impl_info);
84 :
85 : // Append a portion of a string.
86 : void AppendStringPart(const char* str, int len);
87 :
88 : // Helpers for appending char, C-string and heap string without
89 : // buffering. This is useful for entries that can exceed the 2kB
90 : // limit.
91 : void AppendUnbufferedChar(char c);
92 : void AppendUnbufferedCString(const char* str);
93 : void AppendUnbufferedHeapString(String* source);
94 :
95 : // Write the log message to the log file currently opened.
96 : void WriteToLogFile();
97 :
98 : private:
99 : Log* log_;
100 : base::LockGuard<base::Mutex> lock_guard_;
101 : int pos_;
102 : };
103 :
104 : private:
105 : explicit Log(Logger* logger);
106 :
107 : // Opens stdout for logging.
108 : void OpenStdout();
109 :
110 : // Opens file for logging.
111 : void OpenFile(const char* name);
112 :
113 : // Opens a temporary file for logging.
114 : void OpenTemporaryFile();
115 :
116 : // Implementation of writing to a log file.
117 : int WriteToFile(const char* msg, int length) {
118 : DCHECK_NOT_NULL(output_handle_);
119 38018 : size_t rv = fwrite(msg, 1, length, output_handle_);
120 : DCHECK_EQ(length, rv);
121 : USE(rv);
122 : return length;
123 : }
124 :
125 : // Whether logging is stopped (e.g. due to insufficient resources).
126 : bool is_stopped_;
127 :
128 : // When logging is active output_handle_ is used to store a pointer to log
129 : // destination. mutex_ should be acquired before using output_handle_.
130 : FILE* output_handle_;
131 :
132 : // mutex_ is a Mutex used for enforcing exclusive
133 : // access to the formatting buffer and the log file or log memory buffer.
134 : base::Mutex mutex_;
135 :
136 : // Buffer used for formatting log messages. This is a singleton buffer and
137 : // mutex_ should be acquired before using it.
138 : char* message_buffer_;
139 :
140 : Logger* logger_;
141 :
142 : friend class Logger;
143 : };
144 :
145 :
146 : } // namespace internal
147 : } // namespace v8
148 :
149 : #endif // V8_LOG_UTILS_H_
|