1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#include "tensorflow/core/platform/s3/aws_logging.h"
16#include "tensorflow/core/lib/strings/stringprintf.h"
17#include "tensorflow/core/platform/logging.h"
18#include "tensorflow/core/platform/mutex.h"
19
20#include <aws/core/Aws.h>
21#include <aws/core/utils/logging/AWSLogging.h>
22#include <aws/core/utils/logging/LogSystemInterface.h>
23
24#include <cstdarg>
25
26namespace tensorflow {
27
28AWSLogSystem::AWSLogSystem(Aws::Utils::Logging::LogLevel log_level)
29    : log_level_(log_level) {}
30
31void AWSLogSystem::Log(Aws::Utils::Logging::LogLevel log_level, const char* tag,
32                       const char* format, ...) {
33  std::va_list args;
34  va_start(args, format);
35
36  const string s = strings::Printf(format, args);
37
38  va_end(args);
39
40  LogMessage(log_level, s);
41}
42
43void AWSLogSystem::LogStream(Aws::Utils::Logging::LogLevel log_level,
44                             const char* tag,
45                             const Aws::OStringStream& message_stream) {
46  LogMessage(log_level, message_stream.rdbuf()->str().c_str());
47}
48
49void AWSLogSystem::LogMessage(Aws::Utils::Logging::LogLevel log_level,
50                              const std::string& message) {
51  if (message == "Initializing Curl library") return;
52  switch (log_level) {
53    case Aws::Utils::Logging::LogLevel::Info:
54      LOG(INFO) << message;
55      break;
56    case Aws::Utils::Logging::LogLevel::Warn:
57      LOG(WARNING) << message;
58      break;
59    case Aws::Utils::Logging::LogLevel::Error:
60      LOG(ERROR) << message;
61      break;
62    case Aws::Utils::Logging::LogLevel::Fatal:
63      LOG(FATAL) << message;
64      break;
65    default:
66      LOG(ERROR) << message;
67      break;
68  }
69}
70
71namespace {
72static const char* kAWSLoggingTag = "AWSLogging";
73
74Aws::Utils::Logging::LogLevel ParseLogLevelFromEnv() {
75  Aws::Utils::Logging::LogLevel log_level = Aws::Utils::Logging::LogLevel::Info;
76
77  const int64_t level = tensorflow::internal::MinLogLevelFromEnv();
78
79  switch (level) {
80    case INFO:
81      log_level = Aws::Utils::Logging::LogLevel::Info;
82      break;
83    case WARNING:
84      log_level = Aws::Utils::Logging::LogLevel::Warn;
85      break;
86    case ERROR:
87      log_level = Aws::Utils::Logging::LogLevel::Error;
88      break;
89    case FATAL:
90      log_level = Aws::Utils::Logging::LogLevel::Fatal;
91      break;
92    default:
93      log_level = Aws::Utils::Logging::LogLevel::Info;
94      break;
95  }
96
97  return log_level;
98}
99}  // namespace
100
101static bool initialized = false;
102static mutex s3_logging_mutex(LINKER_INITIALIZED);
103void AWSLogSystem::InitializeAWSLogging() {
104  std::lock_guard<mutex> s3_logging_lock(s3_logging_mutex);
105  if (!initialized) {
106    Aws::Utils::Logging::InitializeAWSLogging(
107        Aws::MakeShared<AWSLogSystem>(kAWSLoggingTag, ParseLogLevelFromEnv()));
108    initialized = true;
109    return;
110  }
111}
112
113void AWSLogSystem::ShutdownAWSLogging() {
114  std::lock_guard<mutex> s3_logging_lock(s3_logging_mutex);
115  if (initialized) {
116    Aws::Utils::Logging::ShutdownAWSLogging();
117    initialized = false;
118    return;
119  }
120}
121
122}  // namespace tensorflow
123