blob: 9573a10c4f64e1331f3ebd950f2041e96a24e920 [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
Igor Murashkinec79ef22013-10-24 17:09:15 -070024namespace android {
25
26/*
27 * Implementation of Printer
28 */
29Printer::Printer() {
30 // Intentionally left empty
31}
32
33Printer::~Printer() {
34 // Intentionally left empty
35}
36
37void Printer::printFormatLine(const char* format, ...) {
38 va_list arglist;
39 va_start(arglist, format);
40
41 char* formattedString;
Igor Murashkine65b7ea2013-10-30 18:09:52 -070042
Elliott Hughes34a4f0b2016-10-05 09:37:18 -070043#ifndef _WIN32
Igor Murashkinec79ef22013-10-24 17:09:15 -070044 if (vasprintf(&formattedString, format, arglist) < 0) { // returns -1 on error
45 ALOGE("%s: Failed to format string", __FUNCTION__);
46 return;
47 }
Igor Murashkine65b7ea2013-10-30 18:09:52 -070048#else
49 return;
50#endif
51
Igor Murashkinec79ef22013-10-24 17:09:15 -070052 va_end(arglist);
53
54 printLine(formattedString);
55 free(formattedString);
56}
57
58/*
59 * Implementation of LogPrinter
60 */
61LogPrinter::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
71void 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
86void LogPrinter::printRaw(const char* string) {
87 __android_log_print(mPriority, mLogTag, "%s%s", mPrefix, string);
88}
89
90
91/*
92 * Implementation of FdPrinter
93 */
94FdPrinter::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
105void 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 Hughes34a4f0b2016-10-05 09:37:18 -0700114#ifndef _WIN32
Elliott Hughesdcc98da2014-05-22 01:23:29 -0700115 dprintf(mFd, mFormatString, mPrefix, string);
Igor Murashkine65b7ea2013-10-30 18:09:52 -0700116#endif
Igor Murashkinec79ef22013-10-24 17:09:15 -0700117}
118
119/*
120 * Implementation of String8Printer
121 */
122String8Printer::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
131void 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 Ferris038ac692013-10-31 16:25:04 -0700140 mTarget->append(mPrefix);
Igor Murashkinec79ef22013-10-24 17:09:15 -0700141 mTarget->append(string);
142 mTarget->append("\n");
143}
144
145/*
146 * Implementation of PrefixPrinter
147 */
148PrefixPrinter::PrefixPrinter(Printer& printer, const char* prefix) :
149 mPrinter(printer), mPrefix(prefix ?: "") {
150}
151
152void PrefixPrinter::printLine(const char* string) {
153 mPrinter.printFormatLine("%s%s", mPrefix, string);
154}
155
156}; //namespace android