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