Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 17 | #ifndef ANDROID_HARDWARE_TEXTOUTPUT_H |
| 18 | #define ANDROID_HARDWARE_TEXTOUTPUT_H |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 19 | |
| 20 | #include <utils/Errors.h> |
Wei Wang | a3ff007 | 2016-10-21 11:08:07 -0700 | [diff] [blame] | 21 | #include <utils/String8.h> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
Wei Wang | 8a2e8ac | 2016-10-14 09:54:27 -0700 | [diff] [blame] | 25 | #include <sstream> |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 26 | |
| 27 | // --------------------------------------------------------------------------- |
| 28 | namespace android { |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 29 | namespace hardware { |
Jeff Sharkey | 69c328d | 2013-05-30 13:53:39 -0700 | [diff] [blame] | 30 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 31 | class TextOutput |
| 32 | { |
| 33 | public: |
| 34 | TextOutput(); |
| 35 | virtual ~TextOutput(); |
| 36 | |
| 37 | virtual status_t print(const char* txt, size_t len) = 0; |
| 38 | virtual void moveIndent(int delta) = 0; |
| 39 | |
| 40 | class Bundle { |
| 41 | public: |
Chih-Hung Hsieh | 6b684f4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 42 | inline explicit Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 43 | inline ~Bundle() { mTO.popBundle(); } |
| 44 | private: |
| 45 | TextOutput& mTO; |
| 46 | }; |
| 47 | |
| 48 | virtual void pushBundle() = 0; |
| 49 | virtual void popBundle() = 0; |
| 50 | }; |
| 51 | |
| 52 | // --------------------------------------------------------------------------- |
| 53 | |
| 54 | // Text output stream for printing to the log (via utils/Log.h). |
| 55 | extern TextOutput& alog; |
| 56 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 57 | typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); |
| 58 | |
| 59 | TextOutput& endl(TextOutput& to); |
| 60 | TextOutput& indent(TextOutput& to); |
| 61 | TextOutput& dedent(TextOutput& to); |
| 62 | |
Wei Wang | 8a2e8ac | 2016-10-14 09:54:27 -0700 | [diff] [blame] | 63 | template<typename T> |
| 64 | TextOutput& operator<<(TextOutput& to, const T& val) |
| 65 | { |
| 66 | std::stringstream strbuf; |
| 67 | strbuf << val; |
| 68 | std::string str = strbuf.str(); |
| 69 | to.print(str.c_str(), str.size()); |
| 70 | return to; |
| 71 | } |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 72 | |
Wei Wang | 8a2e8ac | 2016-10-14 09:54:27 -0700 | [diff] [blame] | 73 | TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); |
| 74 | |
| 75 | class TypeCode |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 76 | { |
| 77 | public: |
Chih-Hung Hsieh | 6b684f4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 78 | inline explicit TypeCode(uint32_t code); |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 79 | inline ~TypeCode(); |
| 80 | |
| 81 | inline uint32_t typeCode() const; |
Wei Wang | 8a2e8ac | 2016-10-14 09:54:27 -0700 | [diff] [blame] | 82 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 83 | private: |
| 84 | uint32_t mCode; |
| 85 | }; |
| 86 | |
| 87 | TextOutput& operator<<(TextOutput& to, const TypeCode& val); |
| 88 | |
| 89 | class HexDump |
| 90 | { |
| 91 | public: |
| 92 | HexDump(const void *buf, size_t size, size_t bytesPerLine=16); |
| 93 | inline ~HexDump(); |
| 94 | |
| 95 | inline HexDump& setBytesPerLine(size_t bytesPerLine); |
| 96 | inline HexDump& setSingleLineCutoff(int32_t bytes); |
| 97 | inline HexDump& setAlignment(size_t alignment); |
| 98 | inline HexDump& setCArrayStyle(bool enabled); |
| 99 | |
| 100 | inline const void* buffer() const; |
| 101 | inline size_t size() const; |
| 102 | inline size_t bytesPerLine() const; |
| 103 | inline int32_t singleLineCutoff() const; |
| 104 | inline size_t alignment() const; |
| 105 | inline bool carrayStyle() const; |
| 106 | |
| 107 | private: |
| 108 | const void* mBuffer; |
| 109 | size_t mSize; |
| 110 | size_t mBytesPerLine; |
| 111 | int32_t mSingleLineCutoff; |
| 112 | size_t mAlignment; |
| 113 | bool mCArrayStyle; |
| 114 | }; |
| 115 | |
| 116 | TextOutput& operator<<(TextOutput& to, const HexDump& val); |
Wei Wang | a3ff007 | 2016-10-21 11:08:07 -0700 | [diff] [blame] | 117 | inline TextOutput& operator<<(TextOutput& to, |
| 118 | decltype(std::endl<char, |
| 119 | std::char_traits<char>>) |
| 120 | /*val*/) { |
| 121 | endl(to); |
| 122 | return to; |
| 123 | } |
| 124 | |
| 125 | inline TextOutput& operator<<(TextOutput& to, const char &c) |
| 126 | { |
| 127 | to.print(&c, 1); |
| 128 | return to; |
| 129 | } |
| 130 | |
| 131 | inline TextOutput& operator<<(TextOutput& to, const bool &val) |
| 132 | { |
| 133 | if (val) to.print("true", 4); |
| 134 | else to.print("false", 5); |
| 135 | return to; |
| 136 | } |
| 137 | |
| 138 | inline TextOutput& operator<<(TextOutput& to, const String16& val) |
| 139 | { |
| 140 | to << String8(val).string(); |
| 141 | return to; |
| 142 | } |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 143 | |
| 144 | // --------------------------------------------------------------------------- |
| 145 | // No user servicable parts below. |
| 146 | |
| 147 | inline TextOutput& endl(TextOutput& to) |
| 148 | { |
| 149 | to.print("\n", 1); |
| 150 | return to; |
| 151 | } |
| 152 | |
| 153 | inline TextOutput& indent(TextOutput& to) |
| 154 | { |
| 155 | to.moveIndent(1); |
| 156 | return to; |
| 157 | } |
| 158 | |
| 159 | inline TextOutput& dedent(TextOutput& to) |
| 160 | { |
| 161 | to.moveIndent(-1); |
| 162 | return to; |
| 163 | } |
| 164 | |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 165 | inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) |
| 166 | { |
| 167 | return (*func)(to); |
| 168 | } |
| 169 | |
| 170 | inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } |
| 171 | inline TypeCode::~TypeCode() { } |
| 172 | inline uint32_t TypeCode::typeCode() const { return mCode; } |
| 173 | |
| 174 | inline HexDump::~HexDump() { } |
| 175 | |
| 176 | inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { |
| 177 | mBytesPerLine = bytesPerLine; return *this; |
| 178 | } |
| 179 | inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { |
| 180 | mSingleLineCutoff = bytes; return *this; |
| 181 | } |
| 182 | inline HexDump& HexDump::setAlignment(size_t alignment) { |
| 183 | mAlignment = alignment; return *this; |
| 184 | } |
| 185 | inline HexDump& HexDump::setCArrayStyle(bool enabled) { |
| 186 | mCArrayStyle = enabled; return *this; |
| 187 | } |
| 188 | |
| 189 | inline const void* HexDump::buffer() const { return mBuffer; } |
| 190 | inline size_t HexDump::size() const { return mSize; } |
| 191 | inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } |
| 192 | inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } |
| 193 | inline size_t HexDump::alignment() const { return mAlignment; } |
| 194 | inline bool HexDump::carrayStyle() const { return mCArrayStyle; } |
| 195 | |
| 196 | // --------------------------------------------------------------------------- |
Steven Moreland | 7173a4c | 2019-09-26 15:55:02 -0700 | [diff] [blame] | 197 | } // namespace hardware |
| 198 | } // namespace android |
Mathias Agopian | 4ea13dc | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 199 | |
Martijn Coenen | f75a23d | 2016-08-01 11:55:17 +0200 | [diff] [blame] | 200 | #endif // ANDROID_HARDWARE_TEXTOUTPUT_H |