• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations 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 
26 namespace tensorflow {
27 
AWSLogSystem(Aws::Utils::Logging::LogLevel log_level)28 AWSLogSystem::AWSLogSystem(Aws::Utils::Logging::LogLevel log_level)
29     : log_level_(log_level) {}
30 
Log(Aws::Utils::Logging::LogLevel log_level,const char * tag,const char * format,...)31 void 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 
LogStream(Aws::Utils::Logging::LogLevel log_level,const char * tag,const Aws::OStringStream & message_stream)43 void 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 
LogMessage(Aws::Utils::Logging::LogLevel log_level,const std::string & message)49 void 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 
71 namespace {
72 
73 // Taken from tensorflow/core/platform/default/logging.cc
ParseInteger(const char * str,size_t size)74 int ParseInteger(const char* str, size_t size) {
75   string integer_str(str, size);
76   std::istringstream ss(integer_str);
77   int level = 0;
78   ss >> level;
79   return level;
80 }
81 
82 // Taken from tensorflow/core/platform/default/logging.cc
LogLevelStrToInt(const char * tf_env_var_val)83 int64 LogLevelStrToInt(const char* tf_env_var_val) {
84   if (tf_env_var_val == nullptr) {
85     return 0;
86   }
87   return ParseInteger(tf_env_var_val, strlen(tf_env_var_val));
88 }
89 
90 static const char* kAWSLoggingTag = "AWSLogging";
91 
ParseLogLevelFromEnv()92 Aws::Utils::Logging::LogLevel ParseLogLevelFromEnv() {
93   Aws::Utils::Logging::LogLevel log_level = Aws::Utils::Logging::LogLevel::Info;
94 
95   const int64_t level = getenv("AWS_LOG_LEVEL")
96                             ? LogLevelStrToInt(getenv("AWS_LOG_LEVEL"))
97                             : tensorflow::internal::MinLogLevelFromEnv();
98 
99   switch (level) {
100     case INFO:
101       log_level = Aws::Utils::Logging::LogLevel::Info;
102       break;
103     case WARNING:
104       log_level = Aws::Utils::Logging::LogLevel::Warn;
105       break;
106     case ERROR:
107       log_level = Aws::Utils::Logging::LogLevel::Error;
108       break;
109     case FATAL:
110       log_level = Aws::Utils::Logging::LogLevel::Fatal;
111       break;
112     default:
113       log_level = Aws::Utils::Logging::LogLevel::Info;
114       break;
115   }
116 
117   return log_level;
118 }
119 }  // namespace
120 
121 static bool initialized = false;
122 static mutex s3_logging_mutex(LINKER_INITIALIZED);
InitializeAWSLogging()123 void AWSLogSystem::InitializeAWSLogging() {
124   std::lock_guard<mutex> s3_logging_lock(s3_logging_mutex);
125   if (!initialized) {
126     Aws::Utils::Logging::InitializeAWSLogging(
127         Aws::MakeShared<AWSLogSystem>(kAWSLoggingTag, ParseLogLevelFromEnv()));
128     initialized = true;
129     return;
130   }
131 }
132 
ShutdownAWSLogging()133 void AWSLogSystem::ShutdownAWSLogging() {
134   std::lock_guard<mutex> s3_logging_lock(s3_logging_mutex);
135   if (initialized) {
136     Aws::Utils::Logging::ShutdownAWSLogging();
137     initialized = false;
138     return;
139   }
140 }
141 
142 }  // namespace tensorflow
143