From 63f6bfaf4c298c6e50e4ba3b20df3c9463b62ef8 Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Wed, 22 Feb 2023 15:24:20 -0800 Subject: [PATCH] Emit debug message enum instead of ID. --- src/gpgmm/common/BUILD.gn | 1 + src/gpgmm/common/CMakeLists.txt | 1 + src/gpgmm/common/Message.cpp | 49 +++++++++++++++++++++++++++++++++ src/gpgmm/common/Message.h | 2 ++ src/gpgmm/utils/Log.cpp | 13 +++++---- 5 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 src/gpgmm/common/Message.cpp diff --git a/src/gpgmm/common/BUILD.gn b/src/gpgmm/common/BUILD.gn index ee06306cc..7a81bd33d 100644 --- a/src/gpgmm/common/BUILD.gn +++ b/src/gpgmm/common/BUILD.gn @@ -201,6 +201,7 @@ source_set("gpgmm_common_sources") { "MemoryCache.h", "MemoryPool.cpp", "MemoryPool.h", + "Message.cpp", "Message.h", "PooledMemoryAllocator.cpp", "PooledMemoryAllocator.h", diff --git a/src/gpgmm/common/CMakeLists.txt b/src/gpgmm/common/CMakeLists.txt index 68357dff1..0c9b96cec 100644 --- a/src/gpgmm/common/CMakeLists.txt +++ b/src/gpgmm/common/CMakeLists.txt @@ -44,6 +44,7 @@ target_sources(gpgmm_common PRIVATE "MemoryCache.h" "MemoryPool.cpp" "MemoryPool.h" + "Message.cpp" "Message.h" "PooledMemoryAllocator.cpp" "PooledMemoryAllocator.h" diff --git a/src/gpgmm/common/Message.cpp b/src/gpgmm/common/Message.cpp new file mode 100644 index 000000000..4e691e8d0 --- /dev/null +++ b/src/gpgmm/common/Message.cpp @@ -0,0 +1,49 @@ +// Copyright 2022 The GPGMM 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 "Message.h" + +#include "gpgmm/utils/Assert.h" + +namespace gpgmm { + + const char* GetMessageFromID(MessageId messageId) { + switch (messageId) { + case MessageId::kUnknown: + return "UNKNOWN"; + case MessageId::kSizeExceeded: + return "SIZE_EXCEEDED"; + case MessageId::kAlignmentMismatch: + return "ALIGNMENT_MISMATCH"; + case MessageId::kAllocatorFailed: + return "ALLOCATOR_FAILED"; + case MessageId::kPrefetchFailed: + return "PREFETCH_FAILED"; + case MessageId::kBudgetExceeded: + return "BUDGET_EXCEEDED"; + case MessageId::kBudgetUpdated: + return "BUDGET_UPDATED"; + case MessageId::kBudgetInvalid: + return "BUDGET_INVALID"; + case MessageId::kInvalidArgument: + return "INVALID_ARGUMENT"; + case MessageId::kBadOperation: + return "BAD_OPERATION"; + default: + UNREACHABLE(); + return ""; + } + } + +} // namespace gpgmm diff --git a/src/gpgmm/common/Message.h b/src/gpgmm/common/Message.h index b7893574c..74c0e7be8 100644 --- a/src/gpgmm/common/Message.h +++ b/src/gpgmm/common/Message.h @@ -43,6 +43,8 @@ namespace gpgmm { MessageSeverity Severity; }; + const char* GetMessageFromID(MessageId messageId); + } // namespace gpgmm #endif // GPGMM_COMMON_MESSAGE_H_ diff --git a/src/gpgmm/utils/Log.cpp b/src/gpgmm/utils/Log.cpp index d06863c05..3e1182fcf 100644 --- a/src/gpgmm/utils/Log.cpp +++ b/src/gpgmm/utils/Log.cpp @@ -121,10 +121,10 @@ namespace gpgmm { if (IsDebuggerPresent()) { std::string outputString; if (mMessageId != MessageId::kUnknown) { - outputString = - std::string(kLogTag) + " " + std::string(severityName) + - "(tid: " + ToString(std::this_thread::get_id()) + "): " + fullMessage + - "(MessageId=" + ToString(static_cast(mMessageId)) + ")" + "\n"; + outputString = std::string(kLogTag) + " " + std::string(severityName) + + "(tid: " + ToString(std::this_thread::get_id()) + + "): " + fullMessage + "[" + GetMessageFromID(mMessageId) + "]" + + "\n"; } else { outputString = std::string(kLogTag) + " " + std::string(severityName) + "(tid: " + ToString(std::this_thread::get_id()) + @@ -147,8 +147,9 @@ namespace gpgmm { #else // defined(GPGMM_PLATFORM_ANDROID) // Note: we use fprintf because includes static initializers. if (mMessageId != MessageId::kUnknown) { - fprintf(outputStream, "%s %s (tid:%s): %s (MessageId=%d)\n", kLogTag, severityName, - ToString(std::this_thread::get_id()).c_str(), fullMessage.c_str(), mMessageId); + fprintf(outputStream, "%s %s (tid:%s): %s [%s]\n", kLogTag, severityName, + ToString(std::this_thread::get_id()).c_str(), fullMessage.c_str(), + GetMessageFromID(mMessageId)); } else { fprintf(outputStream, "%s %s (tid:%s): %s\n", kLogTag, severityName, ToString(std::this_thread::get_id()).c_str(), fullMessage.c_str());