AWSLogSystemInterface.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AWSNativeSDKInit/AWSLogSystemInterface.h>
  9. #include <AzCore/base.h>
  10. #include <AzCore/Console/IConsole.h>
  11. #include <AzCore/Interface/Interface.h>
  12. #include <AzCore/Module/Environment.h>
  13. #include <stdarg.h>
  14. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  15. AZ_PUSH_DISABLE_WARNING(4251 4996, "-Wunknown-warning-option")
  16. #include <aws/core/utils/logging/AWSLogging.h>
  17. #include <aws/core/utils/logging/DefaultLogSystem.h>
  18. #include <aws/core/utils/logging/ConsoleLogSystem.h>
  19. AZ_POP_DISABLE_WARNING
  20. #endif
  21. namespace AWSNativeSDKInit
  22. {
  23. AZ_CVAR(int, bg_awsLogLevel, -1, nullptr, AZ::ConsoleFunctorFlags::Null,
  24. "AWSLogLevel used to control verbosity of logging system. Off = 0, Fatal = 1, Error = 2, Warn = 3, Info = 4, Debug = 5, Trace = 6");
  25. const char* AWSLogSystemInterface::AWS_API_LOG_PREFIX = "AwsApi-";
  26. const int AWSLogSystemInterface::MAX_MESSAGE_LENGTH = 4096;
  27. const char* AWSLogSystemInterface::MESSAGE_FORMAT = "[AWS] %s - %s";
  28. const char* AWSLogSystemInterface::ERROR_WINDOW_NAME = "AwsNativeSDK";
  29. AWSLogSystemInterface::AWSLogSystemInterface(Aws::Utils::Logging::LogLevel logLevel)
  30. : m_logLevel(logLevel)
  31. {
  32. }
  33. /**
  34. * Gets the currently configured log level for this logger.
  35. */
  36. Aws::Utils::Logging::LogLevel AWSLogSystemInterface::GetLogLevel() const
  37. {
  38. Aws::Utils::Logging::LogLevel newLevel = m_logLevel;
  39. if (auto console = AZ::Interface<AZ::IConsole>::Get(); console != nullptr)
  40. {
  41. int awsLogLevel = -1;
  42. console->GetCvarValue("bg_awsLogLevel", awsLogLevel);
  43. if (awsLogLevel >= 0)
  44. {
  45. newLevel = static_cast<Aws::Utils::Logging::LogLevel>(awsLogLevel);
  46. }
  47. }
  48. return newLevel;
  49. }
  50. /**
  51. * Does a printf style output to the output stream. Don't use this, it's unsafe. See LogStream
  52. */
  53. void AWSLogSystemInterface::Log(Aws::Utils::Logging::LogLevel logLevel, const char* tag, const char* formatStr, ...)
  54. {
  55. if (!ShouldLog(logLevel))
  56. {
  57. return;
  58. }
  59. char message[MAX_MESSAGE_LENGTH];
  60. va_list mark;
  61. va_start(mark, formatStr);
  62. azvsnprintf(message, MAX_MESSAGE_LENGTH, formatStr, mark);
  63. va_end(mark);
  64. ForwardAwsApiLogMessage(logLevel, tag, message);
  65. }
  66. /**
  67. * Writes the stream to the output stream.
  68. */
  69. void AWSLogSystemInterface::LogStream(Aws::Utils::Logging::LogLevel logLevel, const char* tag, const Aws::OStringStream &messageStream)
  70. {
  71. if(!ShouldLog(logLevel))
  72. {
  73. return;
  74. }
  75. ForwardAwsApiLogMessage(logLevel, tag, messageStream.str().c_str());
  76. }
  77. bool AWSLogSystemInterface::ShouldLog(Aws::Utils::Logging::LogLevel logLevel)
  78. {
  79. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  80. Aws::Utils::Logging::LogLevel newLevel = GetLogLevel();
  81. if (newLevel != m_logLevel)
  82. {
  83. SetLogLevel(newLevel);
  84. }
  85. #endif
  86. return (logLevel <= m_logLevel);
  87. }
  88. void AWSLogSystemInterface::SetLogLevel(Aws::Utils::Logging::LogLevel newLevel)
  89. {
  90. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  91. Aws::Utils::Logging::ShutdownAWSLogging();
  92. Aws::Utils::Logging::InitializeAWSLogging(Aws::MakeShared<AWSLogSystemInterface>("AWS", newLevel));
  93. m_logLevel = newLevel;
  94. #endif
  95. }
  96. void AWSLogSystemInterface::ForwardAwsApiLogMessage(Aws::Utils::Logging::LogLevel logLevel, const char* tag, const char* message)
  97. {
  98. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  99. switch (logLevel)
  100. {
  101. case Aws::Utils::Logging::LogLevel::Off:
  102. break;
  103. case Aws::Utils::Logging::LogLevel::Fatal:
  104. AZ::Debug::Trace::Instance().Error(__FILE__, __LINE__, AZ_FUNCTION_SIGNATURE, AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message);
  105. break;
  106. case Aws::Utils::Logging::LogLevel::Error:
  107. AZ::Debug::Trace::Instance().Error(__FILE__, __LINE__, AZ_FUNCTION_SIGNATURE, AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message);
  108. break;
  109. case Aws::Utils::Logging::LogLevel::Warn:
  110. AZ::Debug::Trace::Instance().Warning(__FILE__, __LINE__, AZ_FUNCTION_SIGNATURE, AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message);
  111. break;
  112. case Aws::Utils::Logging::LogLevel::Info:
  113. AZ::Debug::Trace::Instance().Printf(AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message);
  114. break;
  115. case Aws::Utils::Logging::LogLevel::Debug:
  116. AZ::Debug::Trace::Instance().Printf(AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message);
  117. break;
  118. case Aws::Utils::Logging::LogLevel::Trace:
  119. AZ::Debug::Trace::Instance().Printf(AWSLogSystemInterface::ERROR_WINDOW_NAME, MESSAGE_FORMAT, tag, message);
  120. break;
  121. default:
  122. break;
  123. }
  124. #endif
  125. }
  126. void AWSLogSystemInterface::Flush()
  127. {
  128. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  129. // No-op AZ Debug Trace doesn't have a flush API
  130. #endif
  131. }
  132. }