1 /*
2  * Copyright (C) 2020, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "logging.h"
18 
19 #include "aidl_language.h"
20 
21 bool AidlErrorLog::sHadError = false;
22 
23 AidlErrorLog::AidlErrorLog(Severity severity, const AidlLocation& location,
24                            const std::string& suffix /* = "" */)
25     : os_(&std::cerr), severity_(severity), location_(location), suffix_(suffix) {
26   sHadError |= severity_ >= ERROR;
27   if (severity_ != NO_OP) {
28     (*os_) << (severity_ == WARNING ? "WARNING: " : "ERROR: ");
29     (*os_) << location << ": ";
30   }
31 }
32 
33 AidlErrorLog::AidlErrorLog(Severity severity, const AidlNode& node)
34     : AidlErrorLog(severity, node.location_) {}
35 
36 AidlErrorLog::AidlErrorLog(Severity severity, const std::string& filename)
37     : AidlErrorLog(severity, AidlLocation(filename, AidlLocation::Source::EXTERNAL)) {}
38 
39 AidlErrorLog::~AidlErrorLog() {
40   if (severity_ == NO_OP) return;
41   (*os_) << suffix_ << std::endl;
42   if (severity_ == FATAL) abort();
43   if (location_.IsInternal()) {
44     (*os_) << "Logging an internal location should not happen. Offending location: " << location_
45            << std::endl;
46     abort();
47   }
48 }
49