blob: 84af2930bfb847d66e8005f0791b1a0cc877e940 [file] [log] [blame]
Igor Murashkinec79ef22013-10-24 17:09:15 -07001/*
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
Igor Murashkinec79ef22013-10-24 17:09:15 -070028namespace android {
29
30/*
31 * Implementation of Printer
32 */
33Printer::Printer() {
34 // Intentionally left empty
35}
36
37Printer::~Printer() {
38 // Intentionally left empty
39}
40
41void Printer::printFormatLine(const char* format, ...) {
42 va_list arglist;
43 va_start(arglist, format);
44
45 char* formattedString;
Igor Murashkine65b7ea2013-10-30 18:09:52 -070046
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070047#ifndef _WIN32
Igor Murashkinec79ef22013-10-24 17:09:15 -070048 if (vasprintf(&formattedString, format, arglist) < 0) { // returns -1 on error
49 ALOGE("%s: Failed to format string", __FUNCTION__);
Mikhail Lappo90f5d642017-03-23 22:32:30 +010050 va_end(arglist);
Igor Murashkinec79ef22013-10-24 17:09:15 -070051 return;
52 }
Igor Murashkine65b7ea2013-10-30 18:09:52 -070053#else
Mikhail Lappo90f5d642017-03-23 22:32:30 +010054 va_end(arglist);
Igor Murashkine65b7ea2013-10-30 18:09:52 -070055 return;
56#endif
57
Igor Murashkinec79ef22013-10-24 17:09:15 -070058 va_end(arglist);
59
60 printLine(formattedString);
61 free(formattedString);
62}
63
64/*
65 * Implementation of LogPrinter
66 */
67LogPrinter::LogPrinter(const char* logtag,
68 android_LogPriority priority,
69 const char* prefix,
70 bool ignoreBlankLines) :
71 mLogTag(logtag),
72 mPriority(priority),
73 mPrefix(prefix ?: ""),
74 mIgnoreBlankLines(ignoreBlankLines) {
75}
76
77void LogPrinter::printLine(const char* string) {
78 if (string == NULL) {
79 ALOGW("%s: NULL string passed in", __FUNCTION__);
80 return;
81 }
82
83 if (mIgnoreBlankLines || (*string)) {
84 // Simple case: Line is not blank, or we don't care about printing blank lines
85 printRaw(string);
86 } else {
87 // Force logcat to print empty lines by adding prefixing with a space
88 printRaw(" ");
89 }
90}
91
92void LogPrinter::printRaw(const char* string) {
93 __android_log_print(mPriority, mLogTag, "%s%s", mPrefix, string);
94}
95
96
97/*
98 * Implementation of FdPrinter
99 */
100FdPrinter::FdPrinter(int fd, unsigned int indent, const char* prefix) :
101 mFd(fd), mIndent(indent), mPrefix(prefix ?: "") {
102
103 if (fd < 0) {
104 ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, fd);
105 }
106
107 // <indent><prefix><line> -- e.g. '%-4s%s\n' for indent=4
108 snprintf(mFormatString, sizeof(mFormatString), "%%-%us%%s\n", mIndent);
109}
110
111void FdPrinter::printLine(const char* string) {
112 if (string == NULL) {
113 ALOGW("%s: NULL string passed in", __FUNCTION__);
114 return;
115 } else if (mFd < 0) {
116 ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, mFd);
117 return;
118 }
119
Elliott Hughes34a4f0b2016-10-05 09:37:18 -0700120#ifndef _WIN32
Elliott Hughesdcc98da2014-05-22 01:23:29 -0700121 dprintf(mFd, mFormatString, mPrefix, string);
Igor Murashkine65b7ea2013-10-30 18:09:52 -0700122#endif
Igor Murashkinec79ef22013-10-24 17:09:15 -0700123}
124
125/*
126 * Implementation of String8Printer
127 */
128String8Printer::String8Printer(String8* target, const char* prefix) :
129 mTarget(target),
130 mPrefix(prefix ?: "") {
131
132 if (target == NULL) {
133 ALOGW("%s: Target string was NULL", __FUNCTION__);
134 }
135}
136
137void String8Printer::printLine(const char* string) {
138 if (string == NULL) {
139 ALOGW("%s: NULL string passed in", __FUNCTION__);
140 return;
141 } else if (mTarget == NULL) {
142 ALOGW("%s: Target string was NULL", __FUNCTION__);
143 return;
144 }
145
Christopher Ferris038ac692013-10-31 16:25:04 -0700146 mTarget->append(mPrefix);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700147 mTarget->append(string);
148 mTarget->append("\n");
149}
150
151/*
152 * Implementation of PrefixPrinter
153 */
154PrefixPrinter::PrefixPrinter(Printer& printer, const char* prefix) :
155 mPrinter(printer), mPrefix(prefix ?: "") {
156}
157
158void PrefixPrinter::printLine(const char* string) {
159 mPrinter.printFormatLine("%s%s", mPrefix, string);
160}
161
162}; //namespace android