Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d92ec7

Browse files
authored
Add logger flush API
In execution environments like AWS Lambda, especially short lived functions, the logs do not get flushed fast enough. This is more prominent if the Lambda invocations are small and do not accumulate enough messages to trigger automatic flushing.
1 parent e7bb6e2 commit 4d92ec7

File tree

8 files changed

+48
-7
lines changed

8 files changed

+48
-7
lines changed

aws-cpp-sdk-core-tests/utils/logging/LoggingTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ void LogAllPossibilities(const char* tag)
6464
AWS_LOG_TRACE(tag, "test trace level");
6565
AWS_LOG_TRACE(tag, "test %s format level", "trace");
6666
AWS_LOGSTREAM_TRACE(tag, "test " << "trace " << "stream level" );
67+
68+
AWS_LOG_FLUSH();
69+
AWS_LOGSTREAM_FLUSH();
6770
}
6871

6972
void VerifyAllLogsAtOrBelow(LogLevel logLevel, const Aws::String& tag, const Aws::Vector<Aws::String>& loggedStatements)

aws-cpp-sdk-core/include/aws/core/utils/logging/ConsoleLogSystem.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ namespace Aws
4040

4141
virtual ~ConsoleLogSystem() {}
4242

43+
/**
44+
* Flushes buffered messages to stdout.
45+
*/
46+
void Flush() override;
47+
4348
protected:
4449

4550
virtual void ProcessFormattedStatement(Aws::String&& statement) override;

aws-cpp-sdk-core/include/aws/core/utils/logging/DefaultLogSystem.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,15 @@ namespace Aws
5353
* on construction.
5454
*/
5555
DefaultLogSystem(LogLevel logLevel, const Aws::String& filenamePrefix);
56+
5657
virtual ~DefaultLogSystem();
5758

59+
/**
60+
* Flushes buffered messages to the file system.
61+
* This method is thread-safe.
62+
*/
63+
void Flush() override;
64+
5865
/**
5966
* Structure containing semaphores, queue etc...
6067
*/
@@ -66,7 +73,7 @@ namespace Aws
6673
std::mutex m_logQueueMutex;
6774
std::condition_variable m_queueSignal;
6875
Aws::Vector<Aws::String> m_queuedLogMessages;
69-
std::atomic<bool> m_stopLogging;
76+
bool m_stopLogging;
7077

7178
private:
7279
LogSynchronizationData(const LogSynchronizationData& rhs) = delete;

aws-cpp-sdk-core/include/aws/core/utils/logging/LogMacros.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define AWS_LOG_INFO(tag, ...)
3838
#define AWS_LOG_DEBUG(tag, ...)
3939
#define AWS_LOG_TRACE(tag, ...)
40+
#define AWS_LOG_FLUSH()
4041

4142
#define AWS_LOGSTREAM(level, tag, streamExpression)
4243
#define AWS_LOGSTREAM_FATAL(tag, streamExpression)
@@ -45,9 +46,19 @@
4546
#define AWS_LOGSTREAM_INFO(tag, streamExpression)
4647
#define AWS_LOGSTREAM_DEBUG(tag, streamExpression)
4748
#define AWS_LOGSTREAM_TRACE(tag, streamExpression)
49+
#define AWS_LOGSTREAM_FLUSH()
4850

4951
#else
5052

53+
#define AWS_LOG_FLUSH() \
54+
{ \
55+
Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
56+
if ( logSystem ) \
57+
{ \
58+
logSystem->Flush(); \
59+
} \
60+
}
61+
5162
#define AWS_LOG(level, tag, ...) \
5263
{ \
5364
Aws::Utils::Logging::LogSystemInterface* logSystem = Aws::Utils::Logging::GetLogSystem(); \
@@ -188,4 +199,6 @@
188199
} \
189200
}
190201

202+
#define AWS_LOGSTREAM_FLUSH() AWS_LOG_FLUSH()
203+
191204
#endif // DISABLE_AWS_LOGGING

aws-cpp-sdk-core/include/aws/core/utils/logging/LogSystemInterface.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ namespace Aws
5050
* Writes the stream to the output stream.
5151
*/
5252
virtual void LogStream(LogLevel logLevel, const char* tag, const Aws::OStringStream &messageStream) = 0;
53-
53+
/**
54+
* Writes any buffered messages to the underlying device if the logger supports buffering.
55+
*/
56+
virtual void Flush() = 0;
5457
};
5558

5659
} // namespace Logging

aws-cpp-sdk-core/include/aws/core/utils/logging/android/LogcatLogSystem.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class AWS_CORE_API LogcatLogSystem : public FormattedLogSystem
3232

3333
using Base = FormattedLogSystem;
3434

35-
LogcatLogSystem(LogLevel logLevel) :
36-
Base(logLevel)
37-
{}
35+
LogcatLogSystem(LogLevel logLevel) : Base(logLevel) {}
3836

3937
virtual ~LogcatLogSystem() {}
4038

39+
void Flush() override { /* no-op android NDK does not have a flush api */ }
40+
4141
protected:
4242

4343
virtual void ProcessFormattedStatement(Aws::String&& statement) override;

aws-cpp-sdk-core/source/utils/logging/ConsoleLogSystem.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ void ConsoleLogSystem::ProcessFormattedStatement(Aws::String&& statement)
2525
{
2626
std::cout << statement;
2727
}
28+
29+
void ConsoleLogSystem::Flush()
30+
{
31+
std::cout.flush();
32+
}

aws-cpp-sdk-core/source/utils/logging/DefaultLogSystem.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static void LogThread(DefaultLogSystem::LogSynchronizationData* syncData, const
4242
for(;;)
4343
{
4444
std::unique_lock<std::mutex> locker(syncData->m_logQueueMutex);
45-
syncData->m_queueSignal.wait(locker, [&](){ return syncData->m_stopLogging.load() == true || syncData->m_queuedLogMessages.size() > 0; } );
45+
syncData->m_queueSignal.wait(locker, [&](){ return syncData->m_stopLogging == true || syncData->m_queuedLogMessages.size() > 0; } );
4646

4747
if (syncData->m_stopLogging && syncData->m_queuedLogMessages.size() == 0)
4848
{
@@ -97,7 +97,7 @@ DefaultLogSystem::~DefaultLogSystem()
9797
{
9898
{
9999
std::lock_guard<std::mutex> locker(m_syncData.m_logQueueMutex);
100-
m_syncData.m_stopLogging.store(true);
100+
m_syncData.m_stopLogging = true;
101101
}
102102

103103
m_syncData.m_queueSignal.notify_one();
@@ -120,3 +120,8 @@ void DefaultLogSystem::ProcessFormattedStatement(Aws::String&& statement)
120120
}
121121
}
122122

123+
void DefaultLogSystem::Flush()
124+
{
125+
m_syncData.m_queueSignal.notify_one();
126+
}
127+

0 commit comments

Comments
 (0)