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