blob: 9611ddb5e1b53e4cdc552e5c178a8b282d604779 [file] [log] [blame]
Mathias Agopian4ea13dc2013-05-06 20:20:50 -07001/*
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
Martijn Coenenf75a23d2016-08-01 11:55:17 +020017#ifndef ANDROID_HARDWARE_TEXTOUTPUT_H
18#define ANDROID_HARDWARE_TEXTOUTPUT_H
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070019
20#include <utils/Errors.h>
Wei Wanga3ff0072016-10-21 11:08:07 -070021#include <utils/String8.h>
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070022
23#include <stdint.h>
24#include <string.h>
Wei Wang8a2e8ac2016-10-14 09:54:27 -070025#include <sstream>
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070026
27// ---------------------------------------------------------------------------
28namespace android {
Martijn Coenenf75a23d2016-08-01 11:55:17 +020029namespace hardware {
Jeff Sharkey69c328d2013-05-30 13:53:39 -070030
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070031class TextOutput
32{
33public:
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:
Chih-Hung Hsieh6b684f42018-12-20 15:42:22 -080042 inline explicit Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); }
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070043 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
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070057typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
58
59TextOutput& endl(TextOutput& to);
60TextOutput& indent(TextOutput& to);
61TextOutput& dedent(TextOutput& to);
62
Wei Wang8a2e8ac2016-10-14 09:54:27 -070063template<typename T>
64TextOutput& operator<<(TextOutput& to, const T& val)
65{
66 std::stringstream strbuf;
67 strbuf << val;
68 std::string str = strbuf.str();
69 to.print(str.c_str(), str.size());
70 return to;
71}
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070072
Wei Wang8a2e8ac2016-10-14 09:54:27 -070073TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
74
75class TypeCode
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070076{
77public:
Chih-Hung Hsieh6b684f42018-12-20 15:42:22 -080078 inline explicit TypeCode(uint32_t code);
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070079 inline ~TypeCode();
80
81 inline uint32_t typeCode() const;
Wei Wang8a2e8ac2016-10-14 09:54:27 -070082
Mathias Agopian4ea13dc2013-05-06 20:20:50 -070083private:
84 uint32_t mCode;
85};
86
87TextOutput& operator<<(TextOutput& to, const TypeCode& val);
88
89class HexDump
90{
91public:
92 HexDump(const void *buf, size_t size, size_t bytesPerLine=16);
93 inline ~HexDump();
94
95 inline HexDump& setBytesPerLine(size_t bytesPerLine);
96 inline HexDump& setSingleLineCutoff(int32_t bytes);
97 inline HexDump& setAlignment(size_t alignment);
98 inline HexDump& setCArrayStyle(bool enabled);
99
100 inline const void* buffer() const;
101 inline size_t size() const;
102 inline size_t bytesPerLine() const;
103 inline int32_t singleLineCutoff() const;
104 inline size_t alignment() const;
105 inline bool carrayStyle() const;
106
107private:
108 const void* mBuffer;
109 size_t mSize;
110 size_t mBytesPerLine;
111 int32_t mSingleLineCutoff;
112 size_t mAlignment;
113 bool mCArrayStyle;
114};
115
116TextOutput& operator<<(TextOutput& to, const HexDump& val);
Wei Wanga3ff0072016-10-21 11:08:07 -0700117inline TextOutput& operator<<(TextOutput& to,
118 decltype(std::endl<char,
119 std::char_traits<char>>)
120 /*val*/) {
121 endl(to);
122 return to;
123}
124
125inline TextOutput& operator<<(TextOutput& to, const char &c)
126{
127 to.print(&c, 1);
128 return to;
129}
130
131inline TextOutput& operator<<(TextOutput& to, const bool &val)
132{
133 if (val) to.print("true", 4);
134 else to.print("false", 5);
135 return to;
136}
137
138inline TextOutput& operator<<(TextOutput& to, const String16& val)
139{
140 to << String8(val).string();
141 return to;
142}
Mathias Agopian4ea13dc2013-05-06 20:20:50 -0700143
144// ---------------------------------------------------------------------------
145// No user servicable parts below.
146
147inline TextOutput& endl(TextOutput& to)
148{
149 to.print("\n", 1);
150 return to;
151}
152
153inline TextOutput& indent(TextOutput& to)
154{
155 to.moveIndent(1);
156 return to;
157}
158
159inline TextOutput& dedent(TextOutput& to)
160{
161 to.moveIndent(-1);
162 return to;
163}
164
Mathias Agopian4ea13dc2013-05-06 20:20:50 -0700165inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
166{
167 return (*func)(to);
168}
169
170inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
171inline TypeCode::~TypeCode() { }
172inline uint32_t TypeCode::typeCode() const { return mCode; }
173
174inline HexDump::~HexDump() { }
175
176inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
177 mBytesPerLine = bytesPerLine; return *this;
178}
179inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
180 mSingleLineCutoff = bytes; return *this;
181}
182inline HexDump& HexDump::setAlignment(size_t alignment) {
183 mAlignment = alignment; return *this;
184}
185inline HexDump& HexDump::setCArrayStyle(bool enabled) {
186 mCArrayStyle = enabled; return *this;
187}
188
189inline const void* HexDump::buffer() const { return mBuffer; }
190inline size_t HexDump::size() const { return mSize; }
191inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
192inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
193inline size_t HexDump::alignment() const { return mAlignment; }
194inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
195
196// ---------------------------------------------------------------------------
Steven Moreland7173a4c2019-09-26 15:55:02 -0700197} // namespace hardware
198} // namespace android
Mathias Agopian4ea13dc2013-05-06 20:20:50 -0700199
Martijn Coenenf75a23d2016-08-01 11:55:17 +0200200#endif // ANDROID_HARDWARE_TEXTOUTPUT_H