Printer.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #define LOG_TAG "Printer"
  17. // #define LOG_NDEBUG 0
  18. #include <utils/Printer.h>
  19. #include <utils/String8.h>
  20. #include <utils/Log.h>
  21. #include <stdlib.h>
  22. namespace android {
  23. /*
  24. * Implementation of Printer
  25. */
  26. Printer::Printer() {
  27. // Intentionally left empty
  28. }
  29. Printer::~Printer() {
  30. // Intentionally left empty
  31. }
  32. void Printer::printFormatLine(const char* format, ...) {
  33. va_list arglist;
  34. va_start(arglist, format);
  35. char* formattedString;
  36. #ifndef _WIN32
  37. if (vasprintf(&formattedString, format, arglist) < 0) { // returns -1 on error
  38. ALOGE("%s: Failed to format string", __FUNCTION__);
  39. va_end(arglist);
  40. return;
  41. }
  42. #else
  43. va_end(arglist);
  44. return;
  45. #endif
  46. va_end(arglist);
  47. printLine(formattedString);
  48. free(formattedString);
  49. }
  50. /*
  51. * Implementation of LogPrinter
  52. */
  53. LogPrinter::LogPrinter(const char* logtag,
  54. android_LogPriority priority,
  55. const char* prefix,
  56. bool ignoreBlankLines) :
  57. mLogTag(logtag),
  58. mPriority(priority),
  59. mPrefix(prefix ?: ""),
  60. mIgnoreBlankLines(ignoreBlankLines) {
  61. }
  62. void LogPrinter::printLine(const char* string) {
  63. if (string == nullptr) {
  64. ALOGW("%s: NULL string passed in", __FUNCTION__);
  65. return;
  66. }
  67. if (mIgnoreBlankLines || (*string)) {
  68. // Simple case: Line is not blank, or we don't care about printing blank lines
  69. printRaw(string);
  70. } else {
  71. // Force logcat to print empty lines by adding prefixing with a space
  72. printRaw(" ");
  73. }
  74. }
  75. void LogPrinter::printRaw(const char* string) {
  76. __android_log_print(mPriority, mLogTag, "%s%s", mPrefix, string);
  77. }
  78. /*
  79. * Implementation of FdPrinter
  80. */
  81. FdPrinter::FdPrinter(int fd, unsigned int indent, const char* prefix) :
  82. mFd(fd), mIndent(indent), mPrefix(prefix ?: "") {
  83. if (fd < 0) {
  84. ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, fd);
  85. }
  86. // <indent><prefix><line> -- e.g. '%-4s%s\n' for indent=4
  87. snprintf(mFormatString, sizeof(mFormatString), "%%-%us%%s\n", mIndent);
  88. }
  89. void FdPrinter::printLine(const char* string) {
  90. if (string == nullptr) {
  91. ALOGW("%s: NULL string passed in", __FUNCTION__);
  92. return;
  93. } else if (mFd < 0) {
  94. ALOGW("%s: File descriptor out of range (%d)", __FUNCTION__, mFd);
  95. return;
  96. }
  97. #ifndef _WIN32
  98. dprintf(mFd, mFormatString, mPrefix, string);
  99. #endif
  100. }
  101. /*
  102. * Implementation of String8Printer
  103. */
  104. String8Printer::String8Printer(String8* target, const char* prefix) :
  105. mTarget(target),
  106. mPrefix(prefix ?: "") {
  107. if (target == nullptr) {
  108. ALOGW("%s: Target string was NULL", __FUNCTION__);
  109. }
  110. }
  111. void String8Printer::printLine(const char* string) {
  112. if (string == nullptr) {
  113. ALOGW("%s: NULL string passed in", __FUNCTION__);
  114. return;
  115. } else if (mTarget == nullptr) {
  116. ALOGW("%s: Target string was NULL", __FUNCTION__);
  117. return;
  118. }
  119. mTarget->append(mPrefix);
  120. mTarget->append(string);
  121. mTarget->append("\n");
  122. }
  123. /*
  124. * Implementation of PrefixPrinter
  125. */
  126. PrefixPrinter::PrefixPrinter(Printer& printer, const char* prefix) :
  127. mPrinter(printer), mPrefix(prefix ?: "") {
  128. }
  129. void PrefixPrinter::printLine(const char* string) {
  130. mPrinter.printFormatLine("%s%s", mPrefix, string);
  131. }
  132. }; //namespace android