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 
17 #ifndef ANDROID_PRINTER_H
18 #define ANDROID_PRINTER_H
19 
20 #include <android/log.h>
21 
22 namespace android {
23 
24 // Interface for printing to an arbitrary data stream
25 class Printer {
26 public:
27     // Print a new line specified by 'string'. \n is appended automatically.
28     // -- Assumes that the string has no new line in it.
29     virtual void printLine(const char* string = "") = 0;
30 
31     // Print a new line specified by the format string. \n is appended automatically.
32     // -- Assumes that the resulting string has no new line in it.
33     virtual void printFormatLine(const char* format, ...) __attribute__((format (printf, 2, 3)));
34 
35 protected:
36     Printer();
37     virtual ~Printer();
38 }; // class Printer
39 
40 // Print to logcat
41 class LogPrinter : public Printer {
42 public:
43     // Create a printer using the specified logcat and log priority
44     // - Unless ignoreBlankLines is false, print blank lines to logcat
45     // (Note that the default ALOG behavior is to ignore blank lines)
46     LogPrinter(const char* logtag,
47                android_LogPriority priority = ANDROID_LOG_DEBUG,
48                const char* prefix = nullptr,
49                bool ignoreBlankLines = false);
50 
51     // Print the specified line to logcat. No \n at the end is necessary.
52     virtual void printLine(const char* string);
53 
54 private:
55     void printRaw(const char* string);
56 
57     const char* mLogTag;
58     android_LogPriority mPriority;
59     const char* mPrefix;
60     bool mIgnoreBlankLines;
61 }; // class LogPrinter
62 
63 // Print to a file descriptor
64 class FdPrinter : public Printer {
65 public:
66     // Create a printer using the specified file descriptor.
67     // - Each line will be prefixed with 'indent' number of blank spaces.
68     // - In addition, each line will be prefixed with the 'prefix' string.
69     FdPrinter(int fd, unsigned int indent = 0, const char* prefix = nullptr);
70 
71     // Print the specified line to the file descriptor. \n is appended automatically.
72     virtual void printLine(const char* string);
73 
74 private:
75     enum {
76         MAX_FORMAT_STRING = 20,
77     };
78 
79     int mFd;
80     unsigned int mIndent;
81     const char* mPrefix;
82     char mFormatString[MAX_FORMAT_STRING];
83 }; // class FdPrinter
84 
85 class String8;
86 
87 // Print to a String8
88 class String8Printer : public Printer {
89 public:
90     // Create a printer using the specified String8 as the target.
91     // - In addition, each line will be prefixed with the 'prefix' string.
92     // - target's memory lifetime must be a superset of this String8Printer.
93     String8Printer(String8* target, const char* prefix = nullptr);
94 
95     // Append the specified line to the String8. \n is appended automatically.
96     virtual void printLine(const char* string);
97 
98 private:
99     String8* mTarget;
100     const char* mPrefix;
101 }; // class String8Printer
102 
103 // Print to an existing Printer by adding a prefix to each line
104 class PrefixPrinter : public Printer {
105 public:
106     // Create a printer using the specified printer as the target.
107     PrefixPrinter(Printer& printer, const char* prefix);
108 
109     // Print the line (prefixed with prefix) using the printer.
110     virtual void printLine(const char* string);
111 
112 private:
113     Printer& mPrinter;
114     const char* mPrefix;
115 };
116 
117 }  // namespace android
118 
119 #endif // ANDROID_PRINTER_H
120