From 0d7cf1f9846190c92414a7fb8cea6d426e0e3884 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 13 Sep 2023 21:40:42 -0700 Subject: [PATCH] Disable text color codes when running application from XCode (close #431) --- Platforms/Apple/interface/AppleDebug.hpp | 4 +- Platforms/Apple/src/AppleDebug.mm | 33 +++++++++++-- .../Basic/interface/BasicPlatformDebug.hpp | 7 ++- .../TestFramework/src/TestingEnvironment.cpp | 48 ++++++++++++++----- 4 files changed, 72 insertions(+), 20 deletions(-) diff --git a/Platforms/Apple/interface/AppleDebug.hpp b/Platforms/Apple/interface/AppleDebug.hpp index 236770d362..47b3739ef5 100644 --- a/Platforms/Apple/interface/AppleDebug.hpp +++ b/Platforms/Apple/interface/AppleDebug.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -44,6 +44,8 @@ struct AppleDebug : public BasicPlatformDebug const char* File, // type of __FILE__ int Line, TextColor Color = TextColor::Auto); + + static bool ColoredTextSupported(); }; } // namespace Diligent diff --git a/Platforms/Apple/src/AppleDebug.mm b/Platforms/Apple/src/AppleDebug.mm index 4d1e080ee2..e3aa424798 100644 --- a/Platforms/Apple/src/AppleDebug.mm +++ b/Platforms/Apple/src/AppleDebug.mm @@ -1,4 +1,6 @@ -/* Copyright 2015-2019 Egor Yusov +/* + * Copyright 2019-2023 Diligent Graphics LLC + * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -40,6 +42,19 @@ raise( SIGTRAP ); }; +bool AppleDebug::ColoredTextSupported() +{ + static const bool StartedFromXCode = + []() + { + NSDictionary* environment = [[NSProcessInfo processInfo] environment]; + return [environment[@"OS_ACTIVITY_DT_MODE"] boolValue]; + }(); + // XCode does not support colored console + return !StartedFromXCode; +} + + void AppleDebug::OutputDebugMessage(DEBUG_MESSAGE_SEVERITY Severity, const Char* Message, const char* Function, @@ -48,10 +63,18 @@ TextColor Color) { auto msg = FormatDebugMessage(Severity, Message, Function, File, Line); - const auto* ColorCode = TextColorToTextColorCode(Severity, Color); - printf("%s%s%s\n", ColorCode, msg.c_str(), TextColorCode::Default); - // NSLog truncates the log at 1024 symbols - //NSLog(@"%s", str.c_str()); + + if (ColoredTextSupported()) + { + const auto* ColorCode = TextColorToTextColorCode(Severity, Color); + printf("%s%s%s\n", ColorCode, msg.c_str(), TextColorCode::Default); + // NSLog truncates the log at 1024 symbols + //NSLog(@"%s", str.c_str()); + } + else + { + printf("%s\n",msg.c_str()); + } } void DebugAssertionFailed(const Char* Message, const char* Function, const char* File, int Line) diff --git a/Platforms/Basic/interface/BasicPlatformDebug.hpp b/Platforms/Basic/interface/BasicPlatformDebug.hpp index e9c2395f61..32e035e740 100644 --- a/Platforms/Basic/interface/BasicPlatformDebug.hpp +++ b/Platforms/Basic/interface/BasicPlatformDebug.hpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * Copyright 2015-2019 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -70,6 +70,11 @@ struct BasicPlatformDebug int Line); static const char* TextColorToTextColorCode(DEBUG_MESSAGE_SEVERITY Severity, TextColor Color); + + static bool ColoredTextSupported() + { + return true; + } }; // Forward declarations of platform-specific debug functions diff --git a/Tests/TestFramework/src/TestingEnvironment.cpp b/Tests/TestFramework/src/TestingEnvironment.cpp index 4082677ab1..7731b4fba2 100644 --- a/Tests/TestFramework/src/TestingEnvironment.cpp +++ b/Tests/TestFramework/src/TestingEnvironment.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 Diligent Graphics LLC + * Copyright 2019-2023 Diligent Graphics LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,6 +44,10 @@ TestingEnvironment::TestingEnvironment() { VERIFY(m_pTheEnvironment == nullptr, "Testing environment object has already been initialized!"); m_pTheEnvironment = this; + if (!PlatformDebug::ColoredTextSupported()) + { + GTEST_FLAG_SET(color, "no"); + } SetDebugMessageCallback(MessageCallback); } @@ -85,7 +89,11 @@ void TestingEnvironment::SetErrorAllowance(int NumErrorsToAllow, const char* Inf m_NumAllowedErrors = NumErrorsToAllow; if (InfoMessage != nullptr) { - std::cout << TextColorCode::Cyan << InfoMessage << TextColorCode::Default; + if (PlatformDebug::ColoredTextSupported()) + std::cout << TextColorCode::Cyan; + std::cout << InfoMessage; + if (PlatformDebug::ColoredTextSupported()) + std::cout << TextColorCode::Default; } if (m_NumAllowedErrors == 0) { @@ -101,23 +109,37 @@ void TestingEnvironment::PushExpectedErrorSubstring(const char* Str, bool ClearS m_ExpectedErrorSubstrings.push_back(Str); } - const char* TestingEnvironment::GetCurrentTestStatusString() { - static constexpr char TestFailedString[] = "\033[0;91m" - "[ FAILED ]" - "\033[0;0m"; - static constexpr char TestPassedString[] = "\033[0;92m" - "[ PASSED ]" - "\033[0;0m"; - return testing::Test::HasFailure() ? TestFailedString : TestPassedString; + auto TestFailed = testing::Test::HasFailure(); + if (PlatformDebug::ColoredTextSupported()) + { + static constexpr char TestFailedString[] = "\033[0;91m" + "[ FAILED ]" + "\033[0;0m"; + static constexpr char TestPassedString[] = "\033[0;92m" + "[ PASSED ]" + "\033[0;0m"; + return TestFailed ? TestFailedString : TestPassedString; + } + else + { + return TestFailed ? "[ FAILED ]" : "[ PASSED ]"; + } } const char* TestingEnvironment::GetTestSkippedString() { - return "\033[0;32m" - "[ SKIPPED ]" - "\033[0;0m"; + if (PlatformDebug::ColoredTextSupported()) + { + return "\033[0;32m" + "[ SKIPPED ]" + "\033[0;0m"; + } + else + { + return "[ SKIPPED ]"; + } } } // namespace Testing