• 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 <utils/Errors.h>
20 #include <utils/String8.h>
21 
22 #include <stdint.h>
23 #include <string.h>
24 #include <sstream>
25 
26 // ---------------------------------------------------------------------------
27 namespace android {
28 
29 class TextOutput
30 {
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 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 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 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 TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
84 
85 class TypeCode
86 {
87 public:
88     inline explicit TypeCode(uint32_t code);
89     inline ~TypeCode();
90 
91     inline uint32_t typeCode() const;
92 
93 private:
94     uint32_t mCode;
95 };
96 
97 TextOutput& operator<<(TextOutput& to, const TypeCode& val);
98 
99 class HexDump
100 {
101 public:
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 
117 private:
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 
126 TextOutput& operator<<(TextOutput& to, const HexDump& val);
127 inline TextOutput& operator<<(TextOutput& to,
128                               decltype(std::endl<char,
129                                        std::char_traits<char>>)
130                               /*val*/) {
131     endl(to);
132     return to;
133 }
134 
135 inline TextOutput& operator<<(TextOutput& to, const char &c)
136 {
137     to.print(&c, 1);
138     return to;
139 }
140 
141 inline TextOutput& operator<<(TextOutput& to, const bool &val)
142 {
143     if (val) to.print("true", 4);
144     else to.print("false", 5);
145     return to;
146 }
147 
148 inline TextOutput& operator<<(TextOutput& to, const String16& val)
149 {
150     to << String8(val).string();
151     return to;
152 }
153 
154 // ---------------------------------------------------------------------------
155 // No user servicable parts below.
156 
endl(TextOutput & to)157 inline TextOutput& endl(TextOutput& to)
158 {
159     to.print("\n", 1);
160     return to;
161 }
162 
indent(TextOutput & to)163 inline TextOutput& indent(TextOutput& to)
164 {
165     to.moveIndent(1);
166     return to;
167 }
168 
dedent(TextOutput & to)169 inline TextOutput& dedent(TextOutput& to)
170 {
171     to.moveIndent(-1);
172     return to;
173 }
174 
175 inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
176 {
177     return (*func)(to);
178 }
179 
TypeCode(uint32_t code)180 inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
~TypeCode()181 inline TypeCode::~TypeCode() { }
typeCode()182 inline uint32_t TypeCode::typeCode() const { return mCode; }
183 
~HexDump()184 inline HexDump::~HexDump() { }
185 
setBytesPerLine(size_t bytesPerLine)186 inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
187     mBytesPerLine = bytesPerLine; return *this;
188 }
setSingleLineCutoff(int32_t bytes)189 inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
190     mSingleLineCutoff = bytes; return *this;
191 }
setAlignment(size_t alignment)192 inline HexDump& HexDump::setAlignment(size_t alignment) {
193     mAlignment = alignment; return *this;
194 }
setCArrayStyle(bool enabled)195 inline HexDump& HexDump::setCArrayStyle(bool enabled) {
196     mCArrayStyle = enabled; return *this;
197 }
198 
buffer()199 inline const void* HexDump::buffer() const { return mBuffer; }
size()200 inline size_t HexDump::size() const { return mSize; }
bytesPerLine()201 inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
singleLineCutoff()202 inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
alignment()203 inline size_t HexDump::alignment() const { return mAlignment; }
carrayStyle()204 inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
205 
206 // ---------------------------------------------------------------------------
207 } // namespace android
208