logging.h revision f57589fd40e5b69dad5aff060ccef756507f381a
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
26namespace art {
27
28// Make libbase's LogSeverity more easily available.
29using ::android::base::LogSeverity;
30using ::android::base::ScopedLogSeverity;
31
32// Abort function.
33using 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.
37struct 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};
60
61// Global log verbosity setting, initialized by InitLogging.
62extern LogVerbosity gLogVerbosity;
63
64// 0 if not abort, non-zero if an abort is in progress. Used on fatal exit to prevents recursive
65// aborts. Global declaration allows us to disable some error checking to ensure fatal shutdown
66// makes forward progress.
67extern unsigned int gAborting;
68
69// Configure logging based on ANDROID_LOG_TAGS environment variable.
70// We need to parse a string that looks like
71//
72//      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
73//
74// The tag (or '*' for the global level) comes first, followed by a colon
75// and a letter indicating the minimum priority level we're expected to log.
76// This can be used to reveal or conceal logs with specific tags.
77extern void InitLogging(char* argv[], AbortFunction& default_aborter);
78
79// Returns the command line used to invoke the current tool or null if InitLogging hasn't been
80// performed.
81extern const char* GetCmdLine();
82
83// The command used to start the ART runtime, such as "/system/bin/dalvikvm". If InitLogging hasn't
84// been performed then just returns "art"
85extern const char* ProgramInvocationName();
86
87// A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
88// hasn't been performed then just returns "art"
89extern const char* ProgramInvocationShortName();
90
91class LogHelper {
92 public:
93  // A logging helper for logging a single line. Can be used with little stack.
94  static void LogLineLowStack(const char* file,
95                              unsigned int line,
96                              android::base::LogSeverity severity,
97                              const char* msg);
98
99 private:
100  DISALLOW_ALLOCATION();
101  DISALLOW_COPY_AND_ASSIGN(LogHelper);
102};
103
104// Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
105#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
106
107// Variant of LOG that logs when verbose logging is enabled for a module. For example,
108// VLOG(jni) << "A JNI operation was performed";
109#define VLOG(module) if (VLOG_IS_ON(module)) LOG(INFO)
110
111// Return the stream associated with logging for the given module.
112#define VLOG_STREAM(module) LOG_STREAM(INFO)
113
114}  // namespace art
115
116#endif  // ART_RUNTIME_BASE_LOGGING_H_
117