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#include <utils/String8.h> 22 23#include <stdint.h> 24#include <string.h> 25#include <sstream> 26 27// --------------------------------------------------------------------------- 28namespace android { 29 30class TextOutput 31{ 32public: 33 TextOutput(); 34 virtual ~TextOutput(); 35 36 virtual status_t print(const char* txt, size_t len) = 0; 37 virtual void moveIndent(int delta) = 0; 38 39 class Bundle { 40 public: 41 inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); } 42 inline ~Bundle() { mTO.popBundle(); } 43 private: 44 TextOutput& mTO; 45 }; 46 47 virtual void pushBundle() = 0; 48 virtual void popBundle() = 0; 49}; 50 51// --------------------------------------------------------------------------- 52 53// Text output stream for printing to the log (via utils/Log.h). 54extern TextOutput& alog; 55 56// Text output stream for printing to stdout. 57extern TextOutput& aout; 58 59// Text output stream for printing to stderr. 60extern TextOutput& aerr; 61 62typedef TextOutput& (*TextOutputManipFunc)(TextOutput&); 63 64TextOutput& endl(TextOutput& to); 65TextOutput& indent(TextOutput& to); 66TextOutput& dedent(TextOutput& to); 67 68template<typename T> 69TextOutput& operator<<(TextOutput& to, const T& val) 70{ 71 std::stringstream strbuf; 72 strbuf << val; 73 std::string str = strbuf.str(); 74 to.print(str.c_str(), str.size()); 75 return to; 76} 77 78TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func); 79 80class TypeCode 81{ 82public: 83 inline TypeCode(uint32_t code); 84 inline ~TypeCode(); 85 86 inline uint32_t typeCode() const; 87 88private: 89 uint32_t mCode; 90}; 91 92TextOutput& operator<<(TextOutput& to, const TypeCode& val); 93 94class HexDump 95{ 96public: 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 112private: 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 121TextOutput& operator<<(TextOutput& to, const HexDump& val); 122inline TextOutput& operator<<(TextOutput& to, 123 decltype(std::endl<char, 124 std::char_traits<char>>) 125 /*val*/) { 126 endl(to); 127 return to; 128} 129 130inline TextOutput& operator<<(TextOutput& to, const char &c) 131{ 132 to.print(&c, 1); 133 return to; 134} 135 136inline TextOutput& operator<<(TextOutput& to, const bool &val) 137{ 138 if (val) to.print("true", 4); 139 else to.print("false", 5); 140 return to; 141} 142 143inline TextOutput& operator<<(TextOutput& to, const String16& val) 144{ 145 to << String8(val).string(); 146 return to; 147} 148 149// --------------------------------------------------------------------------- 150// No user servicable parts below. 151 152inline TextOutput& endl(TextOutput& to) 153{ 154 to.print("\n", 1); 155 return to; 156} 157 158inline TextOutput& indent(TextOutput& to) 159{ 160 to.moveIndent(1); 161 return to; 162} 163 164inline TextOutput& dedent(TextOutput& to) 165{ 166 to.moveIndent(-1); 167 return to; 168} 169 170inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func) 171{ 172 return (*func)(to); 173} 174 175inline TypeCode::TypeCode(uint32_t code) : mCode(code) { } 176inline TypeCode::~TypeCode() { } 177inline uint32_t TypeCode::typeCode() const { return mCode; } 178 179inline HexDump::~HexDump() { } 180 181inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) { 182 mBytesPerLine = bytesPerLine; return *this; 183} 184inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) { 185 mSingleLineCutoff = bytes; return *this; 186} 187inline HexDump& HexDump::setAlignment(size_t alignment) { 188 mAlignment = alignment; return *this; 189} 190inline HexDump& HexDump::setCArrayStyle(bool enabled) { 191 mCArrayStyle = enabled; return *this; 192} 193 194inline const void* HexDump::buffer() const { return mBuffer; } 195inline size_t HexDump::size() const { return mSize; } 196inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; } 197inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; } 198inline size_t HexDump::alignment() const { return mAlignment; } 199inline bool HexDump::carrayStyle() const { return mCArrayStyle; } 200 201// --------------------------------------------------------------------------- 202}; // namespace android 203 204#endif // ANDROID_TEXTOUTPUT_H 205