• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <pthread.h>
20 
21 #include <android/log.h>
22 #include <log/event_tag_map.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 typedef enum {
29   /* Verbs */
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   /* Adverbs. The following are modifiers to above format verbs */
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   FORMAT_MODIFIER_YEAR,      /* Adds year to date */
44   FORMAT_MODIFIER_ZONE,      /* Adds zone to date, + UTC */
45   FORMAT_MODIFIER_EPOCH,     /* Print time as seconds since Jan 1 1970 */
46   FORMAT_MODIFIER_MONOTONIC, /* Print cpu time as seconds since start */
47   FORMAT_MODIFIER_UID,       /* Adds uid */
48   FORMAT_MODIFIER_DESCRIPT,  /* Adds descriptive */
49   /* private, undocumented */
50   FORMAT_MODIFIER_TIME_NSEC, /* switches from msec to nsec time precision */
51 } AndroidLogPrintFormat;
52 
53 typedef struct AndroidLogFormat_t AndroidLogFormat;
54 
55 typedef struct AndroidLogEntry_t {
56   time_t tv_sec;
57   long tv_nsec;
58   android_LogPriority priority;
59   int32_t uid;
60   int32_t pid;
61   int32_t tid;
62   const char* tag;
63   size_t tagLen;
64   size_t messageLen;
65   const char* message;
66 } AndroidLogEntry;
67 
68 AndroidLogFormat* android_log_format_new();
69 
70 void android_log_format_free(AndroidLogFormat* p_format);
71 
72 /* currently returns 0 if format is a modifier, 1 if not */
73 int android_log_setPrintFormat(AndroidLogFormat* p_format,
74                                AndroidLogPrintFormat format);
75 
76 /**
77  * Returns FORMAT_OFF on invalid string
78  */
79 AndroidLogPrintFormat android_log_formatFromString(const char* s);
80 
81 /**
82  * filterExpression: a single filter expression
83  * eg "AT:d"
84  *
85  * returns 0 on success and -1 on invalid expression
86  *
87  * Assumes single threaded execution
88  *
89  */
90 
91 int android_log_addFilterRule(AndroidLogFormat* p_format,
92                               const char* filterExpression);
93 
94 /**
95  * filterString: a whitespace-separated set of filter expressions
96  * eg "AT:d *:i"
97  *
98  * returns 0 on success and -1 on invalid expression
99  *
100  * Assumes single threaded execution
101  *
102  */
103 
104 int android_log_addFilterString(AndroidLogFormat* p_format,
105                                 const char* filterString);
106 
107 /**
108  * returns 1 if this log line should be printed based on its priority
109  * and tag, and 0 if it should not
110  */
111 int android_log_shouldPrintLine(AndroidLogFormat* p_format, const char* tag,
112                                 android_LogPriority pri);
113 
114 /**
115  * Splits a wire-format buffer into an AndroidLogEntry
116  * entry allocated by caller. Pointers will point directly into buf
117  *
118  * Returns 0 on success and -1 on invalid wire format (entry will be
119  * in unspecified state)
120  */
121 int android_log_processLogBuffer(struct logger_entry* buf,
122                                  AndroidLogEntry* entry);
123 
124 /**
125  * Like android_log_processLogBuffer, but for binary logs.
126  *
127  * If "map" is non-NULL, it will be used to convert the log tag number
128  * into a string.
129  */
130 int android_log_processBinaryLogBuffer(struct logger_entry* buf,
131                                        AndroidLogEntry* entry,
132                                        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 
143 char* android_log_formatLogLine(AndroidLogFormat* p_format, char* defaultBuffer,
144                                 size_t defaultBufferSize,
145                                 const AndroidLogEntry* p_line,
146                                 size_t* p_outLength);
147 
148 /**
149  * Either print or do not print log line, based on filter
150  *
151  * Assumes single threaded execution
152  *
153  */
154 int android_log_printLogLine(AndroidLogFormat* p_format, int fd,
155                              const AndroidLogEntry* entry);
156 
157 #ifdef __cplusplus
158 }
159 #endif
160