blob: f9de355a794bc4e8def8bd5f5d4b37d616b6a21c [file] [log] [blame]
Elliott Hughes42ee1422011-09-06 12:33:32 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Carl Shapiro6c21dc12011-06-20 15:20:52 -070016
David Sehrc431b9d2018-03-02 12:01:51 -080017#ifndef ART_LIBARTBASE_BASE_LOGGING_H_
18#define ART_LIBARTBASE_BASE_LOGGING_H_
Carl Shapiro6c21dc12011-06-20 15:20:52 -070019
Alex Lightb55ef652019-09-26 15:23:28 -070020#include <sstream>
21#include <variant>
22
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070023#include "android-base/logging.h"
David Sehr1979c642018-04-26 14:41:18 -070024#include "macros.h"
Carl Shapiro6c21dc12011-06-20 15:20:52 -070025
Ian Rogersc7dd2952014-10-21 23:31:19 -070026namespace art {
27
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070028// Make libbase's LogSeverity more easily available.
Andreas Gamped6e54bb2016-09-26 14:07:57 -070029using ::android::base::LogSeverity;
30using ::android::base::ScopedLogSeverity;
Ian Rogersc7dd2952014-10-21 23:31:19 -070031
David Sehrf57589f2016-10-17 10:09:33 -070032// Abort function.
33using AbortFunction = void(const char*);
34
Ian Rogersc7dd2952014-10-21 23:31:19 -070035// The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
36// and the "-verbose:" command line argument.
37struct LogVerbosity {
38 bool class_linker; // Enabled with "-verbose:class".
Mathieu Chartier66a55392016-02-19 10:25:39 -080039 bool collector;
Ian Rogersc7dd2952014-10-21 23:31:19 -070040 bool compiler;
Andreas Gampef3d1f942015-05-18 21:41:13 -070041 bool deopt;
Ian Rogersc7dd2952014-10-21 23:31:19 -070042 bool gc;
43 bool heap;
Mathieu Chartier765b2a02019-05-02 11:04:13 -070044 bool interpreter; // Enabled with "-verbose:interpreter".
Ian Rogersc7dd2952014-10-21 23:31:19 -070045 bool jdwp;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080046 bool jit;
Ian Rogersc7dd2952014-10-21 23:31:19 -070047 bool jni;
48 bool monitor;
Richard Uhler66d874d2015-01-15 09:37:19 -080049 bool oat;
Ian Rogersc7dd2952014-10-21 23:31:19 -070050 bool profiler;
51 bool signals;
Phil Wang751beff2015-08-28 15:17:15 +080052 bool simulator;
Ian Rogersc7dd2952014-10-21 23:31:19 -070053 bool startup;
54 bool third_party_jni; // Enabled with "-verbose:third-party-jni".
55 bool threads;
56 bool verifier;
Andreas Gampe92d77202017-12-06 20:49:00 -080057 bool verifier_debug; // Only works in debug builds.
Mathieu Chartierfbc31082016-01-24 11:59:56 -080058 bool image;
Andreas Gampec7ed09b2016-04-25 20:08:55 -070059 bool systrace_lock_logging; // Enabled with "-verbose:sys-locks".
Alex Light7233c7e2016-07-28 10:07:45 -070060 bool agents;
Andreas Gampebec07a02017-04-11 13:48:37 -070061 bool dex; // Some dex access output etc.
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000062 bool plugin; // Used by some plugins.
Ian Rogersc7dd2952014-10-21 23:31:19 -070063};
64
65// Global log verbosity setting, initialized by InitLogging.
66extern LogVerbosity gLogVerbosity;
67
Ian Rogersc7dd2952014-10-21 23:31:19 -070068// Configure logging based on ANDROID_LOG_TAGS environment variable.
69// We need to parse a string that looks like
70//
71// *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
72//
73// The tag (or '*' for the global level) comes first, followed by a colon
74// and a letter indicating the minimum priority level we're expected to log.
75// This can be used to reveal or conceal logs with specific tags.
David Sehrf57589f2016-10-17 10:09:33 -070076extern void InitLogging(char* argv[], AbortFunction& default_aborter);
Ian Rogersc7dd2952014-10-21 23:31:19 -070077
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078// Returns the command line used to invoke the current tool or null if InitLogging hasn't been
Ian Rogersc7dd2952014-10-21 23:31:19 -070079// performed.
80extern const char* GetCmdLine();
81
Martin Stjernholmad909af2019-07-16 17:02:44 +010082// The command used to start the ART runtime, such as "/apex/com.android.art/bin/dalvikvm". If
Roland Levillain38a938e2018-09-21 10:55:51 +010083// InitLogging hasn't been performed then just returns "art".
Ian Rogersc7dd2952014-10-21 23:31:19 -070084extern const char* ProgramInvocationName();
85
86// A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
Roland Levillain38a938e2018-09-21 10:55:51 +010087// hasn't been performed then just returns "art".
Ian Rogersc7dd2952014-10-21 23:31:19 -070088extern const char* ProgramInvocationShortName();
89
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070090class LogHelper {
91 public:
92 // A logging helper for logging a single line. Can be used with little stack.
93 static void LogLineLowStack(const char* file,
94 unsigned int line,
95 android::base::LogSeverity severity,
96 const char* msg);
Ian Rogersc7dd2952014-10-21 23:31:19 -070097
Andreas Gampe3fec9ac2016-09-13 10:47:28 -070098 private:
99 DISALLOW_ALLOCATION();
100 DISALLOW_COPY_AND_ASSIGN(LogHelper);
101};
Ian Rogersc7dd2952014-10-21 23:31:19 -0700102
David Sehr79e26072018-04-06 17:58:50 -0700103// Copy the contents of file_name to the log stream for level.
104bool PrintFileToLog(const std::string& file_name, android::base::LogSeverity level);
105
Ian Rogersc7dd2952014-10-21 23:31:19 -0700106// Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
107#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
108
109// Variant of LOG that logs when verbose logging is enabled for a module. For example,
110// VLOG(jni) << "A JNI operation was performed";
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700111#define VLOG(module) if (VLOG_IS_ON(module)) LOG(INFO)
Ian Rogersc7dd2952014-10-21 23:31:19 -0700112
Alex Lightb55ef652019-09-26 15:23:28 -0700113// Holder to implement VLOG_STREAM.
114class VlogMessage {
115 public:
116 // TODO Taken from android_base.
117 VlogMessage(bool enable,
118 const char* file,
119 unsigned int line,
120 ::android::base::LogId id,
121 ::android::base::LogSeverity severity,
122 const char* tag,
123 int error)
124 : msg_(std::in_place_type<std::ostringstream>) {
125 if (enable) {
126 msg_.emplace<::android::base::LogMessage>(file, line, id, severity, tag, error);
127 }
128 }
129
130 std::ostream& stream() {
131 if (std::holds_alternative<std::ostringstream>(msg_)) {
132 return std::get<std::ostringstream>(msg_);
133 } else {
134 return std::get<::android::base::LogMessage>(msg_).stream();
135 }
136 }
137
138 private:
139 std::variant<::android::base::LogMessage, std::ostringstream> msg_;
140};
141
142// Return the stream associated with logging for the given module. NB Unlike VLOG function calls
143// will still be performed. Output will be suppressed if the module is not on.
144#define VLOG_STREAM(module) \
145 ::art::VlogMessage(VLOG_IS_ON(module), \
146 __FILE__, \
147 __LINE__, \
148 ::android::base::DEFAULT, \
149 ::android::base::INFO, \
150 _LOG_TAG_INTERNAL, \
151 -1) \
152 .stream()
Andreas Gampe369810a2015-01-14 19:53:31 -0800153
Elliott Hughes3ea7e992011-10-11 18:48:16 -0700154} // namespace art
155
David Sehrc431b9d2018-03-02 12:01:51 -0800156#endif // ART_LIBARTBASE_BASE_LOGGING_H_