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 #ifndef AAPT_DIAGNOSTICS_H_
17 #define AAPT_DIAGNOSTICS_H_
18 
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 
23 #include "android-base/macros.h"
24 #include "androidfw/IDiagnostics.h"
25 #include "androidfw/Source.h"
26 #include "androidfw/StringPiece.h"
27 #include "util/Util.h"
28 
29 namespace aapt {
30 class StdErrDiagnostics : public android::IDiagnostics {
31  public:
32   StdErrDiagnostics() = default;
33 
Log(Level level,android::DiagMessageActual & actual_msg)34   void Log(Level level, android::DiagMessageActual& actual_msg) override {
35     const char* tag;
36 
37     switch (level) {
38       case Level::Error:
39         num_errors_++;
40         if (num_errors_ > 20) {
41           return;
42         }
43         tag = "error";
44         break;
45 
46       case Level::Warn:
47         tag = "warn";
48         break;
49 
50       case Level::Note:
51         tag = "note";
52         break;
53     }
54 
55     if (!actual_msg.source.path.empty()) {
56       std::cerr << actual_msg.source << ": ";
57     }
58     std::cerr << tag << ": " << actual_msg.message << "." << std::endl;
59   }
60 
61  private:
62   size_t num_errors_ = 0;
63 
64   DISALLOW_COPY_AND_ASSIGN(StdErrDiagnostics);
65 };
66 
67 }  // namespace aapt
68 
69 #endif /* AAPT_DIAGNOSTICS_H_ */
70