blob: 851e01f4b87e157b870ec68dc0c540a8132c5c7a [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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>
Wei Wang78f2a372016-10-20 23:18:17 -070021#include <utils/String8.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080022
23#include <stdint.h>
24#include <string.h>
Wei Wang13c41d32016-10-19 10:23:59 -070025#include <sstream>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27// ---------------------------------------------------------------------------
28namespace android {
29
30class TextOutput
31{
32public:
Mathias Agopian83c04462009-05-22 19:00:22 -070033 TextOutput();
34 virtual ~TextOutput();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080035
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
Wei Wang13c41d32016-10-19 10:23:59 -070068template<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}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080077
Wei Wang13c41d32016-10-19 10:23:59 -070078TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
79
80class TypeCode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080081{
82public:
83 inline TypeCode(uint32_t code);
84 inline ~TypeCode();
85
86 inline uint32_t typeCode() const;
Wei Wang13c41d32016-10-19 10:23:59 -070087
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080088private:
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);
Wei Wang78f2a372016-10-20 23:18:17 -0700122inline 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}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800148
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
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800170inline 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