• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2021 The Android Open Source Project
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 #ifdef __ANDROID__
16 #include <android-base/logging.h>
17 #include <log/log.h>
18 #endif
19 
20 #include <ditto/logger.h>
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #include <iostream>
27 #include <sstream>
28 #include <vector>
29 
30 namespace dittosuite {
31 
GetInstance()32 Logger& Logger::GetInstance() {
33   static Logger logger;
34   return logger;
35 }
36 
SetLogLevel(const LogLevel log_level)37 void Logger::SetLogLevel(const LogLevel log_level) {
38   log_level_ = log_level;
39 }
40 
SetLogStream(const LogStream log_stream)41 void Logger::SetLogStream(const LogStream log_stream) {
42   log_stream_ = log_stream;
43 }
44 
GetLogLevel() const45 LogLevel Logger::GetLogLevel() const {
46   return log_level_;
47 }
48 
GetLogStream() const49 LogStream Logger::GetLogStream() const {
50   return log_stream_;
51 }
52 
LogLevelToString(const LogLevel log_level)53 std::string LogLevelToString(const LogLevel log_level) {
54   static const std::string prefixes[] = {"VERBOSE", "DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
55   return prefixes[static_cast<int>(log_level)];
56 }
57 
58 #ifdef __ANDROID__
LogLevelToAndroidLogLevel(const LogLevel log_level)59 android::base::LogSeverity LogLevelToAndroidLogLevel(const LogLevel log_level) {
60   switch (log_level) {
61     case LogLevel::kVerbose:
62       return android::base::VERBOSE;
63     case LogLevel::kDebug:
64       return android::base::DEBUG;
65     case LogLevel::kInfo:
66       return android::base::INFO;
67     case LogLevel::kWarning:
68       return android::base::WARNING;
69     case LogLevel::kError:
70       return android::base::ERROR;
71     case LogLevel::kFatal:
72       return android::base::FATAL;
73   }
74 }
75 #endif
76 
WriteLogMessage(const LogLevel log_level,const std::string & message,const std::string & file_name,int line,bool print_errno)77 void Logger::WriteLogMessage(const LogLevel log_level, const std::string& message,
78                              const std::string& file_name, int line, bool print_errno) {
79   std::stringstream ss;
80   ss << file_name << ":" << line << ": " << LogLevelToString(log_level) << ": " << message;
81   if (print_errno) {
82     ss << ": " << strerror(errno);
83   }
84   switch (log_stream_) {
85     case LogStream::kLogcat:
86 #ifdef __ANDROID__
87       LOG(LogLevelToAndroidLogLevel(log_level)) << ss.str();
88       break;
89 #else
90       [[fallthrough]];
91 #endif
92     case LogStream::kStdout:
93       std::cout << ss.str() << '\n';
94       break;
95   }
96 }
97 
98 }  // namespace dittosuite
99