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