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