fplutil
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Pages
print.h File Reference
#include <android/log.h>
#include <stdarg.h>
#include <stdio.h>

Go to the source code of this file.

Typedefs

typedef int(* AndroidLogOutputFunction )(int priority, const char *tag, const char *format, va_list list)
 

Functions

int SetAndroidLogWrapperTag (const char *tag)
 
void SetAndroidLogWrapperPriority (int priority)
 
int SetAndroidLogWrapperBufferSize (size_t size)
 
void SetAndroidStdioOutputFunction (AndroidLogOutputFunction func)
 
int AndroidPerrorMsg (const char *msg, int err, char *msgout, size_t outsize)
 

Detailed Description

Header for libfplutil_print.

libfplutil_print makes it easy to redirect writes to the standard output stream to the Android log.

Typedef Documentation

typedef int(* AndroidLogOutputFunction)(int priority, const char *tag, const char *format, va_list list)

This is the function signature to use if you would like to intercept printf calls in your code. See SetAndroidLogOutputFunction().

The first two parameters to this function are the log priority and tag, respectively. The remaining ones are the same as for the stdio function vprintf(), and overall the expected semantics for this function are the same as for vprintf(). Please see: http://pubs.opengroup.org/onlinepubs/9699919799/functions/vfprintf.html

Parameters
priorityAndroid log priority.
tagTag to display before the logged string.
formatprintf format string.
listAdditional list of arguments referenced by the printf format string.

Function Documentation

int AndroidPerrorMsg ( const char *  msg,
int  err,
char *  msgout,
size_t  outsize 
)

An internal function that will behave like a snprintf-based version of perror. Used by __wrap_perror() and factored out/exposed to be testable.

Output should look equivalent to what you would expect for:

sprintf(msgout, "%s: %s", msg, strerror(err));

or

strcpy(msgout, strerror(err));

depending on the value of msg.

Parameters
msgAn optional message to prepend to the error to be printed. May be NULL.
errThe error value to be printed (via strerror). Error value behavior is the same as for strerror.
msgoutThe output buffer. Must not be NULL.
outsizeThe size of the memory pointed to by the output buffer. msgout will be null-terminated to this length.
Returns
0 on success, -1 on error.
int SetAndroidLogWrapperBufferSize ( size_t  size)

Set the buffer size for the wrappers. Default is 256 bytes. Setting this to zero will force unbuffered output, which may have unexpected formatting such as additional newlines as text is immediately sent to the log. Nonzero values will accumulate writes until a newline is encountered or the buffer size is reached.

Buffering is done to allow multiple stdio calls to output on the same line, as per normal behavior of stdio. So, something like this:

for (i = 0; i < 5; ++i) {
printf("%c", '1' + i);
}

would output:

12345

and not five separate log lines, which is the unbuffered behavior.

Parameters
sizeThe number of bytes to use for buffering. 0 sets unbuffered mode. Default is 256 bytes.
Returns
0 on success, -1 if buffer allocation failed (nonfatal, buffer is unchanged)
void SetAndroidLogWrapperPriority ( int  priority)

Set the priority used for log output by the wrappers.

Parameters
priorityAn android log priority, as described in the Android NDK header file android/log.h. Default is ANDROID_LOG_INFO from that file.
int SetAndroidLogWrapperTag ( const char *  tag)

Set the tag used for log output by the wrappers. This can used when filtering the output of "adb logcat" to distinguish the log messages from this application vs. other applications and the rest of the system. Note that this pointer is simply assigned so it must have permanent lifetime.

Parameters
tagA null terminated C string to use as the log tag. Default is "main".
Returns
0 on success, -1 if tag is null or an empty string.
void SetAndroidStdioOutputFunction ( AndroidLogOutputFunction  func)

Set the function called when these wrappers perform output. This defaults to __android_log_vprint(), which will cause the output to go to the Android log. You may intercept the output yourself by setting this function. Do not call any stdio output functions or use C++ std::cout/cerr from the function you set here, or infinite recursion will result.

Parameters
funcA function pointer of typedef AndroidLogOutputFunction to use for stdio output.