123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #define LOG_TAG "Printer"
- #include <utils/Printer.h>
- #include <utils/String8.h>
- #include <utils/Log.h>
- #include <stdlib.h>
- namespace android {
- Printer::Printer() {
-
- }
- Printer::~Printer() {
-
- }
- void Printer::printFormatLine(const char* format, ...) {
- va_list arglist;
- va_start(arglist, format);
- char* formattedString;
- #ifndef _WIN32
- if (vasprintf(&formattedString, format, arglist) < 0) {
- ALOGE("%s: Failed to format string", __FUNCTION__);
- va_end(arglist);
- return;
- }
- #else
- va_end(arglist);
- return;
- #endif
- va_end(arglist);
- printLine(formattedString);
- free(formattedString);
- }
- LogPrinter::LogPrinter(const char* logtag,
- android_LogPriority priority,
- const char* prefix,
- bool ignoreBlankLines) :
- mLogTag(logtag),
- mPriority(priority),
- mPrefix(prefix ?: ""),
- mIgnoreBlankLines(ignoreBlankLines) {
- }
- void LogPrinter::printLine(const char* string) {
- if (string == nullptr) {
- ALOGW("%s: NULL string passed in", __FUNCTION__);
- return;
- }
- if (mIgnoreBlankLines || (*string)) {
-
- printRaw(string);
- } else {
-
- printRaw(" ");
- }
- }
- void LogPrinter::printRaw(const char* string) {
- __android_log_print(mPriority, mLogTag, "%s%s", mPrefix, string);
- }
- FdPrinter::FdPrinter(int fd, unsigned int indent, const char* prefix) :
- mFd(fd), mIndent(indent), mPrefix(prefix ?: "") {
- if (fd < 0) {
- ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, fd);
- }
-
- snprintf(mFormatString, sizeof(mFormatString), "%%-%us%%s\n", mIndent);
- }
- void FdPrinter::printLine(const char* string) {
- if (string == nullptr) {
- ALOGW("%s: NULL string passed in", __FUNCTION__);
- return;
- } else if (mFd < 0) {
- ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, mFd);
- return;
- }
- #ifndef _WIN32
- dprintf(mFd, mFormatString, mPrefix, string);
- #endif
- }
- String8Printer::String8Printer(String8* target, const char* prefix) :
- mTarget(target),
- mPrefix(prefix ?: "") {
- if (target == nullptr) {
- ALOGW("%s: Target string was NULL", __FUNCTION__);
- }
- }
- void String8Printer::printLine(const char* string) {
- if (string == nullptr) {
- ALOGW("%s: NULL string passed in", __FUNCTION__);
- return;
- } else if (mTarget == nullptr) {
- ALOGW("%s: Target string was NULL", __FUNCTION__);
- return;
- }
- mTarget->append(mPrefix);
- mTarget->append(string);
- mTarget->append("\n");
- }
- PrefixPrinter::PrefixPrinter(Printer& printer, const char* prefix) :
- mPrinter(printer), mPrefix(prefix ?: "") {
- }
- void PrefixPrinter::printLine(const char* string) {
- mPrinter.printFormatLine("%s%s", mPrefix, string);
- }
- };
|