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