2024-05-10 15:11:36 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
# include <iostream>
# include <fstream>
# include <aws/core/Aws.h>
# include <aws/s3/S3Client.h>
# include <aws/core/utils/logging/DefaultLogSystem.h>
# include <aws/core/utils/stream/SimpleStreamBuf.h>
2024-08-26 12:24:11 -04:00
2024-05-10 15:11:36 -04:00
/*
* This class overrides the default log system and allows temporary logging to a
* 'SimpleStreamBuf'.
*/
2024-08-26 12:24:11 -04:00
// snippet-start:[cpp.example_code.sdk_customization.override_logger.LogSystem]
2024-05-10 15:11:36 -04:00
class LogSystemOverride : public Aws : : Utils : : Logging : : DefaultLogSystem {
public :
explicit LogSystemOverride ( Aws : : Utils : : Logging : : LogLevel logLevel ,
const Aws : : String & logPrefix )
2024-08-26 12:24:11 -04:00
: DefaultLogSystem ( logLevel , logPrefix ) , mLogToStreamBuf ( false ) { }
2024-05-10 15:11:36 -04:00
const Aws : : Utils : : Stream : : SimpleStreamBuf & GetStreamBuf ( ) const {
2024-08-26 12:24:11 -04:00
return mStreamBuf ;
2024-05-10 15:11:36 -04:00
}
2024-08-26 12:24:11 -04:00
void setLogToStreamBuf ( bool logToStreamBuf ) {
mLogToStreamBuf = logToStreamBuf ;
2024-05-10 15:11:36 -04:00
}
protected :
2024-08-26 12:24:11 -04:00
void ProcessFormattedStatement ( Aws : : String & & statement ) override {
if ( mLogToStreamBuf ) {
std : : lock_guard < std : : mutex > lock ( mStreamMutex ) ;
mStreamBuf . sputn ( statement . c_str ( ) , statement . length ( ) ) ;
2024-05-10 15:11:36 -04:00
}
2024-08-26 12:24:11 -04:00
DefaultLogSystem : : ProcessFormattedStatement ( std : : move ( statement ) ) ;
2024-05-10 15:11:36 -04:00
}
private :
2024-08-26 12:24:11 -04:00
Aws : : Utils : : Stream : : SimpleStreamBuf mStreamBuf ;
2024-05-10 15:11:36 -04:00
// Use a mutex when writing to the buffer because
// ProcessFormattedStatement can be called from multiple threads.
2024-08-26 12:24:11 -04:00
std : : mutex mStreamMutex ;
std : : atomic < bool > mLogToStreamBuf ;
2024-05-10 15:11:36 -04:00
} ;
2024-08-26 12:24:11 -04:00
// snippet-end:[cpp.example_code.sdk_customization.override_logger.LogSystem]
2024-05-10 15:11:36 -04:00
/*
*
* main function
*
* Usage: 'run_override_default_logger'
*
*/
2024-08-26 12:24:11 -04:00
// snippet-start:[cpp.example_code..sdk_customization.override_logger.Use]
2024-05-10 15:11:36 -04:00
int main ( int argc , char * * argv ) {
Aws : : SDKOptions options ;
options . loggingOptions . logLevel = Aws : : Utils : : Logging : : LogLevel : : Trace ;
auto logSystemOverride = Aws : : MakeShared < LogSystemOverride > ( " AllocationTag " ,
options . loggingOptions . logLevel ,
options . loggingOptions . defaultLogPrefix ) ;
options . loggingOptions . logger_create_fn = [ logSystemOverride ] ( ) {
2024-08-26 12:24:11 -04:00
return logSystemOverride ;
2024-05-10 15:11:36 -04:00
} ;
2024-08-26 12:24:11 -04:00
Aws : : InitAPI ( options ) ; // Call Aws::InitAPI only once in an application.
2024-05-10 15:11:36 -04:00
{
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
Aws : : S3 : : S3Client s3Client ( clientConfig ) ;
2024-08-26 12:24:11 -04:00
logSystemOverride - > setLogToStreamBuf ( true ) ;
2024-05-10 15:11:36 -04:00
auto outcome = s3Client . ListBuckets ( ) ;
2024-08-26 12:24:11 -04:00
if ( ! outcome . IsSuccess ( ) ) {
std : : cerr < < " ListBuckets error: " < <
outcome . GetError ( ) . GetExceptionName ( ) < < " " < <
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
2024-05-10 15:11:36 -04:00
}
2024-08-26 12:24:11 -04:00
logSystemOverride - > setLogToStreamBuf ( false ) ;
2024-05-10 15:11:36 -04:00
std : : cout < < " Log for ListBuckets " < < std : : endl ;
std : : cout < < logSystemOverride - > GetStreamBuf ( ) . str ( ) < < std : : endl ;
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2024-08-26 12:24:11 -04:00
// snippet-end:[cpp.example_code..sdk_customization.override_logger.Use]