Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [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_LOGGER_H |
| 18 | #define AAPT_LOGGER_H |
| 19 | |
| 20 | #include "Source.h" |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <ostream> |
| 24 | #include <string> |
| 25 | #include <utils/String8.h> |
| 26 | |
| 27 | namespace aapt { |
| 28 | |
| 29 | struct Log { |
| 30 | Log(std::ostream& out, std::ostream& err); |
| 31 | Log(const Log& rhs) = delete; |
| 32 | |
| 33 | std::ostream& out; |
| 34 | std::ostream& err; |
| 35 | }; |
| 36 | |
| 37 | class Logger { |
| 38 | public: |
| 39 | static void setLog(const std::shared_ptr<Log>& log); |
| 40 | |
| 41 | static std::ostream& error(); |
| 42 | static std::ostream& error(const Source& source); |
| 43 | static std::ostream& error(const SourceLine& sourceLine); |
| 44 | |
| 45 | static std::ostream& warn(); |
| 46 | static std::ostream& warn(const Source& source); |
| 47 | static std::ostream& warn(const SourceLine& sourceLine); |
| 48 | |
| 49 | static std::ostream& note(); |
| 50 | static std::ostream& note(const Source& source); |
| 51 | static std::ostream& note(const SourceLine& sourceLine); |
| 52 | |
| 53 | private: |
| 54 | static std::shared_ptr<Log> sLog; |
| 55 | }; |
| 56 | |
| 57 | class SourceLogger { |
| 58 | public: |
| 59 | SourceLogger(const Source& source); |
| 60 | |
| 61 | std::ostream& error(); |
| 62 | std::ostream& error(size_t line); |
| 63 | |
| 64 | std::ostream& warn(); |
| 65 | std::ostream& warn(size_t line); |
| 66 | |
| 67 | std::ostream& note(); |
| 68 | std::ostream& note(size_t line); |
| 69 | |
| 70 | private: |
| 71 | Source mSource; |
| 72 | }; |
| 73 | |
| 74 | inline ::std::ostream& operator<<(::std::ostream& out, const std::u16string& str) { |
| 75 | android::String8 utf8(str.data(), str.size()); |
| 76 | return out.write(utf8.string(), utf8.size()); |
| 77 | } |
| 78 | |
| 79 | } // namespace aapt |
| 80 | |
| 81 | #endif // AAPT_LOGGER_H |