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 #pragma once
18 
19 #include <stdint.h>
20 #include <sys/types.h>
21 
22 #include <android/log.h>
23 #include <log/event_tag_map.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 typedef 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 
54 typedef struct AndroidLogFormat_t AndroidLogFormat;
55 
56 typedef 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 
69 AndroidLogFormat* android_log_format_new();
70 
71 void android_log_format_free(AndroidLogFormat* p_format);
72 
73 /* currently returns 0 if format is a modifier, 1 if not */
74 int android_log_setPrintFormat(AndroidLogFormat* p_format,
75                                AndroidLogPrintFormat format);
76 
77 /**
78  * Returns FORMAT_OFF on invalid string
79  */
80 AndroidLogPrintFormat 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 
92 int 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 
105 int 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  */
112 int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
113                                 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  */
122 int 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  */
131 int android_log_processBinaryLogBuffer(struct logger_entry* buf,
132                                        AndroidLogEntry* entry,
133                                        const EventTagMap* map, char* messageBuf,
134                                        int messageBufLen);
135 
136 /**
137  * Formats a log message into a buffer
138  *
139  * Uses defaultBuffer if it can, otherwise malloc()'s a new buffer
140  * If return value != defaultBuffer, caller must call free()
141  * Returns NULL on malloc error
142  */
143 
144 char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
145                                 size_t defaultBufferSize,
146                                 const AndroidLogEntry* p_line,
147                                 size_t* p_outLength);
148 
149 /**
150  * Either print or do not print log line, based on filter
151  *
152  * Assumes single threaded execution
153  *
154  */
155 int android_log_printLogLine(AndroidLogFormat* p_format, int fd,
156                              const AndroidLogEntry* entry);
157 
158 #ifdef __cplusplus
159 }
160 #endif
161