2 * Copyright (C) 2005 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <binder/TextOutput.h>
19 #include <binder/Debug.h>
21 #include <utils/String8.h>
22 #include <utils/String16.h>
30 // ---------------------------------------------------------------------------
32 TextOutput::TextOutput() {
35 TextOutput::~TextOutput() {
38 // ---------------------------------------------------------------------------
40 TextOutput& operator<<(TextOutput& to, bool val)
42 if (val) to.print("true", 4);
43 else to.print("false", 5);
47 TextOutput& operator<<(TextOutput& to, int val)
50 sprintf(buf, "%d", val);
51 to.print(buf, strlen(buf));
55 TextOutput& operator<<(TextOutput& to, long val)
58 sprintf(buf, "%ld", val);
59 to.print(buf, strlen(buf));
63 TextOutput& operator<<(TextOutput& to, unsigned int val)
66 sprintf(buf, "%u", val);
67 to.print(buf, strlen(buf));
71 TextOutput& operator<<(TextOutput& to, unsigned long val)
74 sprintf(buf, "%lu", val);
75 to.print(buf, strlen(buf));
79 TextOutput& operator<<(TextOutput& to, long long val)
82 sprintf(buf, "%Ld", val);
83 to.print(buf, strlen(buf));
87 TextOutput& operator<<(TextOutput& to, unsigned long long val)
90 sprintf(buf, "%Lu", val);
91 to.print(buf, strlen(buf));
95 static TextOutput& print_float(TextOutput& to, double value)
98 sprintf(buf, "%g", value);
99 if( !strchr(buf, '.') && !strchr(buf, 'e') &&
100 !strchr(buf, 'E') ) {
101 strncat(buf, ".0", sizeof(buf)-1);
103 to.print(buf, strlen(buf));
107 TextOutput& operator<<(TextOutput& to, float val)
109 return print_float(to,val);
112 TextOutput& operator<<(TextOutput& to, double val)
114 return print_float(to,val);
117 TextOutput& operator<<(TextOutput& to, const void* val)
120 snprintf(buf, sizeof(buf), "%p", val);
121 to.print(buf, strlen(buf));
125 TextOutput& operator<<(TextOutput& to, const String8& val)
131 TextOutput& operator<<(TextOutput& to, const String16& val)
133 to << String8(val).string();
137 static void textOutputPrinter(void* cookie, const char* txt)
139 ((TextOutput*)cookie)->print(txt, strlen(txt));
142 TextOutput& operator<<(TextOutput& to, const TypeCode& val)
144 printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to);
148 HexDump::HexDump(const void *buf, size_t size, size_t bytesPerLine)
151 , mBytesPerLine(bytesPerLine)
152 , mSingleLineCutoff(16)
154 , mCArrayStyle(false)
156 if (bytesPerLine >= 16) mAlignment = 4;
157 else if (bytesPerLine >= 8) mAlignment = 2;
161 TextOutput& operator<<(TextOutput& to, const HexDump& val)
163 printHexData(0, val.buffer(), val.size(), val.bytesPerLine(),
164 val.singleLineCutoff(), val.alignment(), val.carrayStyle(),
165 textOutputPrinter, (void*)&to);
169 }; // namespace android