logprint.h revision 9b4d7e1c381289b6b6fba1794b5846e0d34da1cc
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 _LOGPRINT_H
18#define _LOGPRINT_H
19
20#include <pthread.h>
21
22#include <android/log.h>
23#include <log/event_tag_map.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29typedef enum {
30    /* Verbs */
31    FORMAT_OFF = 0,
32    FORMAT_BRIEF,
33    FORMAT_PROCESS,
34    FORMAT_TAG,
35    FORMAT_THREAD,
36    FORMAT_RAW,
37    FORMAT_TIME,
38    FORMAT_THREADTIME,
39    FORMAT_LONG,
40    /* Adverbs. The following are modifiers to above format verbs */
41    FORMAT_MODIFIER_COLOR,     /* converts priority to color */
42    FORMAT_MODIFIER_TIME_USEC, /* switches from msec to usec time precision */
43    FORMAT_MODIFIER_PRINTABLE, /* converts non-printable to printable escapes */
44    FORMAT_MODIFIER_YEAR,      /* Adds year to date */
45    FORMAT_MODIFIER_ZONE,      /* Adds zone to date, + UTC */
46    FORMAT_MODIFIER_EPOCH,     /* Print time as seconds since Jan 1 1970 */
47    FORMAT_MODIFIER_MONOTONIC, /* Print cpu time as seconds since start */
48    FORMAT_MODIFIER_UID,       /* Adds uid */
49    FORMAT_MODIFIER_DESCRIPT,  /* Adds descriptive */
50    /* private, undocumented */
51    FORMAT_MODIFIER_TIME_NSEC, /* switches from msec to nsec time precision */
52} AndroidLogPrintFormat;
53
54typedef struct AndroidLogFormat_t AndroidLogFormat;
55
56typedef struct AndroidLogEntry_t {
57    time_t tv_sec;
58    long tv_nsec;
59    android_LogPriority priority;
60    int32_t uid;
61    int32_t pid;
62    int32_t tid;
63    const char* tag;
64    size_t tagLen;
65    size_t messageLen;
66    const char* message;
67} AndroidLogEntry;
68
69AndroidLogFormat* android_log_format_new();
70
71void android_log_format_free(AndroidLogFormat* p_format);
72
73/* currently returns 0 if format is a modifier, 1 if not */
74int android_log_setPrintFormat(AndroidLogFormat* p_format,
75        AndroidLogPrintFormat format);
76
77/**
78 * Returns FORMAT_OFF on invalid string
79 */
80AndroidLogPrintFormat android_log_formatFromString(const char* s);
81
82/**
83 * filterExpression: a single filter expression
84 * eg "AT:d"
85 *
86 * returns 0 on success and -1 on invalid expression
87 *
88 * Assumes single threaded execution
89 *
90 */
91
92int android_log_addFilterRule(AndroidLogFormat* p_format,
93        const char* filterExpression);
94
95/**
96 * filterString: a whitespace-separated set of filter expressions
97 * eg "AT:d *:i"
98 *
99 * returns 0 on success and -1 on invalid expression
100 *
101 * Assumes single threaded execution
102 *
103 */
104
105int android_log_addFilterString(AndroidLogFormat* p_format,
106        const char* filterString);
107
108/**
109 * returns 1 if this log line should be printed based on its priority
110 * and tag, and 0 if it should not
111 */
112int android_log_shouldPrintLine (
113        AndroidLogFormat* p_format, const char* tag, android_LogPriority pri);
114
115/**
116 * Splits a wire-format buffer into an AndroidLogEntry
117 * entry allocated by caller. Pointers will point directly into buf
118 *
119 * Returns 0 on success and -1 on invalid wire format (entry will be
120 * in unspecified state)
121 */
122int android_log_processLogBuffer(struct logger_entry* buf,
123                                 AndroidLogEntry* entry);
124
125/**
126 * Like android_log_processLogBuffer, but for binary logs.
127 *
128 * If "map" is non-NULL, it will be used to convert the log tag number
129 * into a string.
130 */
131int android_log_processBinaryLogBuffer(struct logger_entry* buf,
132    AndroidLogEntry* entry, const EventTagMap* map, char* messageBuf,
133    int messageBufLen);
134
135/**
136 * Formats a log message into a buffer
137 *
138 * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
139 * If return value != defaultBuffer, caller must call free()
140 * Returns NULL on malloc error
141 */
142
143char* android_log_formatLogLine (
144    AndroidLogFormat* p_format,
145    char* defaultBuffer,
146    size_t defaultBufferSize,
147    const AndroidLogEntry* p_line,
148    size_t* p_outLength);
149
150/**
151 * Either print or do not print log line, based on filter
152 *
153 * Assumes single threaded execution
154 *
155 */
156int android_log_printLogLine(
157    AndroidLogFormat* p_format,
158    int fd,
159    const AndroidLogEntry* entry);
160
161#ifdef __cplusplus
162}
163#endif
164
165#endif /*_LOGPRINT_H*/
166