summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Murashkin2013-10-24 19:09:15 -0500
committerIgor Murashkin2013-10-25 21:24:56 -0500
commitec79ef2e7b6b1d81266637ca0e002b5c0c5a789b (patch)
tree309ff8a328db5ccd522995abe3614959cfded1c6 /libutils/Printer.cpp
parenta9e453f1b552699f69dca19599c7624a581089bd (diff)
downloadplatform-system-core-ec79ef2e7b6b1d81266637ca0e002b5c0c5a789b.tar.gz
platform-system-core-ec79ef2e7b6b1d81266637ca0e002b5c0c5a789b.tar.xz
platform-system-core-ec79ef2e7b6b1d81266637ca0e002b5c0c5a789b.zip
utils: Add ProcessCallStack to collect stack traces for all threads in a process
- Also add a Printer class (print lines to logcat, fd, or strings) Bug: 11324229 Change-Id: I78435ed49aa196a0efb45bf9b2d58b62c41737d3
Diffstat (limited to 'libutils/Printer.cpp')
-rw-r--r--libutils/Printer.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/libutils/Printer.cpp b/libutils/Printer.cpp
new file mode 100644
index 000000000..b062ef0a7
--- /dev/null
+++ b/libutils/Printer.cpp
@@ -0,0 +1,155 @@
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 if (vasprintf(&formattedString, format, arglist) < 0) { // returns -1 on error
51 ALOGE("%s: Failed to format string", __FUNCTION__);
52 return;
53 }
54 va_end(arglist);
55
56 printLine(formattedString);
57 free(formattedString);
58}
59
60/*
61 * Implementation of LogPrinter
62 */
63LogPrinter::LogPrinter(const char* logtag,
64 android_LogPriority priority,
65 const char* prefix,
66 bool ignoreBlankLines) :
67 mLogTag(logtag),
68 mPriority(priority),
69 mPrefix(prefix ?: ""),
70 mIgnoreBlankLines(ignoreBlankLines) {
71}
72
73void LogPrinter::printLine(const char* string) {
74 if (string == NULL) {
75 ALOGW("%s: NULL string passed in", __FUNCTION__);
76 return;
77 }
78
79 if (mIgnoreBlankLines || (*string)) {
80 // Simple case: Line is not blank, or we don't care about printing blank lines
81 printRaw(string);
82 } else {
83 // Force logcat to print empty lines by adding prefixing with a space
84 printRaw(" ");
85 }
86}
87
88void LogPrinter::printRaw(const char* string) {
89 __android_log_print(mPriority, mLogTag, "%s%s", mPrefix, string);
90}
91
92
93/*
94 * Implementation of FdPrinter
95 */
96FdPrinter::FdPrinter(int fd, unsigned int indent, const char* prefix) :
97 mFd(fd), mIndent(indent), mPrefix(prefix ?: "") {
98
99 if (fd < 0) {
100 ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, fd);
101 }
102
103 // <indent><prefix><line> -- e.g. '%-4s%s\n' for indent=4
104 snprintf(mFormatString, sizeof(mFormatString), "%%-%us%%s\n", mIndent);
105}
106
107void FdPrinter::printLine(const char* string) {
108 if (string == NULL) {
109 ALOGW("%s: NULL string passed in", __FUNCTION__);
110 return;
111 } else if (mFd < 0) {
112 ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, mFd);
113 return;
114 }
115
116 fdprintf(mFd, mFormatString, mPrefix, string);
117}
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
140 mTarget->append(string);
141 mTarget->append("\n");
142}
143
144/*
145 * Implementation of PrefixPrinter
146 */
147PrefixPrinter::PrefixPrinter(Printer& printer, const char* prefix) :
148 mPrinter(printer), mPrefix(prefix ?: "") {
149}
150
151void PrefixPrinter::printLine(const char* string) {
152 mPrinter.printFormatLine("%s%s", mPrefix, string);
153}
154
155}; //namespace android