summaryrefslogtreecommitdiffstats
path: root/include/utils/Printer.h
blob: bb6628767ca89384c1ec5b8f64e390260a47c4a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef ANDROID_PRINTER_H
#define ANDROID_PRINTER_H

#include <android/log.h>

namespace android {

// Interface for printing to an arbitrary data stream
class Printer {
public:
    // Print a new line specified by 'string'. \n is appended automatically.
    // -- Assumes that the string has no new line in it.
    virtual void printLine(const char* string = "") = 0;

    // Print a new line specified by the format string. \n is appended automatically.
    // -- Assumes that the resulting string has no new line in it.
    virtual void printFormatLine(const char* format, ...) __attribute__((format (printf, 2, 3)));

protected:
    Printer();
    virtual ~Printer();
}; // class Printer

// Print to logcat
class LogPrinter : public Printer {
public:
    // Create a printer using the specified logcat and log priority
    // - Unless ignoreBlankLines is false, print blank lines to logcat
    // (Note that the default ALOG behavior is to ignore blank lines)
    LogPrinter(const char* logtag,
               android_LogPriority priority = ANDROID_LOG_DEBUG,
               const char* prefix = 0,
               bool ignoreBlankLines = false);

    // Print the specified line to logcat. No \n at the end is necessary.
    virtual void printLine(const char* string);

private:
    void printRaw(const char* string);

    const char* mLogTag;
    android_LogPriority mPriority;
    const char* mPrefix;
    bool mIgnoreBlankLines;
}; // class LogPrinter

// Print to a file descriptor
class FdPrinter : public Printer {
public:
    // Create a printer using the specified file descriptor.
    // - Each line will be prefixed with 'indent' number of blank spaces.
    // - In addition, each line will be prefixed with the 'prefix' string.
    FdPrinter(int fd, unsigned int indent = 0, const char* prefix = 0);

    // Print the specified line to the file descriptor. \n is appended automatically.
    virtual void printLine(const char* string);

private:
    enum {
        MAX_FORMAT_STRING = 20,
    };

    int mFd;
    unsigned int mIndent;
    const char* mPrefix;
    char mFormatString[MAX_FORMAT_STRING];
}; // class FdPrinter

class String8;

// Print to a String8
class String8Printer : public Printer {
public:
    // Create a printer using the specified String8 as the target.
    // - In addition, each line will be prefixed with the 'prefix' string.
    // - target's memory lifetime must be a superset of this String8Printer.
    String8Printer(String8* target, const char* prefix = 0);

    // Append the specified line to the String8. \n is appended automatically.
    virtual void printLine(const char* string);

private:
    String8* mTarget;
    const char* mPrefix;
}; // class String8Printer

// Print to an existing Printer by adding a prefix to each line
class PrefixPrinter : public Printer {
public:
    // Create a printer using the specified printer as the target.
    PrefixPrinter(Printer& printer, const char* prefix);

    // Print the line (prefixed with prefix) using the printer.
    virtual void printLine(const char* string);

private:
    Printer& mPrinter;
    const char* mPrefix;
};

}; // namespace android

#endif // ANDROID_PRINTER_H