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