| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 17 | #include <binder/TextOutput.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 |  | 
| Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 19 | #include <binder/Debug.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 |  | 
| Jeff Sharkey | 8cb8925 | 2013-05-30 13:53:39 -0700 | [diff] [blame] | 21 | #include <utils/String8.h> | 
|  | 22 | #include <utils/String16.h> | 
|  | 23 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <stdio.h> | 
|  | 25 | #include <stdlib.h> | 
|  | 26 | #include <string.h> | 
|  | 27 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 28 | namespace android { | 
|  | 29 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | // --------------------------------------------------------------------------- | 
|  | 31 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 32 | TextOutput::TextOutput() { | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | TextOutput::~TextOutput() { | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | // --------------------------------------------------------------------------- | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 |  | 
|  | 40 | TextOutput& operator<<(TextOutput& to, bool val) | 
|  | 41 | { | 
|  | 42 | if (val) to.print("true", 4); | 
|  | 43 | else to.print("false", 5); | 
|  | 44 | return to; | 
|  | 45 | } | 
|  | 46 |  | 
|  | 47 | TextOutput& 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 |  | 
|  | 55 | TextOutput& 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 |  | 
|  | 63 | TextOutput& 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 |  | 
|  | 71 | TextOutput& 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 |  | 
|  | 79 | TextOutput& 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 |  | 
|  | 87 | TextOutput& 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 |  | 
|  | 95 | static 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 |  | 
|  | 107 | TextOutput& operator<<(TextOutput& to, float val) | 
|  | 108 | { | 
|  | 109 | return print_float(to,val); | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | TextOutput& operator<<(TextOutput& to, double val) | 
|  | 113 | { | 
|  | 114 | return print_float(to,val); | 
|  | 115 | } | 
|  | 116 |  | 
|  | 117 | TextOutput& 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 Sharkey | 8cb8925 | 2013-05-30 13:53:39 -0700 | [diff] [blame] | 125 | TextOutput& operator<<(TextOutput& to, const String8& val) | 
|  | 126 | { | 
|  | 127 | to << val.string(); | 
|  | 128 | return to; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | TextOutput& operator<<(TextOutput& to, const String16& val) | 
|  | 132 | { | 
|  | 133 | to << String8(val).string(); | 
|  | 134 | return to; | 
|  | 135 | } | 
|  | 136 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | static void textOutputPrinter(void* cookie, const char* txt) | 
|  | 138 | { | 
|  | 139 | ((TextOutput*)cookie)->print(txt, strlen(txt)); | 
|  | 140 | } | 
|  | 141 |  | 
|  | 142 | TextOutput& operator<<(TextOutput& to, const TypeCode& val) | 
|  | 143 | { | 
|  | 144 | printTypeCode(val.typeCode(), textOutputPrinter, (void*)&to); | 
|  | 145 | return to; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | HexDump::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 |  | 
|  | 161 | TextOutput& 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 |