TextOutput.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2006 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. #ifndef ANDROID_TEXTOUTPUT_H
  17. #define ANDROID_TEXTOUTPUT_H
  18. #include <utils/Errors.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. // ---------------------------------------------------------------------------
  22. namespace android {
  23. class String8;
  24. class String16;
  25. class TextOutput
  26. {
  27. public:
  28. TextOutput();
  29. virtual ~TextOutput();
  30. virtual status_t print(const char* txt, size_t len) = 0;
  31. virtual void moveIndent(int delta) = 0;
  32. class Bundle {
  33. public:
  34. inline Bundle(TextOutput& to) : mTO(to) { to.pushBundle(); }
  35. inline ~Bundle() { mTO.popBundle(); }
  36. private:
  37. TextOutput& mTO;
  38. };
  39. virtual void pushBundle() = 0;
  40. virtual void popBundle() = 0;
  41. };
  42. // ---------------------------------------------------------------------------
  43. // Text output stream for printing to the log (via utils/Log.h).
  44. extern TextOutput& alog;
  45. // Text output stream for printing to stdout.
  46. extern TextOutput& aout;
  47. // Text output stream for printing to stderr.
  48. extern TextOutput& aerr;
  49. typedef TextOutput& (*TextOutputManipFunc)(TextOutput&);
  50. TextOutput& endl(TextOutput& to);
  51. TextOutput& indent(TextOutput& to);
  52. TextOutput& dedent(TextOutput& to);
  53. TextOutput& operator<<(TextOutput& to, const char* str);
  54. TextOutput& operator<<(TextOutput& to, char); // writes raw character
  55. TextOutput& operator<<(TextOutput& to, bool);
  56. TextOutput& operator<<(TextOutput& to, int);
  57. TextOutput& operator<<(TextOutput& to, long);
  58. TextOutput& operator<<(TextOutput& to, unsigned int);
  59. TextOutput& operator<<(TextOutput& to, unsigned long);
  60. TextOutput& operator<<(TextOutput& to, long long);
  61. TextOutput& operator<<(TextOutput& to, unsigned long long);
  62. TextOutput& operator<<(TextOutput& to, float);
  63. TextOutput& operator<<(TextOutput& to, double);
  64. TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func);
  65. TextOutput& operator<<(TextOutput& to, const void*);
  66. TextOutput& operator<<(TextOutput& to, const String8& val);
  67. TextOutput& operator<<(TextOutput& to, const String16& val);
  68. class TypeCode
  69. {
  70. public:
  71. inline TypeCode(uint32_t code);
  72. inline ~TypeCode();
  73. inline uint32_t typeCode() const;
  74. private:
  75. uint32_t mCode;
  76. };
  77. TextOutput& operator<<(TextOutput& to, const TypeCode& val);
  78. class HexDump
  79. {
  80. public:
  81. HexDump(const void *buf, size_t size, size_t bytesPerLine=16);
  82. inline ~HexDump();
  83. inline HexDump& setBytesPerLine(size_t bytesPerLine);
  84. inline HexDump& setSingleLineCutoff(int32_t bytes);
  85. inline HexDump& setAlignment(size_t alignment);
  86. inline HexDump& setCArrayStyle(bool enabled);
  87. inline const void* buffer() const;
  88. inline size_t size() const;
  89. inline size_t bytesPerLine() const;
  90. inline int32_t singleLineCutoff() const;
  91. inline size_t alignment() const;
  92. inline bool carrayStyle() const;
  93. private:
  94. const void* mBuffer;
  95. size_t mSize;
  96. size_t mBytesPerLine;
  97. int32_t mSingleLineCutoff;
  98. size_t mAlignment;
  99. bool mCArrayStyle;
  100. };
  101. TextOutput& operator<<(TextOutput& to, const HexDump& val);
  102. // ---------------------------------------------------------------------------
  103. // No user servicable parts below.
  104. inline TextOutput& endl(TextOutput& to)
  105. {
  106. to.print("\n", 1);
  107. return to;
  108. }
  109. inline TextOutput& indent(TextOutput& to)
  110. {
  111. to.moveIndent(1);
  112. return to;
  113. }
  114. inline TextOutput& dedent(TextOutput& to)
  115. {
  116. to.moveIndent(-1);
  117. return to;
  118. }
  119. inline TextOutput& operator<<(TextOutput& to, const char* str)
  120. {
  121. to.print(str, strlen(str));
  122. return to;
  123. }
  124. inline TextOutput& operator<<(TextOutput& to, char c)
  125. {
  126. to.print(&c, 1);
  127. return to;
  128. }
  129. inline TextOutput& operator<<(TextOutput& to, TextOutputManipFunc func)
  130. {
  131. return (*func)(to);
  132. }
  133. inline TypeCode::TypeCode(uint32_t code) : mCode(code) { }
  134. inline TypeCode::~TypeCode() { }
  135. inline uint32_t TypeCode::typeCode() const { return mCode; }
  136. inline HexDump::~HexDump() { }
  137. inline HexDump& HexDump::setBytesPerLine(size_t bytesPerLine) {
  138. mBytesPerLine = bytesPerLine; return *this;
  139. }
  140. inline HexDump& HexDump::setSingleLineCutoff(int32_t bytes) {
  141. mSingleLineCutoff = bytes; return *this;
  142. }
  143. inline HexDump& HexDump::setAlignment(size_t alignment) {
  144. mAlignment = alignment; return *this;
  145. }
  146. inline HexDump& HexDump::setCArrayStyle(bool enabled) {
  147. mCArrayStyle = enabled; return *this;
  148. }
  149. inline const void* HexDump::buffer() const { return mBuffer; }
  150. inline size_t HexDump::size() const { return mSize; }
  151. inline size_t HexDump::bytesPerLine() const { return mBytesPerLine; }
  152. inline int32_t HexDump::singleLineCutoff() const { return mSingleLineCutoff; }
  153. inline size_t HexDump::alignment() const { return mAlignment; }
  154. inline bool HexDump::carrayStyle() const { return mCArrayStyle; }
  155. // ---------------------------------------------------------------------------
  156. }; // namespace android
  157. #endif // ANDROID_TEXTOUTPUT_H