1 /*
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  */
16 
17 #ifndef ART_LIBARTBASE_BASE_LOGGING_H_
18 #define ART_LIBARTBASE_BASE_LOGGING_H_
19 
20 #include "android-base/logging.h"
21 #include "macros.h"
22 
23 namespace art {
24 
25 // Make libbase's LogSeverity more easily available.
26 using ::android::base::LogSeverity;
27 using ::android::base::ScopedLogSeverity;
28 
29 // Abort function.
30 using AbortFunction = void(const char*);
31 
32 // The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
33 // and the "-verbose:" command line argument.
34 struct LogVerbosity {
35   bool class_linker;  // Enabled with "-verbose:class".
36   bool collector;
37   bool compiler;
38   bool deopt;
39   bool gc;
40   bool heap;
41   bool jdwp;
42   bool jit;
43   bool jni;
44   bool monitor;
45   bool oat;
46   bool profiler;
47   bool signals;
48   bool simulator;
49   bool startup;
50   bool third_party_jni;  // Enabled with "-verbose:third-party-jni".
51   bool threads;
52   bool verifier;
53   bool verifier_debug;   // Only works in debug builds.
54   bool image;
55   bool systrace_lock_logging;  // Enabled with "-verbose:sys-locks".
56   bool agents;
57   bool dex;  // Some dex access output etc.
58 };
59 
60 // Global log verbosity setting, initialized by InitLogging.
61 extern LogVerbosity gLogVerbosity;
62 
63 // Configure logging based on ANDROID_LOG_TAGS environment variable.
64 // We need to parse a string that looks like
65 //
66 //      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
67 //
68 // The tag (or '*' for the global level) comes first, followed by a colon
69 // and a letter indicating the minimum priority level we're expected to log.
70 // This can be used to reveal or conceal logs with specific tags.
71 extern void InitLogging(char* argv[], AbortFunction& default_aborter);
72 
73 // Returns the command line used to invoke the current tool or null if InitLogging hasn't been
74 // performed.
75 extern const char* GetCmdLine();
76 
77 // The command used to start the ART runtime, such as "/apex/com.android.runtime/bin/dalvikvm". If
78 // InitLogging hasn't been performed then just returns "art".
79 extern const char* ProgramInvocationName();
80 
81 // A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
82 // hasn't been performed then just returns "art".
83 extern const char* ProgramInvocationShortName();
84 
85 class LogHelper {
86  public:
87   // A logging helper for logging a single line. Can be used with little stack.
88   static void LogLineLowStack(const char* file,
89                               unsigned int line,
90                               android::base::LogSeverity severity,
91                               const char* msg);
92 
93  private:
94   DISALLOW_ALLOCATION();
95   DISALLOW_COPY_AND_ASSIGN(LogHelper);
96 };
97 
98 // Copy the contents of file_name to the log stream for level.
99 bool PrintFileToLog(const std::string& file_name, android::base::LogSeverity level);
100 
101 // Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
102 #define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
103 
104 // Variant of LOG that logs when verbose logging is enabled for a module. For example,
105 // VLOG(jni) << "A JNI operation was performed";
106 #define VLOG(module) if (VLOG_IS_ON(module)) LOG(INFO)
107 
108 // Return the stream associated with logging for the given module.
109 #define VLOG_STREAM(module) LOG_STREAM(INFO)
110 
111 }  // namespace art
112 
113 #endif  // ART_LIBARTBASE_BASE_LOGGING_H_
114