Printer.cpp revision e65b7ea8801145626504c724c28aedd0e5038a28
1/* 2 * Copyright (C) 2013 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#define LOG_TAG "Printer" 18// #define LOG_NDEBUG 0 19 20#include <utils/Printer.h> 21#include <utils/String8.h> 22#include <utils/Log.h> 23 24#include <string.h> 25#include <stdio.h> 26#include <stdlib.h> 27 28#ifndef __BIONIC__ 29#define fdprintf dprintf 30#endif 31 32namespace android { 33 34/* 35 * Implementation of Printer 36 */ 37Printer::Printer() { 38 // Intentionally left empty 39} 40 41Printer::~Printer() { 42 // Intentionally left empty 43} 44 45void Printer::printFormatLine(const char* format, ...) { 46 va_list arglist; 47 va_start(arglist, format); 48 49 char* formattedString; 50 51#ifndef USE_MINGW 52 if (vasprintf(&formattedString, format, arglist) < 0) { // returns -1 on error 53 ALOGE("%s: Failed to format string", __FUNCTION__); 54 return; 55 } 56#else 57 return; 58#endif 59 60 va_end(arglist); 61 62 printLine(formattedString); 63 free(formattedString); 64} 65 66/* 67 * Implementation of LogPrinter 68 */ 69LogPrinter::LogPrinter(const char* logtag, 70 android_LogPriority priority, 71 const char* prefix, 72 bool ignoreBlankLines) : 73 mLogTag(logtag), 74 mPriority(priority), 75 mPrefix(prefix ?: ""), 76 mIgnoreBlankLines(ignoreBlankLines) { 77} 78 79void LogPrinter::printLine(const char* string) { 80 if (string == NULL) { 81 ALOGW("%s: NULL string passed in", __FUNCTION__); 82 return; 83 } 84 85 if (mIgnoreBlankLines || (*string)) { 86 // Simple case: Line is not blank, or we don't care about printing blank lines 87 printRaw(string); 88 } else { 89 // Force logcat to print empty lines by adding prefixing with a space 90 printRaw(" "); 91 } 92} 93 94void LogPrinter::printRaw(const char* string) { 95 __android_log_print(mPriority, mLogTag, "%s%s", mPrefix, string); 96} 97 98 99/* 100 * Implementation of FdPrinter 101 */ 102FdPrinter::FdPrinter(int fd, unsigned int indent, const char* prefix) : 103 mFd(fd), mIndent(indent), mPrefix(prefix ?: "") { 104 105 if (fd < 0) { 106 ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, fd); 107 } 108 109 // <indent><prefix><line> -- e.g. '%-4s%s\n' for indent=4 110 snprintf(mFormatString, sizeof(mFormatString), "%%-%us%%s\n", mIndent); 111} 112 113void FdPrinter::printLine(const char* string) { 114 if (string == NULL) { 115 ALOGW("%s: NULL string passed in", __FUNCTION__); 116 return; 117 } else if (mFd < 0) { 118 ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, mFd); 119 return; 120 } 121 122#ifndef USE_MINGW 123 fdprintf(mFd, mFormatString, mPrefix, string); 124#endif 125} 126 127/* 128 * Implementation of String8Printer 129 */ 130String8Printer::String8Printer(String8* target, const char* prefix) : 131 mTarget(target), 132 mPrefix(prefix ?: "") { 133 134 if (target == NULL) { 135 ALOGW("%s: Target string was NULL", __FUNCTION__); 136 } 137} 138 139void String8Printer::printLine(const char* string) { 140 if (string == NULL) { 141 ALOGW("%s: NULL string passed in", __FUNCTION__); 142 return; 143 } else if (mTarget == NULL) { 144 ALOGW("%s: Target string was NULL", __FUNCTION__); 145 return; 146 } 147 148 mTarget->append(string); 149 mTarget->append("\n"); 150} 151 152/* 153 * Implementation of PrefixPrinter 154 */ 155PrefixPrinter::PrefixPrinter(Printer& printer, const char* prefix) : 156 mPrinter(printer), mPrefix(prefix ?: "") { 157} 158 159void PrefixPrinter::printLine(const char* string) { 160 mPrinter.printFormatLine("%s%s", mPrefix, string); 161} 162 163}; //namespace android 164