blob: 0e9975a22c452a323f8eb50db2fbcd1fcfe1cf9b [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>
Wei Wang13c41d32016-10-19 10:23:59 -070024#include <sstream>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080025
26// ---------------------------------------------------------------------------
27namespace android {
28
Jeff Sharkey8cb89252013-05-30 13:53:39 -070029class String8;
30class String16;
31
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032class TextOutput
33{
34public:
Mathias Agopian83c04462009-05-22 19:00:22 -070035 TextOutput();
36 virtual ~TextOutput();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080037
38 virtual status_t print(const char* txt, size_t len) = 0;
39 virtual void moveIndent(int delta) = 0;
40
41 class Bundle {
42 public:
43 inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); }
44 inline ~Bundle() { mTO.popBundle(); }
45 private:
46 TextOutput& mTO;
47 };
48
49 virtual void pushBundle() = 0;
50 virtual void popBundle() = 0;
51};
52
53// ---------------------------------------------------------------------------
54
55// Text output stream for printing to the log (via utils/Log.h).
56extern TextOutput& alog;
57
58// Text output stream for printing to stdout.
59extern TextOutput& aout;
60
61// Text output stream for printing to stderr.
62extern TextOutput& aerr;
63
64typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
65
66TextOutput& endl(TextOutput& to);
67TextOutput& indent(TextOutput& to);
68TextOutput& dedent(TextOutput& to);
69
Wei Wang13c41d32016-10-19 10:23:59 -070070template<typename T>
71TextOutput& operator<<(TextOutput& to, const T& val)
72{
73 std::stringstream strbuf;
74 strbuf << val;
75 std::string str = strbuf.str();
76 to.print(str.c_str(), str.size());
77 return to;
78}
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080079
Wei Wang13c41d32016-10-19 10:23:59 -070080TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
81
82class TypeCode
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080083{
84public:
85 inline TypeCode(uint32_t code);
86 inline ~TypeCode();
87
88 inline uint32_t typeCode() const;
Wei Wang13c41d32016-10-19 10:23:59 -070089
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080090private:
91 uint32_t mCode;
92};
93
94TextOutput& operator<<(TextOutput& to, const TypeCode& val);
95
96class HexDump
97{
98public:
99 HexDump(const void *buf, size_t size, size_t bytesPerLine=16);
100 inline ~HexDump();
101
102 inline HexDump& setBytesPerLine(size_t bytesPerLine);
103 inline HexDump& setSingleLineCutoff(int32_t bytes);
104 inline HexDump& setAlignment(size_t alignment);
105 inline HexDump& setCArrayStyle(bool enabled);
106
107 inline const void* buffer() const;
108 inline size_t size() const;
109 inline size_t bytesPerLine() const;
110 inline int32_t singleLineCutoff() const;
111 inline size_t alignment() const;
112 inline bool carrayStyle() const;
113
114private:
115 const void* mBuffer;
116 size_t mSize;
117 size_t mBytesPerLine;
118 int32_t mSingleLineCutoff;
119 size_t mAlignment;
120 bool mCArrayStyle;
121};
122
123TextOutput& operator<<(TextOutput& to, const HexDump& val);
124
125// ---------------------------------------------------------------------------
126// No user servicable parts below.
127
128inline TextOutput& endl(TextOutput& to)
129{
130 to.print("\n", 1);
131 return to;
132}
133
134inline TextOutput& indent(TextOutput& to)
135{
136 to.moveIndent(1);
137 return to;
138}
139
140inline TextOutput& dedent(TextOutput& to)
141{
142 to.moveIndent(-1);
143 return to;
144}
145
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800146inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
147{
148 return (*func)(to);
149}
150
151inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
152inline TypeCode::~TypeCode() { }
153inline uint32_t TypeCode::typeCode() const { return mCode; }
154
155inline HexDump::~HexDump() { }
156
157inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
158 mBytesPerLine = bytesPerLine; return *this;
159}
160inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
161 mSingleLineCutoff = bytes; return *this;
162}
163inline HexDump& HexDump::setAlignment(size_t alignment) {
164 mAlignment = alignment; return *this;
165}
166inline HexDump& HexDump::setCArrayStyle(bool enabled) {
167 mCArrayStyle = enabled; return *this;
168}
169
170inline const void* HexDump::buffer() const { return mBuffer; }
171inline size_t HexDump::size() const { return mSize; }
172inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
173inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
174inline size_t HexDump::alignment() const { return mAlignment; }
175inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
176
177// ---------------------------------------------------------------------------
178}; // namespace android
179
180#endif // ANDROID_TEXTOUTPUT_H