Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | #ifndef AAPT_DIAGNOSTICS_H |
| 18 | #define AAPT_DIAGNOSTICS_H |
| 19 | |
| 20 | #include "Source.h" |
| 21 | |
| 22 | #include "util/StringPiece.h" |
| 23 | #include "util/Util.h" |
| 24 | |
| 25 | #include <iostream> |
| 26 | #include <sstream> |
| 27 | #include <string> |
| 28 | |
| 29 | namespace aapt { |
| 30 | |
| 31 | struct DiagMessageActual { |
| 32 | Source source; |
| 33 | std::string message; |
| 34 | }; |
| 35 | |
| 36 | struct DiagMessage { |
| 37 | private: |
| 38 | Source mSource; |
| 39 | std::stringstream mMessage; |
| 40 | |
| 41 | public: |
| 42 | DiagMessage() = default; |
| 43 | |
| 44 | DiagMessage(const StringPiece& src) : mSource(src) { |
| 45 | } |
| 46 | |
| 47 | DiagMessage(const Source& src) : mSource(src) { |
| 48 | } |
| 49 | |
| 50 | template <typename T> DiagMessage& operator<<(const T& value) { |
| 51 | mMessage << value; |
| 52 | return *this; |
| 53 | } |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 54 | |
| 55 | DiagMessageActual build() const { |
| 56 | return DiagMessageActual{ mSource, mMessage.str() }; |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | struct IDiagnostics { |
| 61 | virtual ~IDiagnostics() = default; |
| 62 | |
| 63 | virtual void error(const DiagMessage& message) = 0; |
| 64 | virtual void warn(const DiagMessage& message) = 0; |
| 65 | virtual void note(const DiagMessage& message) = 0; |
| 66 | }; |
| 67 | |
| 68 | struct StdErrDiagnostics : public IDiagnostics { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 69 | size_t mNumErrors = 0; |
| 70 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 71 | void emit(const DiagMessage& msg, const char* tag) { |
| 72 | DiagMessageActual actual = msg.build(); |
| 73 | if (!actual.source.path.empty()) { |
| 74 | std::cerr << actual.source << ": "; |
| 75 | } |
| 76 | std::cerr << tag << actual.message << "." << std::endl; |
| 77 | } |
| 78 | |
| 79 | void error(const DiagMessage& msg) override { |
Adam Lesinski | 9ba47d8 | 2015-10-13 11:37:10 -0700 | [diff] [blame^] | 80 | if (mNumErrors < 20) { |
| 81 | emit(msg, "error: "); |
| 82 | } |
| 83 | mNumErrors++; |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void warn(const DiagMessage& msg) override { |
| 87 | emit(msg, "warn: "); |
| 88 | } |
| 89 | |
| 90 | void note(const DiagMessage& msg) override { |
| 91 | emit(msg, "note: "); |
| 92 | } |
| 93 | }; |
| 94 | |
| 95 | } // namespace aapt |
| 96 | |
| 97 | #endif /* AAPT_DIAGNOSTICS_H */ |