1 // Copyright 2023 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 #include <cstdlib>
16 #include <ostream>
17 
18 #include "absl/log/absl_log.h"
19 #include "aemu/base/Metrics.h"
20 #include "host-common/GfxstreamFatalError.h"
21 #include "host-common/logging.h"
22 
23 namespace {
24 
25 using android::base::CreateMetricsLogger;
26 using android::base::GfxstreamVkAbort;
27 
28 std::optional<std::function<void()>> customDieFunction = std::nullopt;
29 
die()30 [[noreturn]] void die() {
31     if (customDieFunction) {
32         (*customDieFunction)();
33     }
34     abort();
35 }
36 
37 }  // namespace
38 
39 namespace emugl {
40 
AbortMessage(const char * file,const char * function,int line,FatalError reason)41 AbortMessage::AbortMessage(const char* file, const char* function, int line, FatalError reason)
42     : mFile(file), mFunction(function), mLine(line), mReason(reason) {}
43 
~AbortMessage()44 AbortMessage::~AbortMessage() {
45     CreateMetricsLogger()->logMetricEvent(GfxstreamVkAbort{.file = mFile,
46                                                            .function = mFunction,
47                                                            .msg = mOss.str().c_str(),
48                                                            .line = mLine,
49                                                            .abort_reason = mReason.getAbortCode()});
50 
51     if (customDieFunction) {
52         ABSL_LOG(ERROR).AtLocation(mFile, mLine)
53             << "FATAL error in " << mFunction
54             << ",  GURU MEDITATION ERROR:" << mReason.getAbortCode();
55         die();
56     } else {
57         ABSL_LOG(FATAL).AtLocation(mFile, mLine)
58             << "FATAL error in " << mFunction
59             << ",  GURU MEDITATION ERROR:" << mReason.getAbortCode();
60     }
61 }
62 
setDieFunction(std::optional<std::function<void ()>> newDie)63 void setDieFunction(std::optional<std::function<void()>> newDie) { customDieFunction = newDie; }
64 }  // namespace emugl
65