1 // Copyright 2019 The Dawn Authors 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 "common/Log.h" 16 17 #include "common/Assert.h" 18 #include "common/Platform.h" 19 20 #include <cstdio> 21 22 #if defined(DAWN_PLATFORM_ANDROID) 23 # include <android/log.h> 24 #endif 25 26 namespace dawn { 27 28 namespace { 29 SeverityName(LogSeverity severity)30 const char* SeverityName(LogSeverity severity) { 31 switch (severity) { 32 case LogSeverity::Debug: 33 return "Debug"; 34 case LogSeverity::Info: 35 return "Info"; 36 case LogSeverity::Warning: 37 return "Warning"; 38 case LogSeverity::Error: 39 return "Error"; 40 default: 41 UNREACHABLE(); 42 return ""; 43 } 44 } 45 46 #if defined(DAWN_PLATFORM_ANDROID) AndroidLogPriority(LogSeverity severity)47 android_LogPriority AndroidLogPriority(LogSeverity severity) { 48 switch (severity) { 49 case LogSeverity::Debug: 50 return ANDROID_LOG_INFO; 51 case LogSeverity::Info: 52 return ANDROID_LOG_INFO; 53 case LogSeverity::Warning: 54 return ANDROID_LOG_WARN; 55 case LogSeverity::Error: 56 return ANDROID_LOG_ERROR; 57 default: 58 UNREACHABLE(); 59 return ANDROID_LOG_ERROR; 60 } 61 } 62 #endif // defined(DAWN_PLATFORM_ANDROID) 63 64 } // anonymous namespace 65 LogMessage(LogSeverity severity)66 LogMessage::LogMessage(LogSeverity severity) : mSeverity(severity) { 67 } 68 ~LogMessage()69 LogMessage::~LogMessage() { 70 std::string fullMessage = mStream.str(); 71 72 // If this message has been moved, its stream is empty. 73 if (fullMessage.empty()) { 74 return; 75 } 76 77 const char* severityName = SeverityName(mSeverity); 78 79 #if defined(DAWN_PLATFORM_ANDROID) 80 android_LogPriority androidPriority = AndroidLogPriority(mSeverity); 81 __android_log_print(androidPriority, "Dawn", "%s: %s\n", severityName, fullMessage.c_str()); 82 #else // defined(DAWN_PLATFORM_ANDROID) 83 FILE* outputStream = stdout; 84 if (mSeverity == LogSeverity::Warning || mSeverity == LogSeverity::Error) { 85 outputStream = stderr; 86 } 87 88 // Note: we use fprintf because <iostream> includes static initializers. 89 fprintf(outputStream, "%s: %s\n", severityName, fullMessage.c_str()); 90 fflush(outputStream); 91 #endif // defined(DAWN_PLATFORM_ANDROID) 92 } 93 DebugLog()94 LogMessage DebugLog() { 95 return {LogSeverity::Debug}; 96 } 97 InfoLog()98 LogMessage InfoLog() { 99 return {LogSeverity::Info}; 100 } 101 WarningLog()102 LogMessage WarningLog() { 103 return {LogSeverity::Warning}; 104 } 105 ErrorLog()106 LogMessage ErrorLog() { 107 return {LogSeverity::Error}; 108 } 109 DebugLog(const char * file,const char * function,int line)110 LogMessage DebugLog(const char* file, const char* function, int line) { 111 LogMessage message = DebugLog(); 112 message << file << ":" << line << "(" << function << ")"; 113 return message; 114 } 115 116 } // namespace dawn 117