Add Log.h to replace all uses of iostream

On Android iostream doesn't appear in logcat, the system log that's
often used for printf debugging. Introduce Chromium/ANGLE like logging
that looks like the following:

  InfoLog() << stuff << stuff;

This makes sure the message is put in logcat on Android and removes
static initializers from <iostream>

BUG=dawn:286

Change-Id: Ie0d018f49bcac1a7b740739a6e59d45ae6728638
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/14102
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: David Turner <digit@google.com>
Reviewed-by: Austin Eng <enga@chromium.org>
This commit is contained in:
Corentin Wallez
2019-12-05 11:13:01 +00:00
committed by Commit Bot service account
parent 1d6250d016
commit 95586ff184
13 changed files with 256 additions and 56 deletions

112
src/common/Log.cpp Normal file
View File

@@ -0,0 +1,112 @@
// Copyright 2019 The Dawn Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "common/Log.h"
#include "common/Assert.h"
#include "common/Platform.h"
#include <cstdio>
#if defined(DAWN_PLATFORM_ANDROID)
# include <android/log.h>
#endif
namespace {
const char* SeverityName(LogSeverity severity) {
switch (severity) {
case LogSeverity::Debug:
return "Debug";
case LogSeverity::Info:
return "Info";
case LogSeverity::Warning:
return "Warning";
case LogSeverity::Error:
return "Error";
default:
UNREACHABLE();
return "";
}
}
#if defined(DAWN_PLATFORM_ANDROID)
android_LogPriority AndroidLogPriority(LogSeverity severity) {
switch (severity) {
case LogSeverity::Debug:
return ANDROID_LOG_INFO;
case LogSeverity::Info:
return ANDROID_LOG_INFO;
case LogSeverity::Warning:
return ANDROID_LOG_WARN;
case LogSeverity::Error:
return ANDROID_LOG_ERROR;
default:
UNREACHABLE();
return ANDROID_LOG_ERROR;
}
}
#endif // defined(DAWN_PLATFORM_ANDROID)
} // anonymous namespace
LogMessage::LogMessage(LogSeverity severity) : mSeverity(severity) {
}
LogMessage::~LogMessage() {
std::string fullMessage = mStream.str();
// If this message has been moved, its stream is empty.
if (fullMessage.empty()) {
return;
}
const char* severityName = SeverityName(mSeverity);
FILE* outputStream = stdout;
if (mSeverity == LogSeverity::Warning || mSeverity == LogSeverity::Error) {
outputStream = stderr;
}
#if defined(DAWN_PLATFORM_ANDROID)
android_LogPriority androidPriority = AndroidLogPriority(mSeverity);
__android_log_print(androidPriority, "Dawn", "%s: %s\n", severityName, fullMessage.c_str());
#else // defined(DAWN_PLATFORM_ANDROID)
// Note: we use fprintf because <iostream> includes static initializers.
fprintf(outputStream, "%s: %s\n", severityName, fullMessage.c_str());
fflush(outputStream);
#endif // defined(DAWN_PLATFORM_ANDROID)
}
LogMessage DebugLog() {
return {LogSeverity::Debug};
}
LogMessage InfoLog() {
return {LogSeverity::Info};
}
LogMessage WarningLog() {
return {LogSeverity::Warning};
}
LogMessage ErrorLog() {
return {LogSeverity::Error};
}
LogMessage DebugLog(const char* file, const char* function, int line) {
LogMessage message = DebugLog();
message << file << ":" << line << "(" << function << ")";
return message;
}