fplutil
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
print.h
Go to the documentation of this file.
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FPLUTIL_PRINT_H
16 #define FPLUTIL_PRINT_H
17 
18 /// @file
19 /// Header for libfplutil_print.
20 ///
21 /// libfplutil_print makes it easy to redirect writes to the standard output
22 /// stream to the Android log.
23 
24 #if defined(ANDROID) || defined(__ANDROID__)
25 
26 #include <android/log.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 
30 #if defined(__cplusplus)
31 extern "C" {
32 #endif // defined(__cplusplus)
33 
34 /// This is the function signature to use if you would like to intercept
35 /// printf calls in your code. See SetAndroidLogOutputFunction().
36 ///
37 /// The first two parameters to this function are the log priority and tag,
38 /// respectively. The remaining ones are the same as for the stdio function
39 /// vprintf(), and overall the expected semantics for this function are the
40 /// same as for vprintf(). Please see:
41 /// http://pubs.opengroup.org/onlinepubs/9699919799/functions/vfprintf.html
42 ///
43 /// @param priority Android log priority.
44 /// @param tag Tag to display before the logged string.
45 /// @param format printf format string.
46 /// @param list Additional list of arguments referenced by the printf format
47 /// string.
48 typedef int (*AndroidLogOutputFunction)(int priority, const char *tag,
49  const char *format, va_list list);
50 
51 /// Set the tag used for log output by the wrappers. This can used when
52 /// filtering the output of "adb logcat" to distinguish the log messages from
53 /// this application vs. other applications and the rest of the system.
54 /// Note that this pointer is simply assigned so it must have permanent
55 /// lifetime.
56 ///
57 /// @param tag A null terminated C string to use as the log tag.
58 /// Default is "main".
59 ///
60 /// @return 0 on success, -1 if tag is null or an empty string.
61 int SetAndroidLogWrapperTag(const char *tag);
62 
63 /// Set the priority used for log output by the wrappers.
64 ///
65 /// @param priority An android log priority, as described in the Android NDK
66 /// header file android/log.h. Default is ANDROID_LOG_INFO
67 /// from that file.
68 void SetAndroidLogWrapperPriority(int priority);
69 
70 /// Set the buffer size for the wrappers. Default is 256 bytes. Setting this to
71 /// zero will force unbuffered output, which may have unexpected formatting
72 /// such as additional newlines as text is immediately sent to the log. Nonzero
73 /// values will accumulate writes until a newline is encountered or the buffer
74 /// size is reached.
75 ///
76 /// Buffering is done to allow multiple stdio calls to output on the same line,
77 /// as per normal behavior of stdio. So, something like this:
78 ///
79 /// @code{.c}
80 /// for (i = 0; i < 5; ++i) {
81 /// printf("%c", '1' + i);
82 /// }
83 /// @endcode
84 ///
85 /// would output:
86 ///
87 /// @code{.c}
88 /// 12345
89 /// @endcode
90 ///
91 /// and not five separate log lines, which is the unbuffered behavior.
92 ///
93 /// @param size The number of bytes to use for buffering. 0 sets unbuffered
94 /// mode. Default is 256 bytes.
95 ///
96 /// @return 0 on success, -1 if buffer allocation failed (nonfatal, buffer is
97 /// unchanged)
98 int SetAndroidLogWrapperBufferSize(size_t size);
99 
100 /// Set the function called when these wrappers perform output. This defaults
101 /// to __android_log_vprint(), which will cause the output to go to the Android
102 /// log. You may intercept the output yourself by setting this function. Do not
103 /// call any stdio output functions or use C++ std::cout/cerr from the function
104 /// you set here, or infinite recursion will result.
105 ///
106 /// @param func A function pointer of typedef AndroidLogOutputFunction to use
107 /// for stdio output.
109 
110 /// An internal function that will behave like a snprintf-based version of
111 /// perror. Used by __wrap_perror() and factored out/exposed to be testable.
112 ///
113 /// Output should look equivalent to what you would expect for:
114 ///
115 /// @code{.c}
116 /// sprintf(msgout, "%s: %s", msg, strerror(err));
117 /// @endcode
118 ///
119 /// or
120 ///
121 /// @code{.c}
122 /// strcpy(msgout, strerror(err));
123 /// @endcode
124 ///
125 /// depending on the value of msg.
126 ///
127 /// @param msg An optional message to prepend to the error to be printed.
128 /// May be NULL.
129 /// @param err The error value to be printed (via strerror). Error value
130 /// behavior is the same as for strerror.
131 /// @param msgout The output buffer. Must not be NULL.
132 /// @param outsize The size of the memory pointed to by the output buffer.
133 /// msgout will be null-terminated to this length.
134 ///
135 /// @return 0 on success, -1 on error.
136 int AndroidPerrorMsg(const char *msg, int err, char *msgout, size_t outsize);
137 
138 #if defined(__cplusplus)
139 } // extern "C"
140 #endif // defined(__cplusplus)
141 
142 #endif // defined(ANDROID) || defined(__ANDROID__)
143 #endif // FPLUTIL_PRINT_H