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_HARDWARE_TEXTOUTPUT_H
18 #define ANDROID_HARDWARE_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 namespace hardware {
30 
31 class TextOutput
32 {
33 public:
34                         TextOutput();
35     virtual             ~TextOutput();
36 
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:
Bundle(TextOutput & to)42         inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); }
~Bundle()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).
55 extern TextOutput& alog;
56 
57 // Text output stream for printing to stdout.
58 extern TextOutput& aout;
59 
60 // Text output stream for printing to stderr.
61 extern TextOutput& aerr;
62 
63 typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
64 
65 TextOutput& endl(TextOutput& to);
66 TextOutput& indent(TextOutput& to);
67 TextOutput& dedent(TextOutput& to);
68 
69 template<typename T>
70 TextOutput& operator<<(TextOutput& to, const T& val)
71 {
72     std::stringstream strbuf;
73     strbuf << val;
74     std::string str = strbuf.str();
75     to.print(str.c_str(), str.size());
76     return to;
77 }
78 
79 TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
80 
81 class TypeCode
82 {
83 public:
84     inline TypeCode(uint32_t code);
85     inline ~TypeCode();
86 
87     inline uint32_t typeCode() const;
88 
89 private:
90     uint32_t mCode;
91 };
92 
93 TextOutput& operator<<(TextOutput& to, const TypeCode& val);
94 
95 class HexDump
96 {
97 public:
98     HexDump(const void *buf, size_t size, size_t bytesPerLine=16);
99     inline ~HexDump();
100 
101     inline HexDump& setBytesPerLine(size_t bytesPerLine);
102     inline HexDump& setSingleLineCutoff(int32_t bytes);
103     inline HexDump& setAlignment(size_t alignment);
104     inline HexDump& setCArrayStyle(bool enabled);
105 
106     inline const void* buffer() const;
107     inline size_t size() const;
108     inline size_t bytesPerLine() const;
109     inline int32_t singleLineCutoff() const;
110     inline size_t alignment() const;
111     inline bool carrayStyle() const;
112 
113 private:
114     const void* mBuffer;
115     size_t mSize;
116     size_t mBytesPerLine;
117     int32_t mSingleLineCutoff;
118     size_t mAlignment;
119     bool mCArrayStyle;
120 };
121 
122 TextOutput& operator<<(TextOutput& to, const HexDump& val);
123 inline TextOutput& operator<<(TextOutput& to,
124                               decltype(std::endl<char,
125                                        std::char_traits<char>>)
126                               /*val*/) {
127     endl(to);
128     return to;
129 }
130 
131 inline TextOutput& operator<<(TextOutput& to, const char &c)
132 {
133     to.print(&c, 1);
134     return to;
135 }
136 
137 inline TextOutput& operator<<(TextOutput& to, const bool &val)
138 {
139     if (val) to.print("true", 4);
140     else to.print("false", 5);
141     return to;
142 }
143 
144 inline TextOutput& operator<<(TextOutput& to, const String16& val)
145 {
146     to << String8(val).string();
147     return to;
148 }
149 
150 // ---------------------------------------------------------------------------
151 // No user servicable parts below.
152 
endl(TextOutput & to)153 inline TextOutput& endl(TextOutput& to)
154 {
155     to.print("\n", 1);
156     return to;
157 }
158 
indent(TextOutput & to)159 inline TextOutput& indent(TextOutput& to)
160 {
161     to.moveIndent(1);
162     return to;
163 }
164 
dedent(TextOutput & to)165 inline TextOutput& dedent(TextOutput& to)
166 {
167     to.moveIndent(-1);
168     return to;
169 }
170 
171 inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
172 {
173     return (*func)(to);
174 }
175 
TypeCode(uint32_t code)176 inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
~TypeCode()177 inline TypeCode::~TypeCode() { }
typeCode()178 inline uint32_t TypeCode::typeCode() const { return mCode; }
179 
~HexDump()180 inline HexDump::~HexDump() { }
181 
setBytesPerLine(size_t bytesPerLine)182 inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
183     mBytesPerLine = bytesPerLine; return *this;
184 }
setSingleLineCutoff(int32_t bytes)185 inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
186     mSingleLineCutoff = bytes; return *this;
187 }
setAlignment(size_t alignment)188 inline HexDump& HexDump::setAlignment(size_t alignment) {
189     mAlignment = alignment; return *this;
190 }
setCArrayStyle(bool enabled)191 inline HexDump& HexDump::setCArrayStyle(bool enabled) {
192     mCArrayStyle = enabled; return *this;
193 }
194 
buffer()195 inline const void* HexDump::buffer() const { return mBuffer; }
size()196 inline size_t HexDump::size() const { return mSize; }
bytesPerLine()197 inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
singleLineCutoff()198 inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
alignment()199 inline size_t HexDump::alignment() const { return mAlignment; }
carrayStyle()200 inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
201 
202 // ---------------------------------------------------------------------------
203 }; // namespace hardware
204 }; // namespace android
205 
206 #endif // ANDROID_HARDWARE_TEXTOUTPUT_H
207