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.
5 #ifndef V8_LOG_UTILS_H_
6 #define V8_LOG_UTILS_H_
8 #include "src/allocation.h"
9 #include "src/base/platform/mutex.h"
10 #include "src/flags.h"
17 // Functions and data for performing output of log messages.
20 // Performs process-wide initialization.
21 void Initialize(const char* log_file_name);
23 // Disables logging, but preserves acquired resources.
24 void stop() { is_stopped_ = true; }
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;
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.
38 // Returns whether logging is enabled.
40 return !is_stopped_ && output_handle_ != NULL;
43 // Size of buffer used for formatting log messages.
44 static const int kMessageBufferSize = 2048;
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;
51 // Utility class for formatting log messages. It fills the message into the
52 // static buffer in Log.
53 class MessageBuilder BASE_EMBEDDED {
55 // Create a message builder starting from position 0.
56 // This acquires the mutex in the log as well.
57 explicit MessageBuilder(Log* log);
60 // Append string data to the log message.
61 void Append(const char* format, ...);
63 // Append string data to the log message.
64 void AppendVA(const char* format, va_list args);
66 // Append a character to the log message.
67 void Append(const char c);
69 // Append double quoted string to the log message.
70 void AppendDoubleQuotedString(const char* string);
72 // Append a heap string.
73 void Append(String* str);
75 // Appends an address.
76 void AppendAddress(Address addr);
78 void AppendSymbolName(Symbol* symbol);
80 void AppendDetailed(String* str, bool show_impl_info);
82 // Append a portion of a string.
83 void AppendStringPart(const char* str, int len);
85 // Write the log message to the log file currently opened.
86 void WriteToLogFile();
90 base::LockGuard<base::Mutex> lock_guard_;
95 explicit Log(Logger* logger);
97 // Opens stdout for logging.
100 // Opens file for logging.
101 void OpenFile(const char* name);
103 // Opens a temporary file for logging.
104 void OpenTemporaryFile();
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);
112 fflush(output_handle_);
116 // Whether logging is stopped (e.g. due to insufficient resources).
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_;
123 // mutex_ is a Mutex used for enforcing exclusive
124 // access to the formatting buffer and the log file or log memory buffer.
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_;
137 } } // namespace v8::internal
139 #endif // V8_LOG_UTILS_H_