mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-12-15 16:16:08 +00:00
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:
committed by
Commit Bot service account
parent
1d6250d016
commit
95586ff184
@@ -13,14 +13,13 @@
|
||||
// limitations under the License.
|
||||
|
||||
#include "common/Assert.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "common/Log.h"
|
||||
|
||||
void HandleAssertionFailure(const char* file,
|
||||
const char* function,
|
||||
int line,
|
||||
const char* condition) {
|
||||
std::cerr << "Assertion failure at " << file << ":" << line << " (" << function
|
||||
<< "): " << condition << std::endl;
|
||||
ErrorLog() << "Assertion failure at " << file << ":" << line << " (" << function
|
||||
<< "): " << condition;
|
||||
DAWN_BREAKPOINT();
|
||||
}
|
||||
|
||||
@@ -90,6 +90,8 @@ if (is_win || is_linux || is_mac || is_fuchsia || is_android) {
|
||||
"DynamicLib.cpp",
|
||||
"DynamicLib.h",
|
||||
"HashUtils.h",
|
||||
"Log.cpp",
|
||||
"Log.h",
|
||||
"Math.cpp",
|
||||
"Math.h",
|
||||
"Platform.h",
|
||||
@@ -116,5 +118,8 @@ if (is_win || is_linux || is_mac || is_fuchsia || is_android) {
|
||||
"../../third_party:vulkan_headers",
|
||||
]
|
||||
}
|
||||
if (is_android) {
|
||||
libs = [ "log" ]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
112
src/common/Log.cpp
Normal file
112
src/common/Log.cpp
Normal 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;
|
||||
}
|
||||
90
src/common/Log.h
Normal file
90
src/common/Log.h
Normal file
@@ -0,0 +1,90 @@
|
||||
// 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.
|
||||
|
||||
#ifndef COMMON_LOG_H_
|
||||
#define COMMON_LOG_H_
|
||||
|
||||
// Dawn targets shouldn't use iostream or printf directly for several reasons:
|
||||
// - iostream adds static initializers which we want to avoid.
|
||||
// - printf and iostream don't show up in logcat on Android so printf debugging doesn't work but
|
||||
// log-message debugging does.
|
||||
// - log severity helps provide intent compared to a printf.
|
||||
//
|
||||
// Logging should in general be avoided: errors should go through the regular WebGPU error reporting
|
||||
// mechanism and others form of logging should (TODO: eventually) go through the logging dependency
|
||||
// injection, so for example they show up in Chromium's about:gpu page. Nonetheless there are some
|
||||
// cases where logging is necessary and when this file was first introduced we needed to replace all
|
||||
// uses of iostream so we could see them in Android's logcat.
|
||||
//
|
||||
// Regular logging is done using the [Debug|Info|Warning|Error]Log() function this way:
|
||||
//
|
||||
// InfoLog() << things << that << ostringstream << supports; // No need for a std::endl or "\n"
|
||||
//
|
||||
// It creates a LogMessage object that isn't stored anywhere and gets its destructor called
|
||||
// immediately which outputs the stored ostringstream in the right place.
|
||||
//
|
||||
// This file also contains DAWN_DEBUG for "printf debugging" which works on Android and
|
||||
// additionally outputs the file, line and function name. Use it this way:
|
||||
//
|
||||
// // Pepper this throughout code to get a log of the execution
|
||||
// DAWN_DEBUG();
|
||||
//
|
||||
// // Get more information
|
||||
// DAWN_DEBUG() << texture.GetFormat();
|
||||
|
||||
#include <sstream>
|
||||
|
||||
// Log levels mostly used to signal intent where the log message is produced and used to route the
|
||||
// message to the correct output.
|
||||
enum class LogSeverity {
|
||||
Debug,
|
||||
Info,
|
||||
Warning,
|
||||
Error,
|
||||
};
|
||||
|
||||
// Essentially an ostringstream that will print itself in its destructor.
|
||||
class LogMessage {
|
||||
public:
|
||||
LogMessage(LogSeverity severity);
|
||||
~LogMessage();
|
||||
|
||||
LogMessage(LogMessage&& other) = default;
|
||||
LogMessage& operator=(LogMessage&& other) = default;
|
||||
|
||||
template <typename T>
|
||||
LogMessage& operator<<(T&& value) {
|
||||
mStream << value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
LogMessage(const LogMessage& other) = delete;
|
||||
LogMessage& operator=(const LogMessage& other) = delete;
|
||||
|
||||
LogSeverity mSeverity;
|
||||
std::ostringstream mStream;
|
||||
};
|
||||
|
||||
// Short-hands to create a LogMessage with the respective severity.
|
||||
LogMessage DebugLog();
|
||||
LogMessage InfoLog();
|
||||
LogMessage WarningLog();
|
||||
LogMessage ErrorLog();
|
||||
|
||||
// DAWN_DEBUG is a helper macro that creates a DebugLog and outputs file/line/function information
|
||||
LogMessage DebugLog(const char* file, const char* function, int line);
|
||||
#define DAWN_DEBUG() DebugLog(__FILE__, __func__, __LINE__)
|
||||
|
||||
#endif // COMMON_LOG_H_
|
||||
Reference in New Issue
Block a user