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