Add cvar for aws log level (#5507)

* Add cvar for aws log level

Signed-off-by: onecent1101 <liug@amazon.com>

* Remove old crysystem registered cmd and add safeguard to get cvar from console

Signed-off-by: onecent1101 <liug@amazon.com>

* Suppress error for client auth unit test as logging was routed to warning before

Signed-off-by: onecent1101 <liug@amazon.com>
This commit is contained in:
Vincent Liu
2021-11-16 12:14:05 -08:00
committed by GitHub
parent 655f082908
commit 59b12a6ec7
10 changed files with 244 additions and 33 deletions
-16
View File
@@ -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<int> logVar = AZ::Environment::CreateVariable<int>(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].");
}
//////////////////////////////////////////////////////////////////////////
@@ -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()
@@ -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
)
@@ -10,6 +10,8 @@
#include <AWSNativeSDKInit/AWSLogSystemInterface.h>
#include <AzCore/base.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/Module/Environment.h>
#include <stdarg.h>
@@ -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<int>(logLevelEnvVar);
if (logVar)
if (auto console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
{
newLevel = (Aws::Utils::Logging::LogLevel) *logVar;
int awsLogLevel = -1;
console->GetCvarValue("bg_awsLogLevel", awsLogLevel);
if (awsLogLevel >= 0)
{
newLevel = static_cast<Aws::Utils::Logging::LogLevel>(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:
@@ -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]()
@@ -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 <AzCore/Console/Console.h>
#include <AzCore/Console/IConsole.h>
#include <AzCore/Interface/Interface.h>
#include <AzCore/std/smart_ptr/unique_ptr.h>
#include <AzCore/UnitTest/TestTypes.h>
#include <AWSNativeSDKInit/AWSLogSystemInterface.h>
#include <aws/core/utils/logging/LogLevel.h>
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<AZ::IConsole>::Get())
{
m_console = AZStd::make_unique<AZ::Console>();
m_console->LinkDeferredFunctors(AZ::ConsoleFunctorBase::GetDeferredHead());
AZ::Interface<AZ::IConsole>::Register(m_console.get());
}
}
void TearDown() override
{
if (m_console)
{
AZ::Interface<AZ::IConsole>::Unregister(m_console.get());
m_console.reset();
}
BusDisconnect();
}
bool m_error = false;
bool m_warning = false;
bool m_printf = false;
private:
AZStd::unique_ptr<AZ::Console> 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<AZ::IConsole>::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<AZ::IConsole>::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<AZ::IConsole>::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);
}
@@ -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 <AzTest/AzTest.h>
AZ_UNIT_TEST_HOOK(DEFAULT_UNIT_TEST_ENV);