blob: 059e5a53c5cab58a811acc01cf90c76eded501e1 [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 Murdochc5610432016-08-08 18:44:38 +010013#include "src/base/compiler-specific.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/base/platform/mutex.h"
15#include "src/flags.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000016
Steve Blocka7e24c12009-10-30 11:49:00 +000017namespace v8 {
18namespace internal {
19
Steve Block44f0eee2011-05-26 01:26:41 +010020class Logger;
21
Steve Blocka7e24c12009-10-30 11:49:00 +000022// Functions and data for performing output of log messages.
Steve Block44f0eee2011-05-26 01:26:41 +010023class Log {
Steve Blocka7e24c12009-10-30 11:49:00 +000024 public:
Steve Block44f0eee2011-05-26 01:26:41 +010025 // Performs process-wide initialization.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026 void Initialize(const char* log_file_name);
Steve Blocka7e24c12009-10-30 11:49:00 +000027
28 // Disables logging, but preserves acquired resources.
Steve Block44f0eee2011-05-26 01:26:41 +010029 void stop() { is_stopped_ = true; }
Steve Blocka7e24c12009-10-30 11:49:00 +000030
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031 static bool InitLogAtStart() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_gc ||
33 FLAG_log_handles || FLAG_log_suspect || FLAG_log_regexp ||
Ben Murdochda12d292016-06-02 14:46:10 +010034 FLAG_ll_prof || FLAG_perf_basic_prof || FLAG_perf_prof ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 FLAG_log_internal_timer_events || FLAG_prof_cpp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 }
37
Steve Block44f0eee2011-05-26 01:26:41 +010038 // Frees all resources acquired in Initialize and Open... functions.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000039 // When a temporary file is used for the log, returns its stream descriptor,
40 // leaving the file open.
41 FILE* Close();
Steve Blocka7e24c12009-10-30 11:49:00 +000042
43 // Returns whether logging is enabled.
Steve Block44f0eee2011-05-26 01:26:41 +010044 bool IsEnabled() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000045 return !is_stopped_ && output_handle_ != NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +000046 }
47
48 // Size of buffer used for formatting log messages.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000049 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.
Ben Murdoch69a99ed2011-11-30 16:03:39 +000053 static const char* const kLogToTemporaryFile;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 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.
Ben Murdochc5610432016-08-08 18:44:38 +010066 void PRINTF_FORMAT(2, 3) Append(const char* format, ...);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067
68 // Append string data to the log message.
Ben Murdochc5610432016-08-08 18:44:38 +010069 void PRINTF_FORMAT(2, 0) AppendVA(const char* format, va_list args);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070
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 };
Steve Blocka7e24c12009-10-30 11:49:00 +000098
99 private:
Steve Block44f0eee2011-05-26 01:26:41 +0100100 explicit Log(Logger* logger);
Steve Blocka7e24c12009-10-30 11:49:00 +0000101
Steve Block44f0eee2011-05-26 01:26:41 +0100102 // Opens stdout for logging.
103 void OpenStdout();
Steve Blocka7e24c12009-10-30 11:49:00 +0000104
Steve Block44f0eee2011-05-26 01:26:41 +0100105 // Opens file for logging.
106 void OpenFile(const char* name);
107
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000108 // Opens a temporary file for logging.
109 void OpenTemporaryFile();
Steve Blocka7e24c12009-10-30 11:49:00 +0000110
111 // Implementation of writing to a log file.
Steve Block44f0eee2011-05-26 01:26:41 +0100112 int WriteToFile(const char* msg, int length) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 DCHECK(output_handle_ != NULL);
Steve Blockd0582a62009-12-15 09:54:21 +0000114 size_t rv = fwrite(msg, 1, length, output_handle_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 DCHECK(static_cast<size_t>(length) == rv);
Steve Blockd0582a62009-12-15 09:54:21 +0000116 USE(rv);
Ben Murdochf87a2032010-10-22 12:50:53 +0100117 fflush(output_handle_);
Steve Blockd0582a62009-12-15 09:54:21 +0000118 return length;
Steve Blocka7e24c12009-10-30 11:49:00 +0000119 }
120
Steve Blocka7e24c12009-10-30 11:49:00 +0000121 // Whether logging is stopped (e.g. due to insufficient resources).
Steve Block44f0eee2011-05-26 01:26:41 +0100122 bool is_stopped_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000123
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000124 // When logging is active output_handle_ is used to store a pointer to log
125 // destination. mutex_ should be acquired before using output_handle_.
Steve Block44f0eee2011-05-26 01:26:41 +0100126 FILE* output_handle_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000127
Steve Blocka7e24c12009-10-30 11:49:00 +0000128 // mutex_ is a Mutex used for enforcing exclusive
129 // access to the formatting buffer and the log file or log memory buffer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130 base::Mutex mutex_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000131
132 // Buffer used for formatting log messages. This is a singleton buffer and
133 // mutex_ should be acquired before using it.
Steve Block44f0eee2011-05-26 01:26:41 +0100134 char* message_buffer_;
135
136 Logger* logger_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000137
Ben Murdochf87a2032010-10-22 12:50:53 +0100138 friend class Logger;
Steve Blocka7e24c12009-10-30 11:49:00 +0000139};
140
141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142} // namespace internal
143} // namespace v8
Steve Blocka7e24c12009-10-30 11:49:00 +0000144
145#endif // V8_LOG_UTILS_H_