v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
log-file.h
Go to the documentation of this file.
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_LOGGING_LOG_FILE_H_
6#define V8_LOGGING_LOG_FILE_H_
7
8#include <stdio.h>
9
10#include <atomic>
11#include <cstdarg>
12#include <memory>
13#include <optional>
14
18#include "src/flags/flags.h"
20#include "src/utils/ostreams.h"
21
22namespace v8 {
23
24namespace base {
25template <typename T>
26class Vector;
27} // namespace base
28
29namespace internal {
30
31class V8FileLogger;
32
33enum class LogSeparator { kSeparator };
34
35// Functions and data for performing output of log messages.
36class LogFile {
37 public:
38 explicit LogFile(V8FileLogger* logger, std::string log_file_name);
39
40 V8_EXPORT_PRIVATE static bool IsLoggingToConsole(std::string file_name);
42
43 // Frees all resources acquired in Initialize and Open... functions.
44 // When a temporary file is used for the log, returns its stream descriptor,
45 // leaving the file open.
46 FILE* Close();
47
48 std::string file_name() const;
49
50 // Size of buffer used for formatting log messages.
51 static const int kMessageBufferSize = 2048;
52
53 // This mode is only used in tests, as temporary files are automatically
54 // deleted on close and thus can't be accessed afterwards.
55 V8_EXPORT_PRIVATE static const char* const kLogToTemporaryFile;
56 static const char* const kLogToConsole;
57
58 // Utility class for formatting log messages. It escapes the given messages
59 // and then appends them to the static buffer in Log.
61 public:
62 ~MessageBuilder() = default;
63
65 std::optional<int> length_limit = std::nullopt);
67 void AppendString(const char* str);
68 void AppendString(const char* str, size_t length, bool is_one_byte = true);
69 void PRINTF_FORMAT(2, 3) AppendFormatString(const char* format, ...);
70 void AppendCharacter(char c);
71 void AppendTwoByteCharacter(char c1, char c2);
72 void AppendSymbolName(Tagged<Symbol> symbol);
73
74 // Delegate insertion to the underlying {log_}.
75 // All appended strings are escaped to maintain one-line log entries.
76 template <typename T>
77 MessageBuilder& operator<<(T value) {
78 log_->os_ << value;
79 return *this;
80 }
81
82 // Finish the current log line an flush the it to the log file.
83 void WriteToLogFile();
84
85 private:
86 // Create a message builder starting from position 0.
87 // This acquires the mutex in the log as well.
88 explicit MessageBuilder(LogFile* log);
89
90 // Prints the format string into |log_->format_buffer_|. Returns the length
91 // of the result, or kMessageBufferSize if it was truncated.
92 int PRINTF_FORMAT(2, 0)
93 FormatStringIntoBuffer(const char* format, va_list args);
94
95 void AppendSymbolNameDetails(Tagged<String> str, bool show_impl_info);
96
97 void PRINTF_FORMAT(2, 3) AppendRawFormatString(const char* format, ...);
98 void AppendRawString(const char* format);
99 void AppendRawCharacter(const char character);
100
103
104 friend class LogFile;
105 };
106
107 // Use this method to create an instance of LogFile::MessageBuilder. This
108 // method will return null if logging is disabled.
110
111 private:
112 static FILE* CreateOutputHandle(std::string file_name);
113 base::Mutex* mutex() { return &mutex_; }
114
115 void WriteLogHeader();
116
118
119 std::string file_name_;
120
121 // When logging is active output_handle_ is used to store a pointer to log
122 // destination. mutex_ should be acquired before using output_handle_.
124
126
127 // mutex_ is a Mutex used for enforcing exclusive
128 // access to the formatting buffer and the log file or log memory buffer.
130
131 // Buffer used for formatting log messages. This is a singleton buffer and
132 // mutex_ should be acquired before using it.
133 std::unique_ptr<char[]> format_buffer_;
134
135 friend class V8FileLogger;
136};
137
138template <>
139LogFile::MessageBuilder& LogFile::MessageBuilder::operator<<<LogSeparator>(
141template <>
142LogFile::MessageBuilder& LogFile::MessageBuilder::operator<<<void*>(
143 void* pointer);
144template <>
145LogFile::MessageBuilder& LogFile::MessageBuilder::operator<<<const char*>(
146 const char* string);
147template <>
148LogFile::MessageBuilder& LogFile::MessageBuilder::operator<<<char>(char c);
149template <>
150LogFile::MessageBuilder& LogFile::MessageBuilder::operator<< <Tagged<String>>(
152template <>
153LogFile::MessageBuilder& LogFile::MessageBuilder::operator<< <Tagged<Symbol>>(
154 Tagged<Symbol> symbol);
155template <>
156LogFile::MessageBuilder& LogFile::MessageBuilder::operator<< <Tagged<Name>>(
158
159} // namespace internal
160} // namespace v8
161
162#endif // V8_LOGGING_LOG_FILE_H_
void void AppendRawString(const char *format)
Definition log-file.cc:247
void AppendSymbolName(Tagged< Symbol > symbol)
Definition log-file.cc:195
void AppendSymbolNameDetails(Tagged< String > str, bool show_impl_info)
Definition log-file.cc:207
NoGarbageCollectionMutexGuard lock_guard_
Definition log-file.h:102
int PRINTF_FORMAT(2, 0) FormatStringIntoBuffer(const char *format
void PRINTF_FORMAT(2, 3) AppendFormatString(const char *format
void AppendString(Tagged< String > str, std::optional< int > length_limit=std::nullopt)
Definition log-file.cc:112
void AppendRawCharacter(const char character)
Definition log-file.cc:251
void AppendTwoByteCharacter(char c1, char c2)
Definition log-file.cc:167
LogFile(V8FileLogger *logger, std::string log_file_name)
Definition log-file.cc:55
base::Mutex mutex_
Definition log-file.h:129
static FILE * CreateOutputHandle(std::string file_name)
Definition log-file.cc:32
static V8_EXPORT_PRIVATE bool IsLoggingToTemporaryFile(std::string file_name)
Definition log-file.cc:51
std::unique_ptr< LogFile::MessageBuilder > NewMessageBuilder()
Definition log-file.cc:79
V8FileLogger * logger_
Definition log-file.h:117
std::unique_ptr< char[]> format_buffer_
Definition log-file.h:133
std::string file_name() const
Definition log-file.cc:107
static V8_EXPORT_PRIVATE bool IsLoggingToConsole(std::string file_name)
Definition log-file.cc:46
std::string file_name_
Definition log-file.h:119
static const char *const kLogToConsole
Definition log-file.h:56
static const int kMessageBufferSize
Definition log-file.h:51
static V8_EXPORT_PRIVATE const char *const kLogToTemporaryFile
Definition log-file.h:55
base::Mutex * mutex()
Definition log-file.h:113
STL namespace.
BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL BUILTIN_FP_CALL int character
too high values may cause the compiler to set high thresholds for inlining to as much as possible avoid inlined allocation of objects that cannot escape trace load stores from virtual maglev objects use TurboFan fast string builder analyze liveness of environment slots and zap dead values trace TurboFan load elimination emit data about basic block usage in builtins to this enable builtin reordering when run mksnapshot flag for emit warnings when applying builtin profile data verify register allocation in TurboFan randomly schedule instructions to stress dependency tracking enable store store elimination in TurboFan rewrite far to near simulate GC compiler thread race related to allow float parameters to be passed in simulator mode JS Wasm Run additional turbo_optimize_inlined_js_wasm_wrappers enable experimental feedback collection in generic lowering enable Turboshaft s WasmLoadElimination enable Turboshaft s low level load elimination for JS enable Turboshaft s escape analysis for string concatenation use enable Turbolev features that we want to ship in the not too far future trace individual Turboshaft reduction steps trace intermediate Turboshaft reduction steps invocation count threshold for early optimization Enables optimizations which favor memory size over execution speed Enables sampling allocation profiler with X as a sample interval min size of a semi the new space consists of two semi spaces max size of the Collect garbage after Collect garbage after keeps maps alive for< n > old space garbage collections print one detailed trace line in name
Definition flags.cc:2086
return value
Definition map-inl.h:893
template const char * string
#define PRINTF_FORMAT(format_param, dots_param)
#define V8_EXPORT_PRIVATE
Definition macros.h:460