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> 24 25 // --------------------------------------------------------------------------- 26 namespace android { 27 28 class TextOutput 29 { 30 public: 31 TextOutput(); 32 virtual ~TextOutput(); 33 34 virtual status_t print(const char* txt, size_t len) = 0; 35 virtual void moveIndent(int delta) = 0; 36 37 class Bundle { 38 public: 39 inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } 40 inline ~Bundle() { mTO.popBundle(); } 41 private: 42 TextOutput& mTO; 43 }; 44 45 virtual void pushBundle() = 0; 46 virtual void popBundle() = 0; 47 }; 48 49 // --------------------------------------------------------------------------- 50 51 // Text output stream for printing to the log (via utils/Log.h). 52 extern TextOutput& alog; 53 54 // Text output stream for printing to stdout. 55 extern TextOutput& aout; 56 57 // Text output stream for printing to stderr. 58 extern TextOutput& aerr; 59 60 typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); 61 62 TextOutput& endl(TextOutput& to); 63 TextOutput& indent(TextOutput& to); 64 TextOutput& dedent(TextOutput& to); 65 66 TextOutput& operator<<(TextOutput& to, const char* str); 67 TextOutput& operator<<(TextOutput& to, char); // writes raw character 68 TextOutput& operator<<(TextOutput& to, bool); 69 TextOutput& operator<<(TextOutput& to, int); 70 TextOutput& operator<<(TextOutput& to, long); 71 TextOutput& operator<<(TextOutput& to, unsigned int); 72 TextOutput& operator<<(TextOutput& to, unsigned long); 73 TextOutput& operator<<(TextOutput& to, long long); 74 TextOutput& operator<<(TextOutput& to, unsigned long long); 75 TextOutput& operator<<(TextOutput& to, float); 76 TextOutput& operator<<(TextOutput& to, double); 77 TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); 78 TextOutput& operator<<(TextOutput& to, const void*); 79 80 class TypeCode 81 { 82 public: 83 inline TypeCode(uint32_t code); 84 inline ~TypeCode(); 85 86 inline uint32_t typeCode() const; 87 88 private: 89 uint32_t mCode; 90 }; 91 92 TextOutput& operator<<(TextOutput& to, const TypeCode& val); 93 94 class HexDump 95 { 96 public: 97 HexDump(const void *buf, size_t size, size_t bytesPerLine=16); 98 inline ~HexDump(); 99 100 inline HexDump& setBytesPerLine(size_t bytesPerLine); 101 inline HexDump& setSingleLineCutoff(int32_t bytes); 102 inline HexDump& setAlignment(size_t alignment); 103 inline HexDump& setCArrayStyle(bool enabled); 104 105 inline const void* buffer() const; 106 inline size_t size() const; 107 inline size_t bytesPerLine() const; 108 inline int32_t singleLineCutoff() const; 109 inline size_t alignment() const; 110 inline bool carrayStyle() const; 111 112 private: 113 const void* mBuffer; 114 size_t mSize; 115 size_t mBytesPerLine; 116 int32_t mSingleLineCutoff; 117 size_t mAlignment; 118 bool mCArrayStyle; 119 }; 120 121 TextOutput& operator<<(TextOutput& to, const HexDump& val); 122 123 // --------------------------------------------------------------------------- 124 // No user servicable parts below. 125 126 inline TextOutput& endl(TextOutput& to) 127 { 128 to.print("\n", 1); 129 return to; 130 } 131 132 inline TextOutput& indent(TextOutput& to) 133 { 134 to.moveIndent(1); 135 return to; 136 } 137 138 inline TextOutput& dedent(TextOutput& to) 139 { 140 to.moveIndent(-1); 141 return to; 142 } 143 144 inline TextOutput& operator<<(TextOutput& to, const char* str) 145 { 146 to.print(str, strlen(str)); 147 return to; 148 } 149 150 inline TextOutput& operator<<(TextOutput& to, char c) 151 { 152 to.print(&c, 1); 153 return to; 154 } 155 156 inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) 157 { 158 return (*func)(to); 159 } 160 161 inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } 162 inline TypeCode::~TypeCode() { } 163 inline uint32_t TypeCode::typeCode() const { return mCode; } 164 165 inline HexDump::~HexDump() { } 166 167 inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { 168 mBytesPerLine = bytesPerLine; return *this; 169 } 170 inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { 171 mSingleLineCutoff = bytes; return *this; 172 } 173 inline HexDump& HexDump::setAlignment(size_t alignment) { 174 mAlignment = alignment; return *this; 175 } 176 inline HexDump& HexDump::setCArrayStyle(bool enabled) { 177 mCArrayStyle = enabled; return *this; 178 } 179 180 inline const void* HexDump::buffer() const { return mBuffer; } 181 inline size_t HexDump::size() const { return mSize; } 182 inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } 183 inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } 184 inline size_t HexDump::alignment() const { return mAlignment; } 185 inline bool HexDump::carrayStyle() const { return mCArrayStyle; } 186 187 // --------------------------------------------------------------------------- 188 }; // namespace android 189 190 #endif // ANDROID_TEXTOUTPUT_H 191