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