blob: 7ea26b3ed0df6a33e996f4b88f6ae3609e2b6e69 [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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
29namespace aapt {
30
31struct DiagMessageActual {
32 Source source;
33 std::string message;
34};
35
36struct DiagMessage {
37private:
38 Source mSource;
39 std::stringstream mMessage;
40
41public:
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 Lesinski1ab598f2015-08-14 14:26:04 -070054
55 DiagMessageActual build() const {
56 return DiagMessageActual{ mSource, mMessage.str() };
57 }
58};
59
60struct 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
68struct StdErrDiagnostics : public IDiagnostics {
Adam Lesinski9ba47d82015-10-13 11:37:10 -070069 size_t mNumErrors = 0;
70
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 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 Lesinski9ba47d82015-10-13 11:37:10 -070080 if (mNumErrors < 20) {
81 emit(msg, "error: ");
82 }
83 mNumErrors++;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 }
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 */