blob: db3e858a450df6267514328dce6de5bed6d7b346 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2005 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
Mathias Agopian002e1e52013-05-06 20:20:50 -070017#include <binder/TextOutput.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080018
Mathias Agopian002e1e52013-05-06 20:20:50 -070019#include <binder/Debug.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080020
Jeff Sharkey8cb89252013-05-30 13:53:39 -070021#include <utils/String8.h>
22#include <utils/String16.h>
23
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
Mathias Agopian83c04462009-05-22 19:00:22 -070028namespace android {
29
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080030// ---------------------------------------------------------------------------
31
Mathias Agopian83c04462009-05-22 19:00:22 -070032TextOutput::TextOutput() {
33}
34
35TextOutput::~TextOutput() {
36}
37
38// ---------------------------------------------------------------------------
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080039
40TextOutput& operator<<(TextOutput& to, bool val)
41{
42 if (val) to.print("true", 4);
43 else to.print("false", 5);
44 return to;
45}
46
47TextOutput& operator<<(TextOutput& to, int val)
48{
49 char buf[16];
50 sprintf(buf, "%d", val);
51 to.print(buf, strlen(buf));
52 return to;
53}
54
55TextOutput& operator<<(TextOutput& to, long val)
56{
57 char buf[16];
58 sprintf(buf, "%ld", val);
59 to.print(buf, strlen(buf));
60 return to;
61}
62
63TextOutput& operator<<(TextOutput& to, unsigned int val)
64{
65 char buf[16];
66 sprintf(buf, "%u", val);
67 to.print(buf, strlen(buf));
68 return to;
69}
70
71TextOutput& operator<<(TextOutput& to, unsigned long val)
72{
73 char buf[16];
74 sprintf(buf, "%lu", val);
75 to.print(buf, strlen(buf));
76 return to;
77}
78
79TextOutput& operator<<(TextOutput& to, long long val)
80{
81 char buf[32];
82 sprintf(buf, "%Ld", val);
83 to.print(buf, strlen(buf));
84 return to;
85}
86
87TextOutput& operator<<(TextOutput& to, unsigned long long val)
88{
89 char buf[32];
90 sprintf(buf, "%Lu", val);
91 to.print(buf, strlen(buf));
92 return to;
93}
94
95static TextOutput& print_float(TextOutput& to, double value)
96{
97 char buf[64];
98 sprintf(buf, "%g", value);
99 if( !strchr(buf, '.') && !strchr(buf, 'e') &&
100 !strchr(buf, 'E') ) {
101 strncat(buf, ".0", sizeof(buf)-1);
102 }
103 to.print(buf, strlen(buf));
104 return to;
105}
106
107TextOutput& operator<<(TextOutput& to, float val)
108{
109 return print_float(to,val);
110}
111
112TextOutput& operator<<(TextOutput& to, double val)
113{
114 return print_float(to,val);
115}
116
117TextOutput& operator<<(TextOutput& to, const void* val)
118{
119 char buf[16];
120 sprintf(buf, "%p", val);
121 to.print(buf, strlen(buf));
122 return to;
123}
124
Jeff Sharkey8cb89252013-05-30 13:53:39 -0700125TextOutput& operator<<(TextOutput& to, const String8& val)
126{
127 to << val.string();
128 return to;
129}
130
131TextOutput& operator<<(TextOutput& to, const String16& val)
132{
133 to << String8(val).string();
134 return to;
135}
136
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800137static void textOutputPrinter(void* cookie, const char* txt)
138{
139 ((TextOutput*)cookie)->print(txt, strlen(txt));
140}
141
142TextOutput& operator<<(TextOutput& to, const TypeCode& val)
143{
144 printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to);
145 return to;
146}
147
148HexDump::HexDump(const void *buf, size_t size, size_t bytesPerLine)
149 : mBuffer(buf)
150 , mSize(size)
151 , mBytesPerLine(bytesPerLine)
152 , mSingleLineCutoff(16)
153 , mAlignment(4)
154 , mCArrayStyle(false)
155{
156 if (bytesPerLine >= 16) mAlignment = 4;
157 else if (bytesPerLine >= 8) mAlignment = 2;
158 else mAlignment = 1;
159}
160
161TextOutput& operator<<(TextOutput& to, const HexDump& val)
162{
163 printHexData(0, val.buffer(), val.size(), val.bytesPerLine(),
164 val.singleLineCutoff(), val.alignment(), val.carrayStyle(),
165 textOutputPrinter, (void*)&to);
166 return to;
167}
168
169}; // namespace android