Merge "setjmp is a macro."
diff --git a/tests/gtest_main.cpp b/tests/gtest_main.cpp
index 1ee8b53..8aa94bb 100644
--- a/tests/gtest_main.cpp
+++ b/tests/gtest_main.cpp
@@ -78,15 +78,39 @@
 static constexpr const char* COLOR_GREEN  = "\033[0;32m";
 static constexpr const char* COLOR_YELLOW = "\033[0;33m";
 
-static void ColoredPrintf(const char* const color, const char* fmt, ...) {
-    va_list args;
-    va_start(args, fmt);
+static bool ShouldUseColor() {
+  const auto& gtest_color = ::testing::GTEST_FLAG(color);
+  if (gtest_color == "yes" || gtest_color == "true" || gtest_color == "t") {
+    return true;
+  }
+  if (gtest_color != "auto") {
+    return false;
+  }
 
+  bool stdout_is_tty = isatty(STDOUT_FILENO) != 0;
+  if (!stdout_is_tty) {
+    return false;
+  }
+
+  const char* const term = getenv("COLORTERM");
+  return term != nullptr && term[0] != 0;
+}
+
+static void ColoredPrintf(const char* const color, const char* fmt, ...) {
+  static const bool use_color = ShouldUseColor();
+
+  va_list args;
+  va_start(args, fmt);
+
+  if (!use_color) {
+    vprintf(fmt, args);
+  } else {
     printf("%s", color);
     vprintf(fmt, args);
     printf("%s", COLOR_RESET);
+  }
 
-    va_end(args);
+  va_end(args);
 }
 
 constexpr int DEFAULT_GLOBAL_TEST_RUN_DEADLINE_MS = 90000;