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 // ---------------------------------------------------------------------------
28 namespace android {
29
30 class TextOutput
31 {
32 public:
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:
Bundle(TextOutput & to)41 inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); }
~Bundle()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).
54 extern TextOutput& alog;
55
56 // Text output stream for printing to stdout.
57 extern TextOutput& aout;
58
59 // Text output stream for printing to stderr.
60 extern TextOutput& aerr;
61
62 typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
63
64 TextOutput& endl(TextOutput& to);
65 TextOutput& indent(TextOutput& to);
66 TextOutput& dedent(TextOutput& to);
67
68 template<typename T>
69 TextOutput& 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
78 TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
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 inline TextOutput& operator<<(TextOutput& to,
123 decltype(std::endl<char,
124 std::char_traits<char>>)
125 /*val*/) {
126 endl(to);
127 return to;
128 }
129
130 inline TextOutput& operator<<(TextOutput& to, const char &c)
131 {
132 to.print(&c, 1);
133 return to;
134 }
135
136 inline 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
143 inline 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
endl(TextOutput & to)152 inline TextOutput& endl(TextOutput& to)
153 {
154 to.print("\n", 1);
155 return to;
156 }
157
indent(TextOutput & to)158 inline TextOutput& indent(TextOutput& to)
159 {
160 to.moveIndent(1);
161 return to;
162 }
163
dedent(TextOutput & to)164 inline TextOutput& dedent(TextOutput& to)
165 {
166 to.moveIndent(-1);
167 return to;
168 }
169
170 inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
171 {
172 return (*func)(to);
173 }
174
TypeCode(uint32_t code)175 inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
~TypeCode()176 inline TypeCode::~TypeCode() { }
typeCode()177 inline uint32_t TypeCode::typeCode() const { return mCode; }
178
~HexDump()179 inline HexDump::~HexDump() { }
180
setBytesPerLine(size_t bytesPerLine)181 inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
182 mBytesPerLine = bytesPerLine; return *this;
183 }
setSingleLineCutoff(int32_t bytes)184 inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
185 mSingleLineCutoff = bytes; return *this;
186 }
setAlignment(size_t alignment)187 inline HexDump& HexDump::setAlignment(size_t alignment) {
188 mAlignment = alignment; return *this;
189 }
setCArrayStyle(bool enabled)190 inline HexDump& HexDump::setCArrayStyle(bool enabled) {
191 mCArrayStyle = enabled; return *this;
192 }
193
buffer()194 inline const void* HexDump::buffer() const { return mBuffer; }
size()195 inline size_t HexDump::size() const { return mSize; }
bytesPerLine()196 inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
singleLineCutoff()197 inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
alignment()198 inline size_t HexDump::alignment() const { return mAlignment; }
carrayStyle()199 inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
200
201 // ---------------------------------------------------------------------------
202 }; // namespace android
203
204 #endif // ANDROID_TEXTOUTPUT_H
205