diff options
author | Devtools Arcadia <arcadia-devtools@yandex-team.ru> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging | |
download | ydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging')
5 files changed, 334 insertions, 0 deletions
diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/AWSLogging.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/AWSLogging.cpp new file mode 100644 index 0000000000..fc1b9fcc2e --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/AWSLogging.cpp @@ -0,0 +1,51 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + + +#include <aws/core/utils/logging/AWSLogging.h> +#include <aws/core/utils/logging/LogSystemInterface.h> +#include <aws/core/utils/memory/stl/AWSStack.h> + +#include <memory> + +using namespace Aws::Utils; +using namespace Aws::Utils::Logging; + +static std::shared_ptr<LogSystemInterface> AWSLogSystem(nullptr); +static std::shared_ptr<LogSystemInterface> OldLogger(nullptr); + +namespace Aws +{ +namespace Utils +{ +namespace Logging { + +void InitializeAWSLogging(const std::shared_ptr<LogSystemInterface> &logSystem) { + AWSLogSystem = logSystem; +} + +void ShutdownAWSLogging(void) { + InitializeAWSLogging(nullptr); +} + +LogSystemInterface *GetLogSystem() { + return AWSLogSystem.get(); +} + +void PushLogger(const std::shared_ptr<LogSystemInterface> &logSystem) +{ + OldLogger = AWSLogSystem; + AWSLogSystem = logSystem; +} + +void PopLogger() +{ + AWSLogSystem = OldLogger; + OldLogger = nullptr; +} + +} // namespace Logging +} // namespace Utils +} // namespace Aws
\ No newline at end of file diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/ConsoleLogSystem.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/ConsoleLogSystem.cpp new file mode 100644 index 0000000000..dec7cef82f --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/ConsoleLogSystem.cpp @@ -0,0 +1,22 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + + +#include <aws/core/utils/logging/ConsoleLogSystem.h> + +#include <iostream> + +using namespace Aws::Utils; +using namespace Aws::Utils::Logging; + +void ConsoleLogSystem::ProcessFormattedStatement(Aws::String&& statement) +{ + std::cout << statement; +} + +void ConsoleLogSystem::Flush() +{ + std::cout.flush(); +} diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp new file mode 100644 index 0000000000..7286bb6378 --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp @@ -0,0 +1,117 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + + +#include <aws/core/utils/logging/DefaultLogSystem.h> + +#include <aws/core/utils/DateTime.h> +#include <aws/core/utils/memory/stl/AWSVector.h> + +#include <fstream> + +using namespace Aws::Utils; +using namespace Aws::Utils::Logging; + +static const char* AllocationTag = "DefaultLogSystem"; +static const int BUFFERED_MSG_COUNT = 100; + +static std::shared_ptr<Aws::OFStream> MakeDefaultLogFile(const Aws::String& filenamePrefix) +{ + Aws::String newFileName = filenamePrefix + DateTime::CalculateGmtTimestampAsString("%Y-%m-%d-%H") + ".log"; + return Aws::MakeShared<Aws::OFStream>(AllocationTag, newFileName.c_str(), Aws::OFStream::out | Aws::OFStream::app); +} + +static void LogThread(DefaultLogSystem::LogSynchronizationData* syncData, const std::shared_ptr<Aws::OStream>& logFile, const Aws::String& filenamePrefix, bool rollLog) +{ + // localtime requires access to env. variables to get Timezone, which is not thread-safe + int32_t lastRolledHour = DateTime::Now().GetHour(false /*localtime*/); + std::shared_ptr<Aws::OStream> log = logFile; + + for(;;) + { + std::unique_lock<std::mutex> locker(syncData->m_logQueueMutex); + syncData->m_queueSignal.wait(locker, [&](){ return syncData->m_stopLogging == true || syncData->m_queuedLogMessages.size() > 0; } ); + + if (syncData->m_stopLogging && syncData->m_queuedLogMessages.size() == 0) + { + break; + } + + Aws::Vector<Aws::String> messages(std::move(syncData->m_queuedLogMessages)); + syncData->m_queuedLogMessages.reserve(BUFFERED_MSG_COUNT); + + locker.unlock(); + + if (messages.size() > 0) + { + if (rollLog) + { + // localtime requires access to env. variables to get Timezone, which is not thread-safe + int32_t currentHour = DateTime::Now().GetHour(false /*localtime*/); + if (currentHour != lastRolledHour) + { + log = MakeDefaultLogFile(filenamePrefix); + lastRolledHour = currentHour; + } + } + + for (const auto& msg : messages) + { + (*log) << msg; + } + + log->flush(); + } + } +} + +DefaultLogSystem::DefaultLogSystem(LogLevel logLevel, const std::shared_ptr<Aws::OStream>& logFile) : + Base(logLevel), + m_syncData(), + m_loggingThread() +{ + m_loggingThread = std::thread(LogThread, &m_syncData, logFile, "", false); +} + +DefaultLogSystem::DefaultLogSystem(LogLevel logLevel, const Aws::String& filenamePrefix) : + Base(logLevel), + m_syncData(), + m_loggingThread() +{ + m_loggingThread = std::thread(LogThread, &m_syncData, MakeDefaultLogFile(filenamePrefix), filenamePrefix, true); +} + +DefaultLogSystem::~DefaultLogSystem() +{ + { + std::lock_guard<std::mutex> locker(m_syncData.m_logQueueMutex); + m_syncData.m_stopLogging = true; + } + + m_syncData.m_queueSignal.notify_one(); + + m_loggingThread.join(); +} + +void DefaultLogSystem::ProcessFormattedStatement(Aws::String&& statement) +{ + std::unique_lock<std::mutex> locker(m_syncData.m_logQueueMutex); + m_syncData.m_queuedLogMessages.emplace_back(std::move(statement)); + if(m_syncData.m_queuedLogMessages.size() >= BUFFERED_MSG_COUNT) + { + locker.unlock(); + m_syncData.m_queueSignal.notify_one(); + } + else + { + locker.unlock(); + } +} + +void DefaultLogSystem::Flush() +{ + m_syncData.m_queueSignal.notify_one(); +} + diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp new file mode 100644 index 0000000000..41c4d7e09c --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/FormattedLogSystem.cpp @@ -0,0 +1,99 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + + +#include <aws/core/utils/logging/FormattedLogSystem.h> + +#include <aws/core/utils/DateTime.h> +#include <aws/core/utils/Array.h> + +#include <fstream> +#include <cstdarg> +#include <stdio.h> +#include <thread> + +using namespace Aws::Utils; +using namespace Aws::Utils::Logging; + +static Aws::String CreateLogPrefixLine(LogLevel logLevel, const char* tag) +{ + Aws::StringStream ss; + + switch(logLevel) + { + case LogLevel::Error: + ss << "[ERROR] "; + break; + + case LogLevel::Fatal: + ss << "[FATAL] "; + break; + + case LogLevel::Warn: + ss << "[WARN] "; + break; + + case LogLevel::Info: + ss << "[INFO] "; + break; + + case LogLevel::Debug: + ss << "[DEBUG] "; + break; + + case LogLevel::Trace: + ss << "[TRACE] "; + break; + + default: + ss << "[UNKOWN] "; + break; + } + + ss << DateTime::Now().CalculateGmtTimeWithMsPrecision() << " " << tag << " [" << std::this_thread::get_id() << "] "; + + return ss.str(); +} + +FormattedLogSystem::FormattedLogSystem(LogLevel logLevel) : + m_logLevel(logLevel) +{ +} + +void FormattedLogSystem::Log(LogLevel logLevel, const char* tag, const char* formatStr, ...) +{ + Aws::StringStream ss; + ss << CreateLogPrefixLine(logLevel, tag); + + std::va_list args; + va_start(args, formatStr); + + va_list tmp_args; //unfortunately you cannot consume a va_list twice + va_copy(tmp_args, args); //so we have to copy it + #ifdef WIN32 + const int requiredLength = _vscprintf(formatStr, tmp_args) + 1; + #else + const int requiredLength = vsnprintf(nullptr, 0, formatStr, tmp_args) + 1; + #endif + va_end(tmp_args); + + Array<char> outputBuff(requiredLength); + #ifdef WIN32 + vsnprintf_s(outputBuff.GetUnderlyingData(), requiredLength, _TRUNCATE, formatStr, args); + #else + vsnprintf(outputBuff.GetUnderlyingData(), requiredLength, formatStr, args); + #endif // WIN32 + + ss << outputBuff.GetUnderlyingData() << std::endl; + + ProcessFormattedStatement(ss.str()); + + va_end(args); +} + +void FormattedLogSystem::LogStream(LogLevel logLevel, const char* tag, const Aws::OStringStream &message_stream) +{ + ProcessFormattedStatement(CreateLogPrefixLine(logLevel, tag) + message_stream.str() + "\n"); +} diff --git a/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/LogLevel.cpp b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/LogLevel.cpp new file mode 100644 index 0000000000..9ff1bf3126 --- /dev/null +++ b/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/utils/logging/LogLevel.cpp @@ -0,0 +1,45 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0. + */ + +#include <aws/core/utils/logging/LogLevel.h> + +#include <aws/core/utils/memory/stl/AWSMap.h> +#include <aws/core/utils/memory/stl/AWSString.h> +#include <cassert> + +using namespace Aws::Utils::Logging; + +namespace Aws +{ +namespace Utils +{ +namespace Logging +{ + +Aws::String GetLogLevelName(LogLevel logLevel) +{ + switch (logLevel) + { + case LogLevel::Fatal: + return "FATAL"; + case LogLevel::Error: + return "ERROR"; + case LogLevel::Warn: + return "WARN"; + case LogLevel::Info: + return "INFO"; + case LogLevel::Debug: + return "DEBUG"; + case LogLevel::Trace: + return "TRACE"; + default: + assert(0); + return ""; + } +} + +} // namespace Logging +} // namespace Utils +} // namespace Aws |