Improve VLOG_STREAM

Previously VLOG_STREAM would always print even if the module was
turned off. This changes VLOG_STREAM to not print when the module is
off. Like with LOG_STREAM the stream is consumed even if it's not
printed.

Also updated some ti_heap.cc logs to use LOG_STREAM.

Test: ./test/run-test --host --dev 1981
Test: ./test/run-test --host --dev --runtime-option -verbose:plugin 1981
Test: ./test.py --host
Change-Id: If709474918b3e67749647fae5accb0ce98ea3a59
diff --git a/libartbase/base/logging.h b/libartbase/base/logging.h
index 47c66bc..f9de355 100644
--- a/libartbase/base/logging.h
+++ b/libartbase/base/logging.h
@@ -17,6 +17,9 @@
 #ifndef ART_LIBARTBASE_BASE_LOGGING_H_
 #define ART_LIBARTBASE_BASE_LOGGING_H_
 
+#include <sstream>
+#include <variant>
+
 #include "android-base/logging.h"
 #include "macros.h"
 
@@ -107,8 +110,46 @@
 // VLOG(jni) << "A JNI operation was performed";
 #define VLOG(module) if (VLOG_IS_ON(module)) LOG(INFO)
 
-// Return the stream associated with logging for the given module.
-#define VLOG_STREAM(module) LOG_STREAM(INFO)
+// Holder to implement VLOG_STREAM.
+class VlogMessage {
+ public:
+  // TODO Taken from android_base.
+  VlogMessage(bool enable,
+              const char* file,
+              unsigned int line,
+              ::android::base::LogId id,
+              ::android::base::LogSeverity severity,
+              const char* tag,
+              int error)
+      : msg_(std::in_place_type<std::ostringstream>) {
+    if (enable) {
+      msg_.emplace<::android::base::LogMessage>(file, line, id, severity, tag, error);
+    }
+  }
+
+  std::ostream& stream() {
+    if (std::holds_alternative<std::ostringstream>(msg_)) {
+      return std::get<std::ostringstream>(msg_);
+    } else {
+      return std::get<::android::base::LogMessage>(msg_).stream();
+    }
+  }
+
+ private:
+  std::variant<::android::base::LogMessage, std::ostringstream> msg_;
+};
+
+// Return the stream associated with logging for the given module. NB Unlike VLOG function calls
+// will still be performed. Output will be suppressed if the module is not on.
+#define VLOG_STREAM(module)                    \
+  ::art::VlogMessage(VLOG_IS_ON(module),       \
+                     __FILE__,                 \
+                     __LINE__,                 \
+                     ::android::base::DEFAULT, \
+                     ::android::base::INFO,    \
+                     _LOG_TAG_INTERNAL,        \
+                     -1)                       \
+      .stream()
 
 }  // namespace art
 
diff --git a/openjdkjvmti/ti_heap.cc b/openjdkjvmti/ti_heap.cc
index f14fbaf..bbb2ced 100644
--- a/openjdkjvmti/ti_heap.cc
+++ b/openjdkjvmti/ti_heap.cc
@@ -1707,11 +1707,11 @@
             const art::JavaFrameRootInfo& jfri =
                 art::down_cast<const art::JavaFrameRootInfo&>(info);
             if (jfri.GetVReg() == art::JavaFrameRootInfo::kMethodDeclaringClass) {
-              info.Describe(LOG_STREAM(INFO) << "Not changing declaring-class during stack walk. "
-                                                "Found obsolete java frame id ");
+              info.Describe(VLOG_STREAM(plugin) << "Not changing declaring-class during stack"
+                                                << " walk. Found obsolete java frame id ");
               continue;
             } else {
-              info.Describe(LOG_STREAM(INFO) << "Found java frame id ");
+              info.Describe(VLOG_STREAM(plugin) << "Found java frame id ");
               threads_with_roots_.insert(info.GetThreadId());
             }
           }
@@ -1733,11 +1733,11 @@
             const art::JavaFrameRootInfo& jfri =
                 art::down_cast<const art::JavaFrameRootInfo&>(info);
             if (jfri.GetVReg() == art::JavaFrameRootInfo::kMethodDeclaringClass) {
-              info.Describe(LOG_STREAM(INFO) << "Not changing declaring-class during stack walk. "
-                                                "Found obsolete java frame id ");
+              info.Describe(VLOG_STREAM(plugin) << "Not changing declaring-class during stack"
+                                                << " walk. Found obsolete java frame id ");
               continue;
             } else {
-              info.Describe(LOG_STREAM(INFO) << "Found java frame id ");
+              info.Describe(VLOG_STREAM(plugin) << "Found java frame id ");
               threads_with_roots_.insert(info.GetThreadId());
             }
           }