142ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes/*
242ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * Copyright (C) 2011 The Android Open Source Project
342ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes *
442ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
542ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * you may not use this file except in compliance with the License.
642ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * You may obtain a copy of the License at
742ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes *
842ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
942ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes *
1042ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1142ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1242ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1342ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * See the License for the specific language governing permissions and
1442ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes * limitations under the License.
1542ee14279065352a4b9a3e8028d02c567e847d05Elliott Hughes */
166c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_BASE_LOGGING_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_BASE_LOGGING_H_
196c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro
20cf7f19135f0e273f7b0136315633c2abfc715343Ian Rogers#include <ostream>
21700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers
22761600567d73b23324ae0251e871c15d6849ffd8Elliott Hughes#include "base/macros.h"
236c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro
24c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersnamespace art {
25c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
26c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersenum LogSeverity {
277fe30233d32a872f5349e921dba65bd767ad994bAndreas Gampe  NONE,            // Fake level, don't log at all.
28c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  VERBOSE,
29c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  DEBUG,
30c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  INFO,
31c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  WARNING,
32c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  ERROR,
33c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  FATAL,
34c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  INTERNAL_FATAL,  // For Runtime::Abort.
35c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers};
36c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
37c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// The members of this struct are the valid arguments to VLOG and VLOG_IS_ON in code,
38c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// and the "-verbose:" command line argument.
39c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersstruct LogVerbosity {
40c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool class_linker;  // Enabled with "-verbose:class".
4166a55394b0489b30576f90499f24b792a400a2d2Mathieu Chartier  bool collector;
42c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool compiler;
43f3d1f94cf730948f8978befa7cf60ab28ff0f281Andreas Gampe  bool deopt;
44c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool gc;
45c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool heap;
46c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool jdwp;
47e5f13e57ff8fa36342beb33830b3ec5942a61ccaMathieu Chartier  bool jit;
48c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool jni;
49c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool monitor;
5066d874d96d5699bb090c59f47a5a528956ca053eRichard Uhler  bool oat;
51c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool profiler;
52c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool signals;
53751beff19b36f777d9e3a966d754fd9cfad5d534Phil Wang  bool simulator;
54c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool startup;
55c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool third_party_jni;  // Enabled with "-verbose:third-party-jni".
56c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool threads;
57c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  bool verifier;
58fbc31087932a65e036a153afab3049dc5298656aMathieu Chartier  bool image;
59fc6898769ae1ef91ec3e41c0a273401213cb82cdAndreas Gampe  bool systrace_lock_logging;  // Enabled with "-verbose:sys-locks".
60c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers};
61c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
62c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Global log verbosity setting, initialized by InitLogging.
63c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersextern LogVerbosity gLogVerbosity;
64c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
65db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray// 0 if not abort, non-zero if an abort is in progress. Used on fatal exit to prevents recursive
66db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray// aborts. Global declaration allows us to disable some error checking to ensure fatal shutdown
67db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray// makes forward progress.
68db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffrayextern unsigned int gAborting;
69db978719dbcb73fc6acfd193561445c4462786b8Nicolas Geoffray
70c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Configure logging based on ANDROID_LOG_TAGS environment variable.
71c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// We need to parse a string that looks like
72c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers//
73c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers//      *:v jdwp:d dalvikvm:d dalvikvm-gc:i dalvikvmi:i
74c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers//
75c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// The tag (or '*' for the global level) comes first, followed by a colon
76c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// and a letter indicating the minimum priority level we're expected to log.
77c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// This can be used to reveal or conceal logs with specific tags.
78c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersextern void InitLogging(char* argv[]);
79c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
802cebb24bfc3247d3e9be138a3350106737455918Mathieu Chartier// Returns the command line used to invoke the current tool or null if InitLogging hasn't been
81c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// performed.
82c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersextern const char* GetCmdLine();
83c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
84c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// The command used to start the ART runtime, such as "/system/bin/dalvikvm". If InitLogging hasn't
85c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// been performed then just returns "art"
86c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersextern const char* ProgramInvocationName();
87c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
88c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// A short version of the command used to start the ART runtime, such as "dalvikvm". If InitLogging
89c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// hasn't been performed then just returns "art"
90c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersextern const char* ProgramInvocationShortName();
91c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
92c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Logs a message to logcat on Android otherwise to stderr. If the severity is FATAL it also causes
93c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// an abort. For example: LOG(FATAL) << "We didn't expect to reach here";
94c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define LOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, -1).stream()
95c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
96c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// A variant of LOG that also logs the current errno value. To be used when library calls fail.
97c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define PLOG(severity) ::art::LogMessage(__FILE__, __LINE__, severity, errno).stream()
98c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
99c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Marker that code is yet to be implemented.
100c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define UNIMPLEMENTED(level) LOG(level) << __PRETTY_FUNCTION__ << " unimplemented "
101c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
102c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Is verbose logging enabled for the given module? Where the module is defined in LogVerbosity.
103c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define VLOG_IS_ON(module) UNLIKELY(::art::gLogVerbosity.module)
104c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
105c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Variant of LOG that logs when verbose logging is enabled for a module. For example,
106c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// VLOG(jni) << "A JNI operation was performed";
107c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define VLOG(module) \
108c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  if (VLOG_IS_ON(module)) \
109c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers    ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
110c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
111c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Return the stream associated with logging for the given module.
112c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define VLOG_STREAM(module) ::art::LogMessage(__FILE__, __LINE__, INFO, -1).stream()
113c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
114c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Check whether condition x holds and LOG(FATAL) if not. The value of the expression x is only
115c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// evaluated once. Extra logging can be appended using << after. For example,
116c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// CHECK(false == true) results in a log message of "Check failed: false == true".
1176c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro#define CHECK(x) \
118caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers  if (UNLIKELY(!(x))) \
119c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers    ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
120710a0cbb4b0af03ce8651ed7936437c73250045eElliott Hughes        << "Check failed: " #x << " "
121eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes
122c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Helper for CHECK_xx(x,y) macros.
1231f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_OP(LHS, RHS, OP) \
1249b3c3cdb62f7142384e6bf2c0cb6e3a76b16f0e3Mathieu Chartier  for (auto _values = ::art::MakeEagerEvaluator(LHS, RHS); \
125362f9bc807169bcfc8761dde067bbfb79b5ad0fdElliott Hughes       UNLIKELY(!(_values.lhs OP _values.rhs)); /* empty */) \
126c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers    ::art::LogMessage(__FILE__, __LINE__, ::art::FATAL, -1).stream() \
127f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes        << "Check failed: " << #LHS << " " << #OP << " " << #RHS \
128f5a7a476e7ea63e094ff0f011dccc170607e6f6bElliott Hughes        << " (" #LHS "=" << _values.lhs << ", " #RHS "=" << _values.rhs << ") "
1291f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes
130c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
131c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Check whether a condition holds between x and y, LOG(FATAL) if not. The value of the expressions
132c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// x and y is evaluated once. Extra logging can be appended using << after. For example,
133c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// CHECK_NE(0 == 1, false) results in "Check failed: false != false (0==1=false, false=false) ".
1341f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_EQ(x, y) CHECK_OP(x, y, ==)
1351f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_NE(x, y) CHECK_OP(x, y, !=)
1361f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_LE(x, y) CHECK_OP(x, y, <=)
1371f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_LT(x, y) CHECK_OP(x, y, <)
1381f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_GE(x, y) CHECK_OP(x, y, >=)
1391f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_GT(x, y) CHECK_OP(x, y, >)
140eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes
141c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Helper for CHECK_STRxx(s1,s2) macros.
142eb4f614f2eb53b92ebd416fa418f550861655887Elliott Hughes#define CHECK_STROP(s1, s2, sense) \
143caab8c4ef372db5c119bfac1911fa27b174a935cIan Rogers  if (UNLIKELY((strcmp(s1, s2) == 0) != sense)) \
144c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers    LOG(::art::FATAL) << "Check failed: " \
145c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers        << "\"" << s1 << "\"" \
146c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers        << (sense ? " == " : " != ") \
147c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers        << "\"" << s2 << "\""
1486c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro
149c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Check for string (const char*) equality between s1 and s2, LOG(FATAL) if not.
1501f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_STREQ(s1, s2) CHECK_STROP(s1, s2, true)
1511f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes#define CHECK_STRNE(s1, s2) CHECK_STROP(s1, s2, false)
1521f359b08f5ad33ae72e4073b86a9257fe1ed11e5Elliott Hughes
153c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Perform the pthread function call(args), LOG(FATAL) on error.
1548d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes#define CHECK_PTHREAD_CALL(call, args, what) \
1558d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes  do { \
1568d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes    int rc = call args; \
1578d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes    if (rc != 0) { \
1588d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes      errno = rc; \
159c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers      PLOG(::art::FATAL) << # call << " failed for " << what; \
1608d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes    } \
1618d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes  } while (false)
1628d768a954b101a9532f980253ac46be2c53aba11Elliott Hughes
163836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko// CHECK that can be used in a constexpr function. For example,
164836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko//    constexpr int half(int n) {
165836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko//      return
166836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko//          DCHECK_CONSTEXPR(n >= 0, , 0)
167836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko//          CHECK_CONSTEXPR((n & 1) == 0), << "Extra debugging output: n = " << n, 0)
168836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko//          n / 2;
169836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko//    }
170836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko#define CHECK_CONSTEXPR(x, out, dummy) \
171c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  (UNLIKELY(!(x))) ? (LOG(::art::FATAL) << "Check failed: " << #x out, dummy) :
1726c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro
173836424805dd48af67088d30992a3f2e6841ca047Vladimir Marko
174c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// DCHECKs are debug variants of CHECKs only enabled in debug builds. Generally CHECK should be
175c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// used unless profiling identifies a CHECK as being in performance critical code.
176c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#if defined(NDEBUG)
177c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersstatic constexpr bool kEnableDChecks = false;
178c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#else
179c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersstatic constexpr bool kEnableDChecks = true;
1806c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro#endif
1816c21dc1bcafd83e90daa42a27dacd285278f3667Carl Shapiro
182c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK(x) if (::art::kEnableDChecks) CHECK(x)
183c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_EQ(x, y) if (::art::kEnableDChecks) CHECK_EQ(x, y)
184c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_NE(x, y) if (::art::kEnableDChecks) CHECK_NE(x, y)
185c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_LE(x, y) if (::art::kEnableDChecks) CHECK_LE(x, y)
186c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_LT(x, y) if (::art::kEnableDChecks) CHECK_LT(x, y)
187c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_GE(x, y) if (::art::kEnableDChecks) CHECK_GE(x, y)
188c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_GT(x, y) if (::art::kEnableDChecks) CHECK_GT(x, y)
189c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_STREQ(s1, s2) if (::art::kEnableDChecks) CHECK_STREQ(s1, s2)
190c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_STRNE(s1, s2) if (::art::kEnableDChecks) CHECK_STRNE(s1, s2)
191c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#if defined(NDEBUG)
192c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_CONSTEXPR(x, out, dummy)
193c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#else
194c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#define DCHECK_CONSTEXPR(x, out, dummy) CHECK_CONSTEXPR(x, out, dummy)
195c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers#endif
1963ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
197c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Temporary class created to evaluate the LHS and RHS, used with MakeEagerEvaluator to infer the
198c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// types of LHS and RHS.
1993ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughestemplate <typename LHS, typename RHS>
2003ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughesstruct EagerEvaluator {
20102875c56e80500fb0cc4671e33f57968232222eeIan Rogers  EagerEvaluator(LHS l, RHS r) : lhs(l), rhs(r) { }
2023ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes  LHS lhs;
2033ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes  RHS rhs;
2043ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes};
2053ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
206c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Helper function for CHECK_xx.
207c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogerstemplate <typename LHS, typename RHS>
208c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersstatic inline EagerEvaluator<LHS, RHS> MakeEagerEvaluator(LHS lhs, RHS rhs) {
209c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  return EagerEvaluator<LHS, RHS>(lhs, rhs);
210c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers}
211c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers
212c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Explicitly instantiate EagerEvalue for pointers so that char*s aren't treated as strings. To
213c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// compare strings use CHECK_STREQ and CHECK_STRNE. We rely on signed/unsigned warnings to
214aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes// protect you against combinations not explicitly listed below.
215aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes#define EAGER_PTR_EVALUATOR(T1, T2) \
216aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes  template <> struct EagerEvaluator<T1, T2> { \
21702875c56e80500fb0cc4671e33f57968232222eeIan Rogers    EagerEvaluator(T1 l, T2 r) \
21802875c56e80500fb0cc4671e33f57968232222eeIan Rogers        : lhs(reinterpret_cast<const void*>(l)), \
21902875c56e80500fb0cc4671e33f57968232222eeIan Rogers          rhs(reinterpret_cast<const void*>(r)) { } \
220aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes    const void* lhs; \
221aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes    const void* rhs; \
222aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes  }
223aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(const char*, const char*);
224aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(const char*, char*);
225aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(char*, const char*);
226aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(char*, char*);
227aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(const unsigned char*, const unsigned char*);
228aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(const unsigned char*, unsigned char*);
229aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(unsigned char*, const unsigned char*);
230aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(unsigned char*, unsigned char*);
231aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(const signed char*, const signed char*);
232aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(const signed char*, signed char*);
233aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(signed char*, const signed char*);
234aa6a588eb83288481389aa08b03105346a87e706Elliott HughesEAGER_PTR_EVALUATOR(signed char*, signed char*);
235aa6a588eb83288481389aa08b03105346a87e706Elliott Hughes
236c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// Data for the log message, not stored in LogMessage to avoid increasing the stack size.
237c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogersclass LogMessageData;
2383b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes
239c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// A LogMessage is a temporarily scoped object used by LOG and the unlikely part of a CHECK. The
240c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers// destructor will abort if the severity is FATAL.
2413ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughesclass LogMessage {
2423ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes public:
243c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  LogMessage(const char* file, unsigned int line, LogSeverity severity, int error);
24474c070440e9c3f362cd0e9e13754bc8693ed9073Sebastien Hertz
24590443477f9a0061581c420775ce3b7eeae7468bcMathieu Chartier  ~LogMessage();  // TODO: enable REQUIRES(!Locks::logging_lock_).
24674c070440e9c3f362cd0e9e13754bc8693ed9073Sebastien Hertz
247c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  // Returns the stream associated with the message, the LogMessage performs output when it goes
248c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  // out of scope.
249c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  std::ostream& stream();
2503ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
251c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  // The routine that performs the actual logging.
252c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers  static void LogLine(const char* file, unsigned int line, LogSeverity severity, const char* msg);
2533ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
254f4d4da18aa1914d10264082bd0433f59bff45453Ian Rogers  // A variant of the above for use with little stack.
255f4d4da18aa1914d10264082bd0433f59bff45453Ian Rogers  static void LogLineLowStack(const char* file, unsigned int line, LogSeverity severity,
256f4d4da18aa1914d10264082bd0433f59bff45453Ian Rogers                              const char* msg);
257f4d4da18aa1914d10264082bd0433f59bff45453Ian Rogers
258c7dd295a4e0cc1d15c0c96088e55a85389bade74Ian Rogers private:
259700a402244a1a423da4f3ba8032459f4b65fa18fIan Rogers  const std::unique_ptr<LogMessageData> data_;
2603ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
2613ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes  DISALLOW_COPY_AND_ASSIGN(LogMessage);
2623ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes};
2633ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
264369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe// Allows to temporarily change the minimum severity level for logging.
265369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampeclass ScopedLogSeverity {
266369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe public:
267369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  explicit ScopedLogSeverity(LogSeverity level);
268369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  ~ScopedLogSeverity();
269369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe
270369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe private:
271369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe  LogSeverity old_;
272369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe};
273369810a98e6394b6dd162f5349e38a1f597b3bc7Andreas Gampe
2743ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes}  // namespace art
2753ea7e9931149dd852bb6fce241d1da6406125df6Elliott Hughes
276fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_BASE_LOGGING_H_
277