blob: 7621668552d197b8126381feec99ca7ec42c89fe [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2006-2009 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
5#ifndef V8_LOG_UTILS_H_
6#define V8_LOG_UTILS_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include <stdio.h>
9
10#include <cstdarg>
11
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/allocation.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/base/platform/mutex.h"
14#include "src/flags.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000015
Steve Blocka7e24c12009-10-30 11:49:00 +000016namespace v8 {
17namespace internal {
18
Steve Block44f0eee2011-05-26 01:26:41 +010019class Logger;
20
Steve Blocka7e24c12009-10-30 11:49:00 +000021// Functions and data for performing output of log messages.
Steve Block44f0eee2011-05-26 01:26:41 +010022class Log {
Steve Blocka7e24c12009-10-30 11:49:00 +000023 public:
Steve Block44f0eee2011-05-26 01:26:41 +010024 // Performs process-wide initialization.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025 void Initialize(const char* log_file_name);
Steve Blocka7e24c12009-10-30 11:49:00 +000026
27 // Disables logging, but preserves acquired resources.
Steve Block44f0eee2011-05-26 01:26:41 +010028 void stop() { is_stopped_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +000029
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030 static bool InitLogAtStart() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031 return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
32 FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp ||
33 FLAG_ll_prof || FLAG_perf_basic_prof ||
34 FLAG_log_internal_timer_events || FLAG_prof_cpp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 }
36
Steve Block44f0eee2011-05-26 01:26:41 +010037 // Frees all resources acquired in Initialize and Open... functions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000038 // When a temporary file is used for the log, returns its stream descriptor,
39 // leaving the file open.
40 FILE* Close();
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42 // Returns whether logging is enabled.
Steve Block44f0eee2011-05-26 01:26:41 +010043 bool IsEnabled() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000044 return !is_stopped_ && output_handle_ != NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000045 }
46
47 // Size of buffer used for formatting log messages.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000048 static const int kMessageBufferSize = 2048;
49
50 // This mode is only used in tests, as temporary files are automatically
51 // deleted on close and thus can't be accessed afterwards.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000052 static const char* const kLogToTemporaryFile;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 static const char* const kLogToConsole;
54
55 // Utility class for formatting log messages. It fills the message into the
56 // static buffer in Log.
57 class MessageBuilder BASE_EMBEDDED {
58 public:
59 // Create a message builder starting from position 0.
60 // This acquires the mutex in the log as well.
61 explicit MessageBuilder(Log* log);
62 ~MessageBuilder() { }
63
64 // Append string data to the log message.
65 void Append(const char* format, ...);
66
67 // Append string data to the log message.
68 void AppendVA(const char* format, va_list args);
69
70 // Append a character to the log message.
71 void Append(const char c);
72
73 // Append double quoted string to the log message.
74 void AppendDoubleQuotedString(const char* string);
75
76 // Append a heap string.
77 void Append(String* str);
78
79 // Appends an address.
80 void AppendAddress(Address addr);
81
82 void AppendSymbolName(Symbol* symbol);
83
84 void AppendDetailed(String* str, bool show_impl_info);
85
86 // Append a portion of a string.
87 void AppendStringPart(const char* str, int len);
88
89 // Write the log message to the log file currently opened.
90 void WriteToLogFile();
91
92 private:
93 Log* log_;
94 base::LockGuard<base::Mutex> lock_guard_;
95 int pos_;
96 };
Steve Blocka7e24c12009-10-30 11:49:00 +000097
98 private:
Steve Block44f0eee2011-05-26 01:26:41 +010099 explicit Log(Logger* logger);
Steve Blocka7e24c12009-10-30 11:49:00 +0000100
Steve Block44f0eee2011-05-26 01:26:41 +0100101 // Opens stdout for logging.
102 void OpenStdout();
Steve Blocka7e24c12009-10-30 11:49:00 +0000103
Steve Block44f0eee2011-05-26 01:26:41 +0100104 // Opens file for logging.
105 void OpenFile(const char* name);
106
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000107 // Opens a temporary file for logging.
108 void OpenTemporaryFile();
Steve Blocka7e24c12009-10-30 11:49:00 +0000109
110 // Implementation of writing to a log file.
Steve Block44f0eee2011-05-26 01:26:41 +0100111 int WriteToFile(const char* msg, int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 DCHECK(output_handle_ != NULL);
Steve Blockd0582a62009-12-15 09:54:21 +0000113 size_t rv = fwrite(msg, 1, length, output_handle_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 DCHECK(static_cast<size_t>(length) == rv);
Steve Blockd0582a62009-12-15 09:54:21 +0000115 USE(rv);
Ben Murdochf87a2032010-10-22 12:50:53 +0100116 fflush(output_handle_);
Steve Blockd0582a62009-12-15 09:54:21 +0000117 return length;
Steve Blocka7e24c12009-10-30 11:49:00 +0000118 }
119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120 // Whether logging is stopped (e.g. due to insufficient resources).
Steve Block44f0eee2011-05-26 01:26:41 +0100121 bool is_stopped_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000123 // When logging is active output_handle_ is used to store a pointer to log
124 // destination. mutex_ should be acquired before using output_handle_.
Steve Block44f0eee2011-05-26 01:26:41 +0100125 FILE* output_handle_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000126
Steve Blocka7e24c12009-10-30 11:49:00 +0000127 // mutex_ is a Mutex used for enforcing exclusive
128 // access to the formatting buffer and the log file or log memory buffer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000129 base::Mutex mutex_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000130
131 // Buffer used for formatting log messages. This is a singleton buffer and
132 // mutex_ should be acquired before using it.
Steve Block44f0eee2011-05-26 01:26:41 +0100133 char* message_buffer_;
134
135 Logger* logger_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000136
Ben Murdochf87a2032010-10-22 12:50:53 +0100137 friend class Logger;
Steve Blocka7e24c12009-10-30 11:49:00 +0000138};
139
140
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141} // namespace internal
142} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000143
144#endif // V8_LOG_UTILS_H_