Skip to content

Commit

Permalink
Disable text color codes when running application from XCode (close #431
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TheMostDiligent committed Sep 14, 2023
1 parent bc9c75c commit 0d7cf1f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 20 deletions.
4 changes: 3 additions & 1 deletion Platforms/Apple/interface/AppleDebug.hpp
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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
33 changes: 28 additions & 5 deletions Platforms/Apple/src/AppleDebug.mm
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion Platforms/Basic/interface/BasicPlatformDebug.hpp
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -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
Expand Down
48 changes: 35 additions & 13 deletions Tests/TestFramework/src/TestingEnvironment.cpp
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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)
{
Expand All @@ -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
Expand Down

0 comments on commit 0d7cf1f

Please sign in to comment.