blob: d8d86ba82cd690b0a11d9975da154726a42d81ae [file] [log] [blame]
The Android Open Source Projectd245d1d2008-10-21 07:00:00 -07001/*
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// ---------------------------------------------------------------------------
26namespace android {
27
28class TextOutput
29{
30public:
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).
52extern TextOutput& alog;
53
54// Text output stream for printing to stdout.
55extern TextOutput& aout;
56
57// Text output stream for printing to stderr.
58extern TextOutput& aerr;
59
60typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
61
62TextOutput& endl(TextOutput& to);
63TextOutput& indent(TextOutput& to);
64TextOutput& dedent(TextOutput& to);
65
66TextOutput& operator<<(TextOutput& to, const char* str);
67TextOutput& operator<<(TextOutput& to, char); // writes raw character
68TextOutput& operator<<(TextOutput& to, bool);
69TextOutput& operator<<(TextOutput& to, int);
70TextOutput& operator<<(TextOutput& to, long);
71TextOutput& operator<<(TextOutput& to, unsigned int);
72TextOutput& operator<<(TextOutput& to, unsigned long);
73TextOutput& operator<<(TextOutput& to, long long);
74TextOutput& operator<<(TextOutput& to, unsigned long long);
75TextOutput& operator<<(TextOutput& to, float);
76TextOutput& operator<<(TextOutput& to, double);
77TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
78TextOutput& operator<<(TextOutput& to, const void*);
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);
122
123// ---------------------------------------------------------------------------
124// No user servicable parts below.
125
126inline TextOutput& endl(TextOutput& to)
127{
128 to.print("\n", 1);
129 return to;
130}
131
132inline TextOutput& indent(TextOutput& to)
133{
134 to.moveIndent(1);
135 return to;
136}
137
138inline TextOutput& dedent(TextOutput& to)
139{
140 to.moveIndent(-1);
141 return to;
142}
143
144inline TextOutput& operator<<(TextOutput& to, const char* str)
145{
146 to.print(str, strlen(str));
147 return to;
148}
149
150inline TextOutput& operator<<(TextOutput& to, char c)
151{
152 to.print(&c, 1);
153 return to;
154}
155
156inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
157{
158 return (*func)(to);
159}
160
161inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
162inline TypeCode::~TypeCode() { }
163inline uint32_t TypeCode::typeCode() const { return mCode; }
164
165inline HexDump::~HexDump() { }
166
167inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
168 mBytesPerLine = bytesPerLine; return *this;
169}
170inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
171 mSingleLineCutoff = bytes; return *this;
172}
173inline HexDump& HexDump::setAlignment(size_t alignment) {
174 mAlignment = alignment; return *this;
175}
176inline HexDump& HexDump::setCArrayStyle(bool enabled) {
177 mCArrayStyle = enabled; return *this;
178}
179
180inline const void* HexDump::buffer() const { return mBuffer; }
181inline size_t HexDump::size() const { return mSize; }
182inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
183inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
184inline size_t HexDump::alignment() const { return mAlignment; }
185inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
186
187// ---------------------------------------------------------------------------
188}; // namespace android
189
190#endif // ANDROID_TEXTOUTPUT_H