aws_log_system.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #include <aws/core/utils/logging/AWSLogging.h>
18 
19 #include <cstdarg>
20 #include <iostream>
21 #include <mutex>
22 
23 
24 namespace Aws {
25 namespace Utils {
26 namespace Logging {
27 
28 
29 AWSLogSystem::AWSLogSystem(Aws::Utils::Logging::LogLevel log_level)
30 : configured_log_level_(log_level)
31 {
32 }
33 
34 Aws::Utils::Logging::LogLevel AWSLogSystem::GetLogLevel() const
35 {
36  return configured_log_level_;
37 }
38 
39 void AWSLogSystem::SetLogLevel(Aws::Utils::Logging::LogLevel log_level)
40 {
41  configured_log_level_.store(log_level);
42 }
43 
44 // NOLINTNEXTLINE(cert-dcl50-cpp)
45 void AWSLogSystem::Log(Aws::Utils::Logging::LogLevel log_level, const char * tag,
46  const char * format, ...)
47 {
48  // Do not log for all log_levels greater than the configured log level.
49  //
50  // Aws::Utils::Logging::LogLevel values are here -
51  // https://sdk.amazonaws.com/cpp/api/0.14.3/aws-cpp-sdk-core_2include_2aws_2core_2utils_2logging_2_log_level_8h_source.html
52  if (log_level > configured_log_level_) {
53  return;
54  }
55 
56  // Max log line size DEFAULT_LOG_MESSAGE_SIZE_BYTES(1024) bytes.
58 
59  va_list argptr;
60  va_start(argptr, format);
61  vsnprintf(buf, sizeof(buf), format, argptr);
62  va_end(argptr);
63 
64  const std::string s(buf);
65  LogMessage(log_level, tag, s);
66 }
67 
68 void AWSLogSystem::LogStream(Aws::Utils::Logging::LogLevel log_level, const char * tag,
69  const Aws::OStringStream & message_stream)
70 {
71  if (log_level > configured_log_level_) {
72  return;
73  }
74 
75  LogMessage(log_level, tag, message_stream.rdbuf()->str().c_str());
76 }
77 
78 void AWSLogSystem::LogMessage(Aws::Utils::Logging::LogLevel log_level, const char * tag,
79  const std::string & message)
80 {
81  const char * normalized_tag = (tag == nullptr) ? "" : tag;
82  switch (log_level) {
83  case Aws::Utils::Logging::LogLevel::Info:
84  LogInfo(normalized_tag, message);
85  break;
86  case Aws::Utils::Logging::LogLevel::Debug:
87  LogDebug(normalized_tag, message);
88  break;
89  case Aws::Utils::Logging::LogLevel::Warn:
90  LogWarn(normalized_tag, message);
91  break;
92  case Aws::Utils::Logging::LogLevel::Error:
93  LogError(normalized_tag, message);
94  break;
95  case Aws::Utils::Logging::LogLevel::Fatal:
96  LogFatal(normalized_tag, message);
97  break;
98  case Aws::Utils::Logging::LogLevel::Trace:
99  LogTrace(normalized_tag, message);
100  break;
101  default:
102  LogError(normalized_tag, message);
103  break;
104  }
105 }
106 
107 } // namespace Logging
108 } // namespace Utils
109 } // namespace Aws
virtual void LogDebug(const char *tag, const std::string &message)=0
#define DEFAULT_LOG_MESSAGE_SIZE_BYTES
virtual void LogInfo(const char *tag, const std::string &message)=0
virtual void LogFatal(const char *tag, const std::string &message)=0
void Log(Aws::Utils::Logging::LogLevel log_level, const char *tag, const char *format,...) override
void LogMessage(Aws::Utils::Logging::LogLevel log_level, const char *tag, const std::string &message)
Aws::Utils::Logging::LogLevel GetLogLevel() const override
AWSLogSystem(Aws::Utils::Logging::LogLevel log_level)
virtual void LogTrace(const char *tag, const std::string &message)=0
void LogStream(Aws::Utils::Logging::LogLevel log_level, const char *tag, const Aws::OStringStream &message_stream) override
virtual void LogWarn(const char *tag, const std::string &message)=0
void SetLogLevel(Aws::Utils::Logging::LogLevel log_level)
std::atomic< Aws::Utils::Logging::LogLevel > configured_log_level_
virtual void LogError(const char *tag, const std::string &message)=0


aws_common
Author(s): AWS RoboMaker
autogenerated on Sat Mar 6 2021 03:11:38