From 59b12a6ec7e8e2bce2c6a2669fc71bca6fd673ce Mon Sep 17 00:00:00 2001 From: Vincent Liu <5900509+onecent1101@users.noreply.github.com> Date: Tue, 16 Nov 2021 12:14:05 -0800 Subject: [PATCH] Add cvar for aws log level (#5507) * Add cvar for aws log level Signed-off-by: onecent1101 * Remove old crysystem registered cmd and add safeguard to get cvar from console Signed-off-by: onecent1101 * Suppress error for client auth unit test as logging was routed to warning before Signed-off-by: onecent1101 --- Code/Legacy/CrySystem/SystemInit.cpp | 16 -- Code/Tools/AWSNativeSDKInit/CMakeLists.txt | 27 +++ .../aws_native_sdk_init_tests_files.cmake | 12 ++ .../source/AWSLogSystemInterface.cpp | 26 +-- .../source/AWSNativeSDKInit.cpp | 4 +- .../tests/AWSLogSystemInterfaceTest.cpp | 169 ++++++++++++++++++ .../tests/AWSNativeSDKInitTest.cpp | 11 ++ .../AWSCognitoAuthorizationControllerTest.cpp | 6 + .../Code/AWSGameLiftClient/CMakeLists.txt | 2 - .../Source/AWSGameLiftClientManager.cpp | 4 +- 10 files changed, 244 insertions(+), 33 deletions(-) create mode 100644 Code/Tools/AWSNativeSDKInit/aws_native_sdk_init_tests_files.cmake create mode 100644 Code/Tools/AWSNativeSDKInit/tests/AWSLogSystemInterfaceTest.cpp create mode 100644 Code/Tools/AWSNativeSDKInit/tests/AWSNativeSDKInitTest.cpp diff --git a/Code/Legacy/CrySystem/SystemInit.cpp b/Code/Legacy/CrySystem/SystemInit.cpp index 8a0586fb13..5cb60f2cdf 100644 --- a/Code/Legacy/CrySystem/SystemInit.cpp +++ b/Code/Legacy/CrySystem/SystemInit.cpp @@ -1225,20 +1225,6 @@ static AZStd::string ConcatPath(const char* szPart1, const char* szPart2) return ret; } -// Helper to maintain backwards compatibility with our CVar but not force our new code to -// pull in CryCommon by routing through an environment variable -void CmdSetAwsLogLevel(IConsoleCmdArgs* pArgs) -{ - static const char* const logLevelEnvVar = "sys_SetLogLevel"; - static AZ::EnvironmentVariable logVar = AZ::Environment::CreateVariable(logLevelEnvVar); - if (pArgs->GetArgCount() > 1) - { - int logLevel = atoi(pArgs->GetArg(1)); - *logVar = logLevel; - AZ_TracePrintf("AWSLogging", "Log level set to %d", *logVar); - } -} - ////////////////////////////////////////////////////////////////////////// void CSystem::CreateSystemVars() { @@ -1601,8 +1587,6 @@ void CSystem::CreateSystemVars() // Since the UI Canvas Editor is incomplete, we have a variable to enable it. // By default it is now enabled. Modify system.cfg or game.cfg to disable it REGISTER_INT("sys_enableCanvasEditor", 1, VF_NULL, "Enables the UI Canvas Editor"); - - REGISTER_COMMAND("sys_SetLogLevel", CmdSetAwsLogLevel, 0, "Set AWS log level [0 - 6]."); } ////////////////////////////////////////////////////////////////////////// diff --git a/Code/Tools/AWSNativeSDKInit/CMakeLists.txt b/Code/Tools/AWSNativeSDKInit/CMakeLists.txt index d571f0d9e3..57ee31d30d 100644 --- a/Code/Tools/AWSNativeSDKInit/CMakeLists.txt +++ b/Code/Tools/AWSNativeSDKInit/CMakeLists.txt @@ -24,3 +24,30 @@ ly_add_target( 3rdParty::AWSNativeSDK::Core AZ::AzCore ) + +################################################################################ +# Tests +################################################################################ +if(PAL_TRAIT_BUILD_TESTS_SUPPORTED) + ly_add_target( + NAME AWSNativeSDKInit.Tests ${PAL_TRAIT_TEST_TARGET_TYPE} + NAMESPACE AZ + FILES_CMAKE + aws_native_sdk_init_tests_files.cmake + INCLUDE_DIRECTORIES + PRIVATE + include + tests + source + BUILD_DEPENDENCIES + PRIVATE + AZ::AzCore + AZ::AzFramework + AZ::AzTest + AZ::AWSNativeSDKInit + 3rdParty::AWSNativeSDK::Core + ) + ly_add_googletest( + NAME AZ::AWSNativeSDKInit.Tests + ) +endif() diff --git a/Code/Tools/AWSNativeSDKInit/aws_native_sdk_init_tests_files.cmake b/Code/Tools/AWSNativeSDKInit/aws_native_sdk_init_tests_files.cmake new file mode 100644 index 0000000000..9029a1198a --- /dev/null +++ b/Code/Tools/AWSNativeSDKInit/aws_native_sdk_init_tests_files.cmake @@ -0,0 +1,12 @@ +# +# Copyright (c) Contributors to the Open 3D Engine Project. +# For complete copyright and license terms please see the LICENSE at the root of this distribution. +# +# SPDX-License-Identifier: Apache-2.0 OR MIT +# +# + +set(FILES + tests/AWSLogSystemInterfaceTest.cpp + tests/AWSNativeSDKInitTest.cpp +) diff --git a/Code/Tools/AWSNativeSDKInit/source/AWSLogSystemInterface.cpp b/Code/Tools/AWSNativeSDKInit/source/AWSLogSystemInterface.cpp index 113e513743..0ccfc43ba4 100644 --- a/Code/Tools/AWSNativeSDKInit/source/AWSLogSystemInterface.cpp +++ b/Code/Tools/AWSNativeSDKInit/source/AWSLogSystemInterface.cpp @@ -10,6 +10,8 @@ #include #include +#include +#include #include #include @@ -24,6 +26,9 @@ AZ_POP_DISABLE_WARNING namespace AWSNativeSDKInit { + AZ_CVAR(int, bg_awsLogLevel, -1, nullptr, AZ::ConsoleFunctorFlags::Null, + "AWSLogLevel used to control verbosity of logging system. Off = 0, Fatal = 1, Error = 2, Warn = 3, Info = 4, Debug = 5, Trace = 6"); + const char* AWSLogSystemInterface::AWS_API_LOG_PREFIX = "AwsApi-"; const int AWSLogSystemInterface::MAX_MESSAGE_LENGTH = 4096; const char* AWSLogSystemInterface::MESSAGE_FORMAT = "[AWS] %s - %s"; @@ -40,15 +45,16 @@ namespace AWSNativeSDKInit Aws::Utils::Logging::LogLevel AWSLogSystemInterface::GetLogLevel() const { Aws::Utils::Logging::LogLevel newLevel = m_logLevel; - static const char* const logLevelEnvVar = "sys_SetLogLevel"; - auto logVar = AZ::Environment::FindVariable(logLevelEnvVar); - - if (logVar) + if (auto console = AZ::Interface::Get(); console != nullptr) { - newLevel = (Aws::Utils::Logging::LogLevel) *logVar; + int awsLogLevel = -1; + console->GetCvarValue("bg_awsLogLevel", awsLogLevel); + if (awsLogLevel >= 0) + { + newLevel = static_cast(awsLogLevel); + } } - - return newLevel != m_logLevel ? newLevel : m_logLevel; + return newLevel; } /** @@ -78,14 +84,12 @@ namespace AWSNativeSDKInit */ void AWSLogSystemInterface::LogStream(Aws::Utils::Logging::LogLevel logLevel, const char* tag, const Aws::OStringStream &messageStream) { - if(!ShouldLog(logLevel)) { return; } ForwardAwsApiLogMessage(logLevel, tag, messageStream.str().c_str()); - } bool AWSLogSystemInterface::ShouldLog(Aws::Utils::Logging::LogLevel logLevel) @@ -93,7 +97,7 @@ namespace AWSNativeSDKInit #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK) Aws::Utils::Logging::LogLevel newLevel = GetLogLevel(); - if (newLevel > Aws::Utils::Logging::LogLevel::Info && newLevel <= Aws::Utils::Logging::LogLevel::Trace && newLevel != m_logLevel) + if (newLevel != m_logLevel) { SetLogLevel(newLevel); } @@ -124,7 +128,7 @@ namespace AWSNativeSDKInit break; case Aws::Utils::Logging::LogLevel::Error: - AZ::Debug::Trace::Instance().Warning(__FILE__, __LINE__, AZ_FUNCTION_SIGNATURE, AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message); + AZ::Debug::Trace::Instance().Error(__FILE__, __LINE__, AZ_FUNCTION_SIGNATURE, AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message); break; case Aws::Utils::Logging::LogLevel::Warn: diff --git a/Code/Tools/AWSNativeSDKInit/source/AWSNativeSDKInit.cpp b/Code/Tools/AWSNativeSDKInit/source/AWSNativeSDKInit.cpp index 815fc1bbf0..ca63859945 100644 --- a/Code/Tools/AWSNativeSDKInit/source/AWSNativeSDKInit.cpp +++ b/Code/Tools/AWSNativeSDKInit/source/AWSNativeSDKInit.cpp @@ -64,10 +64,10 @@ namespace AWSNativeSDKInit { #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK) Aws::Utils::Logging::LogLevel logLevel; -#ifdef _DEBUG +#if defined(AZ_DEBUG_BUILD) || defined(AZ_PROFILE_BUILD) logLevel = Aws::Utils::Logging::LogLevel::Warn; #else - logLevel = Aws::Utils::Logging::LogLevel::Warn; + logLevel = Aws::Utils::Logging::LogLevel::Error; #endif m_awsSDKOptions.loggingOptions.logLevel = logLevel; m_awsSDKOptions.loggingOptions.logger_create_fn = [logLevel]() diff --git a/Code/Tools/AWSNativeSDKInit/tests/AWSLogSystemInterfaceTest.cpp b/Code/Tools/AWSNativeSDKInit/tests/AWSLogSystemInterfaceTest.cpp new file mode 100644 index 0000000000..02b6cbebc1 --- /dev/null +++ b/Code/Tools/AWSNativeSDKInit/tests/AWSLogSystemInterfaceTest.cpp @@ -0,0 +1,169 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include +#include +#include +#include +#include + +#include + +#include + +using namespace AWSNativeSDKInit; + +class AWSLogSystemInterfaceTest + : public UnitTest::ScopedAllocatorSetupFixture + , public AZ::Debug::TraceMessageBus::Handler +{ +public: + bool OnPreAssert(const char*, int, const char*, const char*) override + { + return true; + } + + bool OnPreError(const char*, const char*, int, const char*, const char*) override + { + m_error = true; + return true; + } + + bool OnPreWarning(const char*, const char*, int, const char*, const char*) override + { + m_warning = true; + return true; + } + + bool OnPrintf(const char*, const char*) override + { + m_printf = true; + return true; + } + + void SetUp() override + { + BusConnect(); + if (!AZ::Interface::Get()) + { + m_console = AZStd::make_unique(); + m_console->LinkDeferredFunctors(AZ::ConsoleFunctorBase::GetDeferredHead()); + AZ::Interface::Register(m_console.get()); + } + } + + void TearDown() override + { + if (m_console) + { + AZ::Interface::Unregister(m_console.get()); + m_console.reset(); + } + BusDisconnect(); + } + + bool m_error = false; + bool m_warning = false; + bool m_printf = false; + +private: + AZStd::unique_ptr m_console; +}; + +TEST_F(AWSLogSystemInterfaceTest, LogStream_LogFatalMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Fatal, "test", testString); + ASSERT_TRUE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_FALSE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_LogErrorMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Error, "test", testString); + ASSERT_TRUE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_FALSE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_LogWarningMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Warn, "test", testString); + ASSERT_FALSE(m_error); + ASSERT_TRUE(m_warning); + ASSERT_FALSE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_LogInfoMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Info, "test", testString); + ASSERT_FALSE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_TRUE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_LogDebugMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Debug, "test", testString); + ASSERT_FALSE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_TRUE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_LogTraceMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Trace, "test", testString); + ASSERT_FALSE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_TRUE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_OverrideWarnAndLogInfoMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + AZ::Interface::Get()->PerformCommand("bg_awsLogLevel 3"); + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Info, "test", testString); + ASSERT_FALSE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_FALSE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_OverrideWarnAndLogeErrorMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + AZ::Interface::Get()->PerformCommand("bg_awsLogLevel 3"); + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Error, "test", testString); + ASSERT_TRUE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_FALSE(m_printf); +} + +TEST_F(AWSLogSystemInterfaceTest, LogStream_OverrideOffAndLogInfoMessage_GetExpectedNotification) +{ + AWSLogSystemInterface logSystem(Aws::Utils::Logging::LogLevel::Trace); + Aws::OStringStream testString; + AZ::Interface::Get()->PerformCommand("bg_awsLogLevel 0"); + logSystem.LogStream(Aws::Utils::Logging::LogLevel::Info, "test", testString); + ASSERT_FALSE(m_error); + ASSERT_FALSE(m_warning); + ASSERT_FALSE(m_printf); +} diff --git a/Code/Tools/AWSNativeSDKInit/tests/AWSNativeSDKInitTest.cpp b/Code/Tools/AWSNativeSDKInit/tests/AWSNativeSDKInitTest.cpp new file mode 100644 index 0000000000..40217ff9bc --- /dev/null +++ b/Code/Tools/AWSNativeSDKInit/tests/AWSNativeSDKInitTest.cpp @@ -0,0 +1,11 @@ +/* + * Copyright (c) Contributors to the Open 3D Engine Project. + * For complete copyright and license terms please see the LICENSE at the root of this distribution. + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + * + */ + +#include + +AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV); diff --git a/Gems/AWSClientAuth/Code/Tests/Authorization/AWSCognitoAuthorizationControllerTest.cpp b/Gems/AWSClientAuth/Code/Tests/Authorization/AWSCognitoAuthorizationControllerTest.cpp index 9f31891512..2269a2340a 100644 --- a/Gems/AWSClientAuth/Code/Tests/Authorization/AWSCognitoAuthorizationControllerTest.cpp +++ b/Gems/AWSClientAuth/Code/Tests/Authorization/AWSCognitoAuthorizationControllerTest.cpp @@ -140,7 +140,9 @@ TEST_F(AWSCognitoAuthorizationControllerTest, RequestAWSCredentials_Fail_GetIdEr EXPECT_CALL(*m_cognitoIdentityClientMock, GetCredentialsForIdentity(testing::_)).Times(0); EXPECT_CALL(m_awsCognitoAuthorizationNotificationsBusMock, OnRequestAWSCredentialsSuccess(testing::_)).Times(0); EXPECT_CALL(m_awsCognitoAuthorizationNotificationsBusMock, OnRequestAWSCredentialsFail(testing::_)).Times(1); + AZ_TEST_START_TRACE_SUPPRESSION; m_mockController->RequestAWSCredentialsAsync(); + AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT; } TEST_F(AWSCognitoAuthorizationControllerTest, RequestAWSCredentials_Fail_GetCredentialsForIdentityError) @@ -174,7 +176,9 @@ TEST_F(AWSCognitoAuthorizationControllerTest, RequestAWSCredentials_Fail_GetCred EXPECT_CALL(*m_cognitoIdentityClientMock, GetCredentialsForIdentity(testing::_)).Times(1).WillOnce(testing::Return(outcome)); EXPECT_CALL(m_awsCognitoAuthorizationNotificationsBusMock, OnRequestAWSCredentialsSuccess(testing::_)).Times(0); EXPECT_CALL(m_awsCognitoAuthorizationNotificationsBusMock, OnRequestAWSCredentialsFail(testing::_)).Times(1); + AZ_TEST_START_TRACE_SUPPRESSION; m_mockController->RequestAWSCredentialsAsync(); + AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT; } TEST_F(AWSCognitoAuthorizationControllerTest, AddRemoveLogins_Succuess) @@ -331,8 +335,10 @@ TEST_F(AWSCognitoAuthorizationControllerTest, GetCredentialsProvider_NoPersisted EXPECT_CALL(*m_cognitoIdentityClientMock, GetCredentialsForIdentity(testing::_)).Times(0); std::shared_ptr actualCredentialsProvider; + AZ_TEST_START_TRACE_SUPPRESSION; AWSCore::AWSCredentialRequestBus::BroadcastResult( actualCredentialsProvider, &AWSCore::AWSCredentialRequests::GetCredentialsProvider); + AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT; EXPECT_TRUE(actualCredentialsProvider == nullptr); } diff --git a/Gems/AWSGameLift/Code/AWSGameLiftClient/CMakeLists.txt b/Gems/AWSGameLift/Code/AWSGameLiftClient/CMakeLists.txt index 1fab09e9f4..ab85e89f75 100644 --- a/Gems/AWSGameLift/Code/AWSGameLiftClient/CMakeLists.txt +++ b/Gems/AWSGameLift/Code/AWSGameLiftClient/CMakeLists.txt @@ -6,8 +6,6 @@ # # -set(awsgameliftclient_compile_definition $,AWSGAMELIFT_RELEASE,AWSGAMELIFT_DEV>) - ly_add_target( NAME AWSGameLift.Client.Static STATIC NAMESPACE Gem diff --git a/Gems/AWSGameLift/Code/AWSGameLiftClient/Source/AWSGameLiftClientManager.cpp b/Gems/AWSGameLift/Code/AWSGameLiftClient/Source/AWSGameLiftClientManager.cpp index 4ee2d31ebf..a071b8f859 100644 --- a/Gems/AWSGameLift/Code/AWSGameLiftClient/Source/AWSGameLiftClientManager.cpp +++ b/Gems/AWSGameLift/Code/AWSGameLiftClient/Source/AWSGameLiftClientManager.cpp @@ -33,7 +33,7 @@ namespace AWSGameLift { -#if defined(AWSGAMELIFT_DEV) +#if defined(AZ_DEBUG_BUILD) || defined(AZ_PROFILE_BUILD) AZ_CVAR(AZ::CVarFixedString, cl_gameliftLocalEndpoint, "", nullptr, AZ::ConsoleFunctorFlags::Null, "The local endpoint to test with GameLiftLocal SDK."); #endif @@ -87,7 +87,7 @@ namespace AWSGameLift // Set up client endpoint or region AZStd::string localEndpoint = ""; -#if defined(AWSGAMELIFT_DEV) +#if defined(AZ_DEBUG_BUILD) || defined(AZ_PROFILE_BUILD) localEndpoint = static_cast(cl_gameliftLocalEndpoint); #endif if (!localEndpoint.empty())