Home | History | Annotate | Download | only in base
      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 // Runtime debug flags are flags that have a runtime component, that is, their value can be changed.
     66 // This is meant to implement fast vs slow debug builds, in that certain debug flags can be turned
     67 // on and off. To that effect, expose two macros to help implement and globally drive these flags:
     68 //
     69 // In the header, declare a (class) flag like this:
     70 //
     71 //   class C {
     72 //     DECLARE_RUNTIME_DEBUG_FLAG(kFlag);
     73 //   };
     74 //
     75 // This will declare a flag kFlag that is a constexpr false in release builds, and a static field
     76 // in debug builds. Usage is than uniform as C::kFlag.
     77 //
     78 // In the cc file, define the flag like this:
     79 //
     80 //   DEFINE_RUNTIME_DEBUG_FLAG(C, kFlag);
     81 //
     82 // This will define the static storage, as necessary, and register the flag with the runtime
     83 // infrastructure to toggle the value.
     84 
     85 #ifdef NDEBUG
     86 #define DECLARE_RUNTIME_DEBUG_FLAG(x) \
     87   static constexpr bool x = false;
     88 // Note: the static_assert in the following only works for public flags. Fix this when we cross
     89 //       the line at some point.
     90 #define DEFINE_RUNTIME_DEBUG_FLAG(C, x) \
     91   static_assert(!C::x, "Unexpected enabled flag in release build");
     92 #else
     93 #define DECLARE_RUNTIME_DEBUG_FLAG(x) \
     94   static bool x;
     95 #define DEFINE_RUNTIME_DEBUG_FLAG(C, x) \
     96   bool C::x = RegisterRuntimeDebugFlag(&C::x);
     97 #endif  // NDEBUG
     98 
     99 bool RegisterRuntimeDebugFlag(bool* runtime_debug_flag);
    100 void SetRuntimeDebugFlagsEnabled(bool enabled);
    101 
    102 // 0 if not abort, non-zero if an abort is in progress. Used on fatal exit to prevents recursive
    103 // aborts. Global declaration allows us to disable some error checking to ensure fatal shutdown
    104 // makes forward progress.
    105 extern std::atomic<unsigned int> gAborting;
    106 
    107 // Configure logging based on ANDROID_LOG_TAGS environment variable.
    108 // We need to parse a string that looks like
    109 //
    110 //      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
    111 //
    112 // The tag (or '*' for the global level) comes first, followed by a colon
    113 // and a letter indicating the minimum priority level we're expected to log.
    114 // This can be used to reveal or conceal logs with specific tags.
    115 extern void InitLogging(char* argv[], AbortFunction& default_aborter);
    116 
    117 // Returns the command line used to invoke the current tool or null if InitLogging hasn't been
    118 // performed.
    119 extern const char* GetCmdLine();
    120 
    121 // The command used to start the ART runtime, such as "/system/bin/dalvikvm". If InitLogging hasn't
    122 // been performed then just returns "art"
    123 extern const char* ProgramInvocationName();
    124 
    125 // A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
    126 // hasn't been performed then just returns "art"
    127 extern const char* ProgramInvocationShortName();
    128 
    129 class LogHelper {
    130  public:
    131   // A logging helper for logging a single line. Can be used with little stack.
    132   static void LogLineLowStack(const char* file,
    133                               unsigned int line,
    134                               android::base::LogSeverity severity,
    135                               const char* msg);
    136 
    137  private:
    138   DISALLOW_ALLOCATION();
    139   DISALLOW_COPY_AND_ASSIGN(LogHelper);
    140 };
    141 
    142 // Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
    143 #define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
    144 
    145 // Variant of LOG that logs when verbose logging is enabled for a module. For example,
    146 // VLOG(jni) << "A JNI operation was performed";
    147 #define VLOG(module) if (VLOG_IS_ON(module)) LOG(INFO)
    148 
    149 // Return the stream associated with logging for the given module.
    150 #define VLOG_STREAM(module) LOG_STREAM(INFO)
    151 
    152 }  // namespace art
    153 
    154 #endif  // ART_RUNTIME_BASE_LOGGING_H_
    155