Android: Remove GetThreadInfo()

This CL is part of merging the helper functions for audio and non-audio JNI code.
The GetThreadInfo() function is unrelated to JNI and I would prefer not to keep
it in a JNI helper file. Also, GetThreadInfo() is a very small function and inlining
it makes it simpler and more transparent IMO, as well as removing a lot of unnecessary
std::string creations.

Bug: webrtc:8689
Change-Id: I7d238fee826d310c0f5343d18b92d0dff864fd6a
Reviewed-on: https://webrtc-review.googlesource.com/36302
Reviewed-by: Henrik Andreassson <henrika@webrtc.org>
Commit-Queue: Henrik Andreassson <henrika@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#21466}
diff --git a/modules/utility/source/helpers_android.cc b/modules/utility/source/helpers_android.cc
index c5a9123..5e760b4 100644
--- a/modules/utility/source/helpers_android.cc
+++ b/modules/utility/source/helpers_android.cc
@@ -9,7 +9,6 @@
  */
 
 #include "modules/utility/include/helpers_android.h"
-#include "rtc_base/checks.h"
 
 #include <android/log.h>
 #include <assert.h>
@@ -17,6 +16,9 @@
 #include <stddef.h>
 #include <unistd.h>
 
+#include "rtc_base/checks.h"
+#include "rtc_base/platform_thread.h"
+
 #define TAG "HelpersAndroid"
 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
 
@@ -82,19 +84,6 @@
   CHECK_EXCEPTION(jni) << "Error during DeleteGlobalRef";
 }
 
-std::string GetThreadId() {
-  char buf[21];  // Big enough to hold a kuint64max plus terminating NULL.
-  int thread_id = gettid();
-  RTC_CHECK_LT(snprintf(buf, sizeof(buf), "%i", thread_id),
-               static_cast<int>(sizeof(buf)))
-      << "Thread id is bigger than uint64??";
-  return std::string(buf);
-}
-
-std::string GetThreadInfo() {
-  return "@[tid=" + GetThreadId() + "]";
-}
-
 AttachThreadScoped::AttachThreadScoped(JavaVM* jvm)
     : attached_(false), jvm_(jvm), env_(NULL) {
   env_ = GetEnv(jvm);
@@ -102,7 +91,7 @@
     // Adding debug log here so we can track down potential leaks and figure
     // out why we sometimes see "Native thread exiting without having called
     // DetachCurrentThread" in logcat outputs.
-    ALOGD("Attaching thread to JVM%s", GetThreadInfo().c_str());
+    ALOGD("Attaching thread to JVM[tid=%d]", rtc::CurrentThreadId());
     jint res = jvm->AttachCurrentThread(&env_, NULL);
     attached_ = (res == JNI_OK);
     RTC_CHECK(attached_) << "AttachCurrentThread failed: " << res;
@@ -111,7 +100,7 @@
 
 AttachThreadScoped::~AttachThreadScoped() {
   if (attached_) {
-    ALOGD("Detaching thread from JVM%s", GetThreadInfo().c_str());
+    ALOGD("Detaching thread from JVM[tid=%d]", rtc::CurrentThreadId());
     jint res = jvm_->DetachCurrentThread();
     RTC_CHECK(res == JNI_OK) << "DetachCurrentThread failed: " << res;
     RTC_CHECK(!GetEnv(jvm_));
diff --git a/modules/utility/source/jvm_android.cc b/modules/utility/source/jvm_android.cc
index 8c27624..fc0739c 100644
--- a/modules/utility/source/jvm_android.cc
+++ b/modules/utility/source/jvm_android.cc
@@ -15,6 +15,7 @@
 #include "modules/utility/include/jvm_android.h"
 
 #include "rtc_base/checks.h"
+#include "rtc_base/platform_thread.h"
 
 #define TAG "JVM"
 #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
@@ -72,7 +73,7 @@
 // AttachCurrentThreadIfNeeded implementation.
 AttachCurrentThreadIfNeeded::AttachCurrentThreadIfNeeded()
     : attached_(false) {
-  ALOGD("AttachCurrentThreadIfNeeded::ctor%s", GetThreadInfo().c_str());
+  ALOGD("AttachCurrentThreadIfNeeded::ctor[tid=%d]", rtc::CurrentThreadId());
   JavaVM* jvm = JVM::GetInstance()->jvm();
   RTC_CHECK(jvm);
   JNIEnv* jni = GetEnv(jvm);
@@ -85,7 +86,7 @@
 }
 
 AttachCurrentThreadIfNeeded::~AttachCurrentThreadIfNeeded() {
-  ALOGD("AttachCurrentThreadIfNeeded::dtor%s", GetThreadInfo().c_str());
+  ALOGD("AttachCurrentThreadIfNeeded::dtor[tid=%d]", rtc::CurrentThreadId());
   RTC_DCHECK(thread_checker_.CalledOnValidThread());
   if (attached_) {
     ALOGD("Detaching thread from JVM");
@@ -97,11 +98,11 @@
 // GlobalRef implementation.
 GlobalRef::GlobalRef(JNIEnv* jni, jobject object)
     : jni_(jni), j_object_(NewGlobalRef(jni, object)) {
-  ALOGD("GlobalRef::ctor%s", GetThreadInfo().c_str());
+  ALOGD("GlobalRef::ctor[tid=%d]", rtc::CurrentThreadId());
 }
 
 GlobalRef::~GlobalRef() {
-  ALOGD("GlobalRef::dtor%s", GetThreadInfo().c_str());
+  ALOGD("GlobalRef::dtor[tid=%d]", rtc::CurrentThreadId());
   DeleteGlobalRef(jni_, j_object_);
 }
 
@@ -134,18 +135,18 @@
 // NativeRegistration implementation.
 NativeRegistration::NativeRegistration(JNIEnv* jni, jclass clazz)
     : JavaClass(jni, clazz), jni_(jni) {
-  ALOGD("NativeRegistration::ctor%s", GetThreadInfo().c_str());
+  ALOGD("NativeRegistration::ctor[tid=%d]", rtc::CurrentThreadId());
 }
 
 NativeRegistration::~NativeRegistration() {
-  ALOGD("NativeRegistration::dtor%s", GetThreadInfo().c_str());
+  ALOGD("NativeRegistration::dtor[tid=%d]", rtc::CurrentThreadId());
   jni_->UnregisterNatives(j_class_);
   CHECK_EXCEPTION(jni_) << "Error during UnregisterNatives";
 }
 
 std::unique_ptr<GlobalRef> NativeRegistration::NewObject(
     const char* name, const char* signature, ...) {
-  ALOGD("NativeRegistration::NewObject%s", GetThreadInfo().c_str());
+  ALOGD("NativeRegistration::NewObject[tid=%d]", rtc::CurrentThreadId());
   va_list args;
   va_start(args, signature);
   jobject obj = jni_->NewObjectV(j_class_,
@@ -185,11 +186,11 @@
 
 // JNIEnvironment implementation.
 JNIEnvironment::JNIEnvironment(JNIEnv* jni) : jni_(jni) {
-  ALOGD("JNIEnvironment::ctor%s", GetThreadInfo().c_str());
+  ALOGD("JNIEnvironment::ctor[tid=%d]", rtc::CurrentThreadId());
 }
 
 JNIEnvironment::~JNIEnvironment() {
-  ALOGD("JNIEnvironment::dtor%s", GetThreadInfo().c_str());
+  ALOGD("JNIEnvironment::dtor[tid=%d]", rtc::CurrentThreadId());
   RTC_DCHECK(thread_checker_.CalledOnValidThread());
 }
 
@@ -218,7 +219,7 @@
 
 // static
 void JVM::Initialize(JavaVM* jvm) {
-  ALOGD("JVM::Initialize%s", GetThreadInfo().c_str());
+  ALOGD("JVM::Initialize[tid=%d]", rtc::CurrentThreadId());
   RTC_CHECK(!g_jvm);
   g_jvm = new JVM(jvm);
 }
@@ -236,7 +237,7 @@
 
 // static
 void JVM::Uninitialize() {
-  ALOGD("JVM::Uninitialize%s", GetThreadInfo().c_str());
+  ALOGD("JVM::Uninitialize[tid=%d]", rtc::CurrentThreadId());
   RTC_DCHECK(g_jvm);
   delete g_jvm;
   g_jvm = nullptr;
@@ -249,19 +250,19 @@
 }
 
 JVM::JVM(JavaVM* jvm) : jvm_(jvm) {
-  ALOGD("JVM::JVM%s", GetThreadInfo().c_str());
+  ALOGD("JVM::JVM[tid=%d]", rtc::CurrentThreadId());
   RTC_CHECK(jni()) << "AttachCurrentThread() must be called on this thread.";
   LoadClasses(jni());
 }
 
 JVM::~JVM() {
-  ALOGD("JVM::~JVM%s", GetThreadInfo().c_str());
+  ALOGD("JVM::~JVM[tid=%d]", rtc::CurrentThreadId());
   RTC_DCHECK(thread_checker_.CalledOnValidThread());
   FreeClassReferences(jni());
 }
 
 std::unique_ptr<JNIEnvironment> JVM::environment() {
-  ALOGD("JVM::environment%s", GetThreadInfo().c_str());
+  ALOGD("JVM::environment[tid=%d]", rtc::CurrentThreadId());
   // The JNIEnv is used for thread-local storage. For this reason, we cannot
   // share a JNIEnv between threads. If a piece of code has no other way to get
   // its JNIEnv, we should share the JavaVM, and use GetEnv to discover the
@@ -276,7 +277,7 @@
 }
 
 JavaClass JVM::GetClass(const char* name) {
-  ALOGD("JVM::GetClass(%s)%s", name, GetThreadInfo().c_str());
+  ALOGD("JVM::GetClass(%s)[tid=%d]", name, rtc::CurrentThreadId());
   RTC_DCHECK(thread_checker_.CalledOnValidThread());
   return JavaClass(jni(), LookUpClass(name));
 }