The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [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 | |
| 17 | #ifndef ANDROID_TEXTOUTPUT_H |
| 18 | #define ANDROID_TEXTOUTPUT_H |
| 19 | |
| 20 | #include <utils/Errors.h> |
| 21 | |
| 22 | #include <stdint.h> |
| 23 | #include <string.h> |
Wei Wang | 13c41d3 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 24 | #include <sstream> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | // --------------------------------------------------------------------------- |
| 27 | namespace android { |
| 28 | |
Jeff Sharkey | 8cb8925 | 2013-05-30 13:53:39 -0700 | [diff] [blame] | 29 | class String8; |
| 30 | class String16; |
| 31 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | class TextOutput |
| 33 | { |
| 34 | public: |
Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 35 | TextOutput(); |
| 36 | virtual ~TextOutput(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | virtual status_t print(const char* txt, size_t len) = 0; |
| 39 | virtual void moveIndent(int delta) = 0; |
| 40 | |
| 41 | class Bundle { |
| 42 | public: |
| 43 | inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } |
| 44 | inline ~Bundle() { mTO.popBundle(); } |
| 45 | private: |
| 46 | TextOutput& mTO; |
| 47 | }; |
| 48 | |
| 49 | virtual void pushBundle() = 0; |
| 50 | virtual void popBundle() = 0; |
| 51 | }; |
| 52 | |
| 53 | // --------------------------------------------------------------------------- |
| 54 | |
| 55 | // Text output stream for printing to the log (via utils/Log.h). |
| 56 | extern TextOutput& alog; |
| 57 | |
| 58 | // Text output stream for printing to stdout. |
| 59 | extern TextOutput& aout; |
| 60 | |
| 61 | // Text output stream for printing to stderr. |
| 62 | extern TextOutput& aerr; |
| 63 | |
| 64 | typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); |
| 65 | |
| 66 | TextOutput& endl(TextOutput& to); |
| 67 | TextOutput& indent(TextOutput& to); |
| 68 | TextOutput& dedent(TextOutput& to); |
| 69 | |
Wei Wang | 13c41d3 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 70 | template<typename T> |
| 71 | TextOutput& operator<<(TextOutput& to, const T& val) |
| 72 | { |
| 73 | std::stringstream strbuf; |
| 74 | strbuf << val; |
| 75 | std::string str = strbuf.str(); |
| 76 | to.print(str.c_str(), str.size()); |
| 77 | return to; |
| 78 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
Wei Wang | 13c41d3 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 80 | TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); |
| 81 | |
| 82 | class TypeCode |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | { |
| 84 | public: |
| 85 | inline TypeCode(uint32_t code); |
| 86 | inline ~TypeCode(); |
| 87 | |
| 88 | inline uint32_t typeCode() const; |
Wei Wang | 13c41d3 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 89 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | private: |
| 91 | uint32_t mCode; |
| 92 | }; |
| 93 | |
| 94 | TextOutput& operator<<(TextOutput& to, const TypeCode& val); |
| 95 | |
| 96 | class HexDump |
| 97 | { |
| 98 | public: |
| 99 | HexDump(const void *buf, size_t size, size_t bytesPerLine=16); |
| 100 | inline ~HexDump(); |
| 101 | |
| 102 | inline HexDump& setBytesPerLine(size_t bytesPerLine); |
| 103 | inline HexDump& setSingleLineCutoff(int32_t bytes); |
| 104 | inline HexDump& setAlignment(size_t alignment); |
| 105 | inline HexDump& setCArrayStyle(bool enabled); |
| 106 | |
| 107 | inline const void* buffer() const; |
| 108 | inline size_t size() const; |
| 109 | inline size_t bytesPerLine() const; |
| 110 | inline int32_t singleLineCutoff() const; |
| 111 | inline size_t alignment() const; |
| 112 | inline bool carrayStyle() const; |
| 113 | |
| 114 | private: |
| 115 | const void* mBuffer; |
| 116 | size_t mSize; |
| 117 | size_t mBytesPerLine; |
| 118 | int32_t mSingleLineCutoff; |
| 119 | size_t mAlignment; |
| 120 | bool mCArrayStyle; |
| 121 | }; |
| 122 | |
| 123 | TextOutput& operator<<(TextOutput& to, const HexDump& val); |
| 124 | |
| 125 | // --------------------------------------------------------------------------- |
| 126 | // No user servicable parts below. |
| 127 | |
| 128 | inline TextOutput& endl(TextOutput& to) |
| 129 | { |
| 130 | to.print("\n", 1); |
| 131 | return to; |
| 132 | } |
| 133 | |
| 134 | inline TextOutput& indent(TextOutput& to) |
| 135 | { |
| 136 | to.moveIndent(1); |
| 137 | return to; |
| 138 | } |
| 139 | |
| 140 | inline TextOutput& dedent(TextOutput& to) |
| 141 | { |
| 142 | to.moveIndent(-1); |
| 143 | return to; |
| 144 | } |
| 145 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) |
| 147 | { |
| 148 | return (*func)(to); |
| 149 | } |
| 150 | |
| 151 | inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } |
| 152 | inline TypeCode::~TypeCode() { } |
| 153 | inline uint32_t TypeCode::typeCode() const { return mCode; } |
| 154 | |
| 155 | inline HexDump::~HexDump() { } |
| 156 | |
| 157 | inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { |
| 158 | mBytesPerLine = bytesPerLine; return *this; |
| 159 | } |
| 160 | inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { |
| 161 | mSingleLineCutoff = bytes; return *this; |
| 162 | } |
| 163 | inline HexDump& HexDump::setAlignment(size_t alignment) { |
| 164 | mAlignment = alignment; return *this; |
| 165 | } |
| 166 | inline HexDump& HexDump::setCArrayStyle(bool enabled) { |
| 167 | mCArrayStyle = enabled; return *this; |
| 168 | } |
| 169 | |
| 170 | inline const void* HexDump::buffer() const { return mBuffer; } |
| 171 | inline size_t HexDump::size() const { return mSize; } |
| 172 | inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } |
| 173 | inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } |
| 174 | inline size_t HexDump::alignment() const { return mAlignment; } |
| 175 | inline bool HexDump::carrayStyle() const { return mCArrayStyle; } |
| 176 | |
| 177 | // --------------------------------------------------------------------------- |
| 178 | }; // namespace android |
| 179 | |
| 180 | #endif // ANDROID_TEXTOUTPUT_H |