blob: c89db725e6f2bbacabaced73f816757c26f4cb2d [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 */
Jeremy Meyer56f36e82022-05-20 20:35:42 +000016#ifndef AAPT_DIAGNOSTICS_H_
17#define AAPT_DIAGNOSTICS_H_
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinski1ab598f2015-08-14 14:26:04 -070019#include <iostream>
20#include <sstream>
21#include <string>
22
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "android-base/macros.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000024#include "androidfw/IDiagnostics.h"
25#include "androidfw/Source.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "util/Util.h"
28
Adam Lesinski1ab598f2015-08-14 14:26:04 -070029namespace aapt {
Jeremy Meyer56f36e82022-05-20 20:35:42 +000030class StdErrDiagnostics : public android::IDiagnostics {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070031 public:
32 StdErrDiagnostics() = default;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070033
Jeremy Meyer56f36e82022-05-20 20:35:42 +000034 void Log(Level level, android::DiagMessageActual& actual_msg) override {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 const char* tag;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 switch (level) {
38 case Level::Error:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039 num_errors_++;
40 if (num_errors_ > 20) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 return;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070042 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 tag = "error";
44 break;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 case Level::Warn:
47 tag = "warn";
48 break;
49
50 case Level::Note:
51 tag = "note";
52 break;
Adam Lesinskicc5609d2016-04-05 12:41:07 -070053 }
54
Adam Lesinskice5e56e2016-10-21 17:56:45 -070055 if (!actual_msg.source.path.empty()) {
56 std::cerr << actual_msg.source << ": ";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 std::cerr << tag << ": " << actual_msg.message << "." << std::endl;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 }
Adam Lesinski9ba47d82015-10-13 11:37:10 -070060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 size_t num_errors_ = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063
64 DISALLOW_COPY_AND_ASSIGN(StdErrDiagnostics);
Adam Lesinskicc5609d2016-04-05 12:41:07 -070065};
66
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067} // namespace aapt
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Jeremy Meyer56f36e82022-05-20 20:35:42 +000069#endif /* AAPT_DIAGNOSTICS_H_ */